secret
Created

  • Download Gist
gistfile1.asm
Assembly
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
.word $1000
 
.org $1000
jmp main
 
.alias program $1f00
.alias data_array $2000
.alias loop_stack $2100
.alias loop_cache $1e00
 
.global prg_off
prg_off: ; stores 1 byte offset
.byte 0 ; for program data
data_off:
.byte 0 ; for data pointer
loop_stack_off:
.byte 0 ; for loop stack
loop_begin_to_eat:
.byte 0
loop_entry:
.byte 0
 
.global main
main: ; clear memory and offsets
ldx #$00
lda #$00
sta prg_off
sta loop_stack_off
sta data_off
_clear_data:
sta data_array,X
sta loop_stack,X
sta loop_cache,X
inx
bne _clear_data
.global decode_next
decode_next:
ldx prg_off
inc prg_off
 
lda program, X
 
cmp #$00 ; end
beq done
 
cmp #$2b ; '+'
beq opj_add
 
cmp #$2d ; '-'
beq opj_sub
 
cmp #$3e ; '>'
beq opj_fwd
 
cmp #$3c ; '<'
beq opj_back
 
cmp #$5b ; '['
beq opj_loop_beg
 
cmp #$5d ; ']'
beq opj_loop_end
 
cmp #$2e ; '.'
beq opj_print
 
jmp decode_next
.global done
done:
rts
 
; silly jump distance limitations
opj_add:
jmp op_add
opj_sub:
jmp op_sub
opj_fwd:
jmp op_fwd
opj_back:
jmp op_back
opj_loop_beg:
jmp op_loop_beg
opj_loop_end:
jmp op_loop_end
opj_print:
jmp op_print
 
.global op_add
op_add:
ldx data_off
inc data_array,X
jmp decode_next
op_sub:
ldx data_off
dec data_array,X
jmp decode_next
 
op_fwd:
inc data_off
jmp decode_next
op_back:
dec data_off
jmp decode_next
 
.scope
op_loop_beg:
ldx data_off
lda data_array,X
cmp #$00
beq _exit_loop
 
dec loop_stack_off
ldx loop_stack_off
 
lda prg_off
sta loop_stack,X
jmp decode_next
_exit_loop:
lda #$01
sta loop_begin_to_eat
 
ldx prg_off
dex
 
inc prg_off
 
lda loop_cache,X
 
cmp #$00
bne _cache_hit
 
stx loop_entry
_consume:
ldx prg_off
inc prg_off
 
lda program,X
 
cmp #$00 ; end
beq done
 
cmp #$5b ; '['
beq _eat_more
 
cmp #$5d ; ']'
beq _got_end
 
jmp _consume
_eat_more:
inc loop_begin_to_eat
jmp _consume
_got_end:
dec loop_begin_to_eat
bne _consume
_exit:
ldx loop_entry
lda prg_off
 
sta loop_cache,X
 
jmp decode_next
_cache_hit:
sta prg_off
jmp decode_next
.scend
 
.scope
op_loop_end:
ldx data_off
lda data_array,X
cmp #$00
beq _exit_loop
 
ldx loop_stack_off
lda loop_stack,X
sta prg_off
 
jmp decode_next
_exit_loop:
inc loop_stack_off
jmp decode_next
.scend
 
.global op_print
op_print:
ldx data_off
lda data_array,X
jsr $FFD2
jmp decode_next

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.