Assembly guide: the 6 instructions
Assembly is made of the tiny instructions a CPU understands, each with an opcode (what to do) and operands (what to do it to). In this simulator, there are registers R0-R3, memory cells 0-63 in boxes of 4, and ticks to measure time. There are no flags and no stack in this simulator.
How to read an operand
- R0-R3: A register (tiny, instant storage).
- 0-65535: A plain number. Hexadecimal like
0x1Fis allowed. - [4]: Memory cell 4.
- [R2]: The memory cell whose address is currently stored in R2.
Memory cells are handled in boxes of 4 because the CPU fetches a whole box from the Warehouse to the Desk at once.
Instruction Cheat-sheet
| Group | Opcode | What it does | Operands | Ticks | Example |
|---|---|---|---|---|---|
| Data transfer | MOV | Put a value into a register | Rd, reg|number | 1 | MOV R0, 5 |
| LOAD | Read a memory cell into a register | Rd, [number|reg] | 3 or 100 | LOAD R1, [4] | |
| STORE | Write a register into a memory cell | Rs, [number|reg] | 3 or 100 | STORE R1, [8] | |
| Arithmetic | ADD | Add two numbers | Rd, reg|number | 1 | ADD R0, R1 |
| SUB | Subtract a number | Rd, reg|number | 1 | SUB R0, 1 | |
| Control flow | JNZ | Jump if not zero | Rs, line number | 1 | JNZ R0, 2 |
MOV — Put a value into a register
| Pos | Accepts | Meaning |
|---|---|---|
| 1 | Rd | The register that receives the answer |
| 2 | reg | number | The register to read, or a plain number |
Changes: Registers (R0-R3). Flags: none.
Cost: 1 tick.
- Operand 1 of MOV is not valid. Example: "MOV R0, 5".
- MOV needs 2 operands, like "MOV R0, 5".
LOAD — Read a memory cell into a register
| Pos | Accepts | Meaning |
|---|---|---|
| 1 | Rd | The register that receives the answer |
| 2 | [number] | [reg] | The memory address to read |
Changes: Registers (R0-R3). Flags: none.
Cost: 3 ticks if the box is on the Desk, 100 if not.
- Operand 2 of LOAD is not valid. Example: "LOAD R1, [4]".
- Address 64 is outside memory (0 to 63).
STORE — Write a register into a memory cell
| Pos | Accepts | Meaning |
|---|---|---|
| 1 | Rs | The register to save |
| 2 | [number] | [reg] | The memory address to write to |
Changes: Memory. Flags: none.
Cost: 3 ticks if the box is on the Desk, 100 if not.
- STORE needs 2 operands, like "STORE R1, [8]".
- Operand 1 of STORE is not valid. Example: "STORE R1, [8]".
ADD — Add two numbers
| Pos | Accepts | Meaning |
|---|---|---|
| 1 | Rd | The register that receives the answer, and provides the first number |
| 2 | reg | number | The second number to add |
Changes: Registers (R0-R3). Flags: none.
Cost: 1 tick.
- The number 99999 is too big (max 65535).
- Operand 2 of ADD is not valid. Example: "ADD R0, R1".
SUB — Subtract a number
| Pos | Accepts | Meaning |
|---|---|---|
| 1 | Rd | The register that receives the answer, and provides the first number |
| 2 | reg | number | The number to subtract |
Changes: Registers (R0-R3). Flags: none.
Cost: 1 tick.
- SUB needs 2 operands, like "SUB R0, 1".
- Operand 1 of SUB is not valid. Example: "SUB R0, 1".
JNZ — Jump if not zero
| Pos | Accepts | Meaning |
|---|---|---|
| 1 | Rs | The register to check |
| 2 | number | The program line to jump to |
Changes: PC (Program Counter). Flags: none.
Cost: 1 tick.
- JNZ jumps to line 99, but the program only has lines 0 to 1.
- Operand 1 of JNZ is not valid. Example: "JNZ R0, 2".
Compare and jump in this simulator
There is no CMP or JMP instruction. Here is how you do it:
1. Compare two numbers
R0 becomes 0 when they are equal; JNZ jumps only when R0 is not 0. In this example, R2 = 1 means "equal". SUB changes R0, so copy it first if you need the original.
2. Always jump
R3 is never 0, so the jump always happens and line 2 is skipped.