Skip to content

Instantly share code, notes, and snippets.

@scorbiclife
Last active April 24, 2025 10:58
Show Gist options
  • Save scorbiclife/94a100e852699076763fa1ddbe173307 to your computer and use it in GitHub Desktop.
Save scorbiclife/94a100e852699076763fa1ddbe173307 to your computer and use it in GitHub Desktop.
Third is the little sibling of Forth
_start:
jmp main
; functions with fixed addresses go here
; User compiled code starts at address $2000
initCode:
lda #$00
sta $00
lda #$20
sta $01
rts
; Compile a byte stored on the accumulator
compileByte:
ldx #$00
sta ($00,x)
; increment user code address
inc $00
bne compileByteRet
inc $01
compileByteRet:
rts
; compile a rts instruction (opcode 60)
compileRts:
lda #$60
jsr compileByte
rts
; To compile instructions with addresses,
; we need a way to store an address on registers.
;
; We store a 2-byte address as follows:
; - store the lower byte on the X register
; - store the higher byte on the Y register
; compile the address stored in registers X and Y
compileAddr:
txa
jsr compileByte
tya
jsr compileByte
rts
compileJsr:
lda #$20
jsr compileByte
rts
compileJmpAbs:
lda #$4c
jsr compileByte
rts
compileJmpInd:
lda #$6c
jsr compileByte
rts
compileLdaImm:
lda #$a9
jsr compileByte
rts
compileLdxImm:
lda #$a2
jsr compileByte
rts
compileLdyImm:
lda #$a0
jsr compileByte
rts
compileLabel:
ldx $00
ldy $01
jsr compileLdxImm
txa
adc #$08
bcc endIny
iny
endIny: jsr compileByte
jsr compileLdyImm
tya
jsr compileByte
jsr compileJsr
; address of `compileAddr`
ldx #$1d
ldy #$06
jsr compileAddr
jsr compileRts
rts
compileStaAbs:
lda #$8d
jsr compileByte
rts
; User dictionary starts at address $4000
initDict:
lda #$00
sta $02
lda #$40
sta $03
rts
main:
; initialize scaffold
jsr initCode
jsr initDict
; dot label: address $2000
jsr compileLabel
; dot: address $2008
jsr compileLdaImm
lda #$01
jsr compileByte
jsr compileStaAbs
ldx #$00
ldy #$02
jsr compileAddr
jsr compileRts
; subroutine that calls dot: address $200e
jsr compileJsr
jsr $2000 ; run dot label -> compile dot address
jsr compileRts
jsr $200e
brk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment