Created
January 16, 2016 04:50
-
-
Save digarok/beaad77599a52c5967b3 to your computer and use it in GitHub Desktop.
Some code I used for doing decimal <-> hex conversions in my Apple IIgs memory testing app. This is actually straight 6502 code. Max int $FFFF.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* x/y = high/low | |
BINtoBCD stx BIN | |
sty BIN+1 | |
jsr BINBCD16 | |
ldx BCD | |
ldy BCD+1 | |
rts | |
BCDtoBIN | |
stx BCD | |
sty BCD+1 | |
jsr BCDBIN16 | |
ldx BIN | |
ldy BIN+1 | |
rts | |
BIN dw $0000 | |
BCD ds 3 | |
BINBCD16 SED ; Switch to decimal mode | |
LDA #0 ; Ensure the result is clear | |
STA BCD+0 | |
STA BCD+1 | |
STA BCD+2 | |
LDX #16 ; The number of source bits | |
CNVBIT ASL BIN+0 ; Shift out one bit | |
ROL BIN+1 | |
LDA BCD+0 ; And add into result | |
ADC BCD+0 | |
STA BCD+0 | |
LDA BCD+1 ; propagating any carry | |
ADC BCD+1 | |
STA BCD+1 | |
LDA BCD+2 ; ... thru whole result | |
ADC BCD+2 | |
STA BCD+2 | |
DEX ; And repeat for next bit | |
BNE :CNVBIT | |
CLD ; Back to binary | |
rts | |
* 16-bit mode!!! | |
BCDBIN16 | |
clc | |
xce | |
rep #$30 | |
stz BIN | |
lda BCD | |
and #$000F ;get 1's | |
sta BIN | |
lda BCD | |
and #$00F0 ;get 10's | |
lsr | |
lsr | |
lsr | |
lsr | |
jsr TIMES10 | |
clc | |
adc BIN ;add 10's back to BIN | |
sta BIN | |
lda BCD | |
and #$0f00 ;get 100's | |
xba | |
jsr TIMES10 | |
jsr TIMES10 | |
clc | |
adc BIN | |
sta BIN | |
lda BCD | |
and #$f000 ;get 1000's | |
xba | |
lsr | |
lsr | |
lsr | |
lsr | |
jsr TIMES10 | |
jsr TIMES10 | |
jsr TIMES10 | |
clc | |
adc BIN | |
sta BIN | |
sep #$30 | |
rts | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment