RosettaCodeData/Task/Count-in-octal/6502-Assembly/count-in-octal.6502
2023-07-01 13:44:08 -04:00

60 lines
1.3 KiB
Text

define SRC_LO $00
define SRC_HI $01
define DEST_LO $02
define DEST_HI $03
define temp $04 ;temp storage used by foo
;some prep work since easy6502 doesn't allow you to define arbitrary bytes before runtime.
SET_TABLE:
TXA
STA $1000,X
INX
BNE SET_TABLE
;stores the identity table at memory address $1000-$10FF
CLEAR_TABLE:
LDA #0
STA $1200,X
INX
BNE CLEAR_TABLE
;fills the range $1200-$12FF with zeroes.
LDA #$10
STA SRC_HI
LDA #$00
STA SRC_LO
;store memory address $1000 in zero page
LDA #$12
STA DEST_HI
LDA #$00
STA DEST_LO
;store memory address $1200 in zero page
loop:
LDA (SRC_LO),y ;load accumulator from memory address $1000+y
JSR foo ;convert accumulator to octal
STA (DEST_LO),y ;store accumulator in memory address $1200+y
INY
CPY #$40
BCC loop
BRK
foo:
sta temp ;store input temporarily
asl ;bit shift, this places the top bit of the right nibble in the bottom of the left nibble.
pha ;back this value up
lda temp
and #$07 ;take the original input and remove everything except the bottom 3 bits.
sta temp ;store it for later. What used to be stored here is no longer needed.
pla ;get the pushed value back.
and #$F0 ;clear the bottom 4 bits.
ora temp ;put the bottom 3 bits of the original input back.
and #$7F ;clear bit 7.
rts