IncOnes: LDA onesDigit ; load the lowest digit of the number CLC ADC #$01 ; add one STA onesDigit CMP #$0A ; check if it overflowed, now equals 10 BNE IncDone ; if there was no overflow, all done IncTens: LDA #$00 STA onesDigit ; wrap digit to 0 LDA tensDigit ; load the next digit CLC ADC #$01 ; add one, the carry from previous digit STA tensDigit CMP #$0A ; check if it overflowed, now equals 10 BNE IncDone ; if there was no overflow, all done IncHundreds: LDA #$00 STA tensDigit ; wrap digit to 0 LDA hundredsDigit ; load the next digit CLC ADC #$01 ; add one, the carry from previous digit STA hundredsDigit IncDone:
AddOnes: LDA onesDigit ; load the lowest digit of the number CLC ADC onesAdd ; add new number, no carry STA onesDigit CMP #$0A ; check if digit went above 9. If accumulator >= $0A, carry is set BCC AddTens ; if carry is clear, all done with ones digit ; carry was set, so we need to handle wrapping LDA onesDigit SEC SBC #$0A ; subtract off what doesnt fit in 1 digit STA onesDigit ; then store the rest INC tensDigit ; increment the tens digit AddTens: LDA tensDigit ; load the next digit CLC ADC tensAdd ; add new number STA tensDigit CMP #$0A ; check if digit went above 9 BCC AddHundreds ; no carry, digit done LDA tensDigit SEC SBC #$0A ; subtract off what doesnt fit in 1 digit STA tensDigit ; then store the rest INC hundredsDigit ; increment the hundreds digit AddHundreds: LDA hundredsDigit ; load the next digit CLC ADC hundredsAdd ; add new number STA hundredsDigit AddDone:
initial binary: 124 initial decimal: 000
1: compare to 100 2: 124 greater than 100, so subtract 100 and increment the decimal hundreds digit 3: repeat hundreds again
current binary: 024 current decimal: 100
1: compare to 100 2: 024 less than 100, so all done with hundreds digit
current binary: 024 current decimal: 100
1: compare to 10 2: 024 greater than 10, so subtract 10 and increment the decimal tens digit 3 repeat tens again
current binary: 014 current decimal: 110
1: compare to 10 2: 014 greater than 10, so subtract 10 and increment the decimal tens digit 3 repeat tens again
current binary: 004 current decimal: 120
etc for ones digit
HundredsLoop: LDA binary CMP #100 ; compare binary to 100 BCC TensLoop ; if binary < 100, all done with hundreds digit LDA binary SEC SBC #100 STA binary ; subtract 100, store whats left INC hundredsDigit ; increment the digital result JMP HundredsLoop ; run the hundreds loop again
TensLoop: LDA binary CMP #10 ; compare binary to 10 BCC OnesLoop ; if binary < 10, all done with hundreds digit LDA binary SEC SBC #10 STA binary ; subtract 10, store whats left INC tensDigit ; increment the digital result JMP TensLoop ; run the tens loop again
OnesLoop: LDA binary STA onesDigit ; result is already under 10, can copy directly to result
tempBinary - 16 bits input binary value decimalResult - 5 bytes for the decimal result
BinaryToDecimal: lda #$00 sta decimalResult+0 sta decimalResult+1 sta decimalResult+2 sta decimalResult+3 sta decimalResult+4 ldx #$10 BitLoop: asl tempBinary+0 rol tempBinary+1 ldy decimalResult+0 lda BinTable, y rol a sta decimalResult+0 ldy decimalResult+1 lda BinTable, y rol a sta decimalResult+1 ldy decimalResult+2 lda BinTable, y rol a sta decimalResult+2 ldy decimalResult+3 lda BinTable, y rol a sta decimalResult+3 rol decimalResult+4 dex bne BitLoop rts BinTable: .db $00, $01, $02, $03, $04, $80, $81, $82, $83, $84
My Gameboy collection 97% complete. My N64 collection 88% complete
My Gamecube collection 99% complete My NES collection 97% complete
I modified this to convert a 24-bit number to an 8 bit decimal. But it takes like 2200 cycles or 18 scan lines. Surely there's a faster way. Can anyone point me in the right direction?
;-----------------------------------------------------------------------------------------------------
; tempBinary - 16 bits input binary value
; decimalResult - 5 bytes for the decimal result
BinaryToDecimal:
lda #$00 ;2
sta decimalResult+0 ;4
sta decimalResult+1 ;4
sta decimalResult+2 ;4
sta decimalResult+3 ;4
sta decimalResult+4 ;4
sta decimalResult+5 ;4
sta decimalResult+6 ;4
sta decimalResult+7 ;4
ldx #$18 ;2
BitLoop:
asl tempBinary+0 ;6
rol tempBinary+1 ;6
rol tempBinary+2 ;6
ldy decimalResult+0 ;4
lda BinTable, y ;4
rol a ;2
sta decimalResult+0 ;4
ldy decimalResult+1 ;4
lda BinTable, y ;4
rol a ;2
sta decimalResult+1 ;4
ldy decimalResult+2 ;4
lda BinTable, y ;4
rol a ;2
sta decimalResult+2 ;4
ldy decimalResult+3 ;4
lda BinTable, y ;4
rol a ;2
sta decimalResult+3 ;4
ldy decimalResult+4 ;4
lda BinTable, y ;4
rol a ;2
sta decimalResult+4 ;4
ldy decimalResult+5 ;4
lda BinTable, y ;4
rol a ;2
sta decimalResult+5 ;4
ldy decimalResult+6 ;4
lda BinTable, y ;4
rol a ;2
sta decimalResult+6 ;4
rol decimalResult+7 ;6
dex ;2
bne BitLoop ;2
rts
BinTable:
.db $00, $01, $02, $03, $04, $80, $81, $82, $83, $84
;-----------------------------------------------------------------------------------------------------
gauauu: look, we all paid $10K at some point in our lives for the privilege of hanging out with Kevin
gauauu: look, we all paid $10K at some point in our lives for the privilege of hanging out with Kevin
LDA HexValue STA TempBinaryis different than this code:
LDA TempBinary CLC ADC HexValue STA TempBinaryLet say HexValue is == "x", TempBinary is == "y".
gauauu: look, we all paid $10K at some point in our lives for the privilege of hanging out with Kevin