Stage-3 bootstrap

October 21, 2024 ยท View on GitHub

Implements a full assembler that supports text-format instructions, macros, and labels up to 15 characters long.

TODO

  • Multi-file support
  • Multiple register push/pop

Syntax

The first character read from a line determines the behaviour of this assembler:

  • #: Comment (ignore text until newline)
  • =abcd: Defines a 2-byte (16-bit) hex constant
  • :abcdefgh: Defines a global label
  • .abcdefgh: Defines a local label (local to the enclosing global)
  • tab: Assemble (copy) chars to output until a newline
  • newline: Blank line, skipped

Instruction set

InstructionExpansionNotes
data ......Literal data. Use backslash hex for escapes (\00, \ff)
mov? r1, r2=?12Loads a register from another
ldc r1,=\$1 Load constant
ldh r1, #2345=#1 2345Load register
ldh r1, 'c'=!1cLoad char constant
ldb [r1], r2=[12Load byte
ldw [r1], r2={12Load word
ldd [r1], r2=(12Load double word
stb [r1], r2[=12Store byte
stw [r1], r2{=12Store word
std [r1], r2(=12Store double word
add? r1, r2+?12Add with optional condition
sub? r1, r2-?12Subtract with optional condition
mul? r1, r2*?12Multiply with optional condition
div? r1, r2/?12Divide with optional condition
mod? r1, r2%?12Modulo with optional condition
or? r1, r2|?12Bitwise OR with optional condition
and? r1, r2&?12Bitwise AND with optional condition
xor? r1, r2^?12Bitwise XOR with optional condition
eq r1, r2?=12Compare equal
ne r1, r2?!12Compare not equal
gt r1, r2?>12Compare greater than
lt r1, r2?<12Compare less than
push r1- yd(=y1Push to stack
pop r1=(1y+ ydPop from stack
sys r1S 1 System call with 1 argument
sys r1, r2S 12System call with 2 arguments
sys r1, r2, r3S+123 System call with 3 arguments
sys r1, r2, r3, r4S+1234 System call with 4 arguments
sys r1, r2, r3, r4, r5S+12345 System call with 5 arguments
sys r1, r2, r3, r4, r5, r6S+123456System call with 6 arguments
jump=$z Unconditional jump
jump^+?ze=$z Jump if flag
jump?+^ze=$z Jump if not zero
ret=(xy+ yd= zxUnconditional return
ret?=(xy+?yd=?zxConditional return
call- yd=#x 000c+ xz(=yx=$z Function call
db 0\00Define zero byte
dw 0\00\00Define zero word
dd 0\00\00\00\00Define zero double word