Skip to content

Instantly share code, notes, and snippets.

@HMPerson1
Last active Jun 28, 2020
Embed
What would you like to do?
value = $0200 ; 2 bytes
; tmp1 is (tmp1H):Y:X
tmp1H = $0202 ; 1 bytes
tmp2 = $0203 ; 3 bytes
MOD10_LUT:
.byte 0
.byte 1
.byte FF
.byte 2
.byte 3
.byte 3
.byte 4
.byte FF
.byte 5
.byte 6
.byte FF
.byte 7
.byte 8
.byte 8
.byte 9
.byte FF
; tmp1 <- value << 1 (== value * 2)
LDA value
ASL
TAX
LDA value+1
ROL
TAY
LDA #$0
ROL
STA tmp1H
; tmp1 <- tmp1 + value (== value * 3)
; CLC -- carry is already 0 from ROL
TXA
ADC value
TAX
TYA
ADC value+1
TAY
BCC l1
INC tmp1H
l1:
; tmp1 <- tmp1 << 2 (== value * 0xC)
TXA
ASL
TAX
TYA
ROL
TAY
ROL tmp1H
TXA
ASL
TAX
TYA
ROL
TAY
ROL tmp1H
; tmp2 <- tmp1 << 1 (== value * 0x18)
TXA
ASL
STA tmp2
TYA
ROL
STA tmp2+1
LDA tmp1H
ROL
STA tmp2+2
; tmp2 <- tmp2 << 3 (== value * 0xC0)
ASL tmp2
ROL tmp2+1
ROL tmp2+2
ASL tmp2
ROL tmp2+1
ROL tmp2+2
ASL tmp2
ROL tmp2+1
ROL tmp2+2
; tmp2 <- tmp2 + tmp1 (== value * 0xCC)
; CLC -- carry is already 0 from ROL
TXA
ADC tmp2
STA tmp2
TYA
ADC tmp2+1
STA tmp2+1
LDA tmp1H
ADC tmp2+2
STA tmp2+2
; tmp1 <- (tmp2 + value) >> 8 (== (value * 0xCD) >> 8)
; CLC -- carry is already 0 from ADC
LDA tmp2
ADC value
LDA tmp2+1
ADC value+1
TAX
LDA tmp2+2
ADC #$0
TAY
; value[1]:Y:X <- tmp1 + tmp2 (== (value * 0xCCCD) >> 8)
; CLC -- carry is already 0 from ADC
TXA
ADC tmp2
TAX
TYA
ADC tmp2+1
TAY
LDA #$0
ADC tmp2+2
STA value+1
; X <- (((Y:X) << 1) >> 8) & 0xF (== ((value * 0xCCCD) >> 15) & 0xF)
TXA
ASL
TYA
ROL
AND #$0F
TAX
; value <- value[1]:Y >> 3 (== (value * 0xCCCD) >> 19) (== value / 10)
TYA
LSR value+1
ROR
LSR value+1
ROR
LSR value+1
ROR
STA value
; A <- MOD10_LUT[X]
LDA MOD10_LUT,X
; DONE!
; A == value % 10
; value == value / 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment