← Back to the simulator

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 0x1F is 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

GroupOpcodeWhat it doesOperandsTicksExample
Data transferMOVPut a value into a registerRd, reg|number1MOV R0, 5
LOADRead a memory cell into a registerRd, [number|reg]3 or 100LOAD R1, [4]
STOREWrite a register into a memory cellRs, [number|reg]3 or 100STORE R1, [8]
ArithmeticADDAdd two numbersRd, reg|number1ADD R0, R1
SUBSubtract a numberRd, reg|number1SUB R0, 1
Control flowJNZJump if not zeroRs, line number1JNZ R0, 2

MOV — Put a value into a register

PosAcceptsMeaning
1RdThe register that receives the answer
2reg | numberThe register to read, or a plain number

Changes: Registers (R0-R3). Flags: none.

Cost: 1 tick.

Common mistakes:
  • Operand 1 of MOV is not valid. Example: "MOV R0, 5".
  • MOV needs 2 operands, like "MOV R0, 5".
Initial state
Mini RAM (cells 0-15)
Box 0
Box 1
Box 2
Box 3

LOAD — Read a memory cell into a register

PosAcceptsMeaning
1RdThe 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.

Common mistakes:
  • Operand 2 of LOAD is not valid. Example: "LOAD R1, [4]".
  • Address 64 is outside memory (0 to 63).
Initial state
Mini RAM (cells 0-15)
Box 0
Box 1
Box 2
Box 3

STORE — Write a register into a memory cell

PosAcceptsMeaning
1RsThe 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.

Common mistakes:
  • STORE needs 2 operands, like "STORE R1, [8]".
  • Operand 1 of STORE is not valid. Example: "STORE R1, [8]".
Initial state
Mini RAM (cells 0-15)
Box 0
Box 1
Box 2
Box 3

ADD — Add two numbers

PosAcceptsMeaning
1RdThe register that receives the answer, and provides the first number
2reg | numberThe second number to add

Changes: Registers (R0-R3). Flags: none.

Cost: 1 tick.

Common mistakes:
  • The number 99999 is too big (max 65535).
  • Operand 2 of ADD is not valid. Example: "ADD R0, R1".
Initial state
Mini RAM (cells 0-15)
Box 0
Box 1
Box 2
Box 3

SUB — Subtract a number

PosAcceptsMeaning
1RdThe register that receives the answer, and provides the first number
2reg | numberThe number to subtract

Changes: Registers (R0-R3). Flags: none.

Cost: 1 tick.

Common mistakes:
  • SUB needs 2 operands, like "SUB R0, 1".
  • Operand 1 of SUB is not valid. Example: "SUB R0, 1".
Initial state
Mini RAM (cells 0-15)
Box 0
Box 1
Box 2
Box 3

JNZ — Jump if not zero

PosAcceptsMeaning
1RsThe register to check
2numberThe program line to jump to

Changes: PC (Program Counter). Flags: none.

Cost: 1 tick.

Common mistakes:
  • 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".
Initial state
Mini RAM (cells 0-15)
Box 0
Box 1
Box 2
Box 3

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.

Initial state
Mini RAM (cells 0-15)
Box 0
Box 1
Box 2
Box 3

2. Always jump

R3 is never 0, so the jump always happens and line 2 is skipped.

Initial state
Mini RAM (cells 0-15)
Box 0
Box 1
Box 2
Box 3