At each “step”, the computer reads the memory word whose address is in $IP$ and then increments $IP$. Then it carries out the instruction that that word represents.
This is how most computers, uh, cores, work. It is called the fetch-execute cycle.
Each word that the computer tries to execute will be interpreted to have:
For a given instruction word, let’s denote by $op$ the hexadecimal digit corresponding to the first four bits, and let $x$ represent the last 28 bits. With these, we formally specify the operation of each instruction as follows.
(Note, "$:=$" is used to represent the "assignment" of the value to the right of this symbol to the element on its left.)
op | Mnemonic | Action | Remarks |
---|---|---|---|
0 | LOAD | $A := M[x]$ | Load accumulator from memory |
1 | STORE | $M[x] := A$ | Store accumulator to memory |
2 | IN | $A := P[x]$ | Read from a port into the accumulator |
3 | OUT | $P[x] := A$ | Write accumulator out to a port |
4 | ADD | $A := A + M[x]$ | Add into accumulator |
5 | SUB | $A := A - M[x]$ | Subtract from accumulator |
6 | MUL | $A := A \times M[x]$ | Multiply into accumulator |
7 | DIV | $A := A \div M[x]$ | Divide accumulator |
8 | MOD | $A := A \:\texttt{mod}\: M[x]$ | Modulo |
9 | AND | $A := A \,\&\, M[x]$ | Bitwise AND |
A | OR | $A := A \,\,|\,\, M[x]$ | Bitwise OR |
B | XOR | $A := A \wedge M[x]$ | Bitwise XOR |
C | JUMP | $IP := x$ | Jump to new address |
D | JZ | if $A = 0$ then $IP := x$ | Jump if accumulator is zero |
E | JLZ | if $A < 0$ then $IP := x$ | Jump if accumulator is less than zero |
F | JGZ | if $A > 0$ then $IP := x$ | Jump if accumulator is greater than zero |
This program, when loaded into memory at address 0, outputs powers of two, starting with 1, and going just past 1,000,000, to port 100 (64 hex):
C0000003 00000001 000F4240 00000001 30000064 40000001 10000001 50000002 E0000003 C0000009
The example above was hard to read. Just listing the contents of memory that the processor executes is called “writing machine language.” Let’s use the mnemonics given in the above table for each instruction to make things a bit clearer. Then the example becomes:
0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 |
C0000003 00000001 000F4240 00000001 30000064 40000001 10000001 50000002 E0000003 C0000009 |
JUMP start ; begin by jumping over the data area pow: 1 ; store the current power value here limit: 1000000 ; we'll be computing powers up to this amount start: LOAD pow ; bring the value into accumulator to use OUT 100 ; output the current power ADD pow ; adding to itself makes the next power! STORE pow ; store it (for next time) SUB limit ; we need to compare with limit, subtracting helps JLZ start ; if not yet past limit, keep going end: JUMP end ; this "stops" the program! |
In general, each line of an assembly language program contains:
;
character.
The process of converting from assembly language to machine language is called assembly. The reverse process is called disassembly. Yes, there exist programs called assemblers and disassemblers.