User Tools

Site Tools


base:decimal_to_hexadecimal_conversion

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Last revisionBoth sides next revision
base:decimal_to_hexadecimal_conversion [2015-04-17 04:31] – external edit 127.0.0.1base:decimal_to_hexadecimal_conversion [2016-02-14 21:59] verz
Line 44: Line 44:
 hiResult: .byte $00 hiResult: .byte $00
 loResult: .byte $00</code> loResult: .byte $00</code>
 +
 +
 +
 +By Verz:
 +
 +I made a small improvement, the idea being that a 10x multiplication can be computed as a sum of a (8x + 2x); since we're shifting the high nibble to the right, we get the 8x and 2x values in the process.
 +(Note that using a loResult in page zero there are 5 bytes/cycles of improvement)
 +
 +<code>
 +bcdbin
 +            lda #$00        ; Init result bytes
 +            ;sta loResult    ; useless
 +            sta hiResult
 +            lda loInput     ; Fetch ones and tens
 +            tay             ; Save to Y
 +            and #$f0        ; this two instructions, or use the undocumented ALR #$f0 = (and #$f0) + (lsr)
 +            lsr
 +            ;alr #$f0
 +            sta loResult
 +            lsr
 +            lsr
 +            adc loResult
 +            sta loResult
 +            tya
 +            and #$0f        ; Strip the ones
 +            adc loResult
 +
 +            ldx hiInput     ; Fetch the hundreds
 +            beq END         ; No hundreds? Then go to end
 +HUND:       clc
 +            adc #$64        ; Add as many hundreds as value of X
 +            bcc loop
 +            inc hiResult    ; Increase high byte every passed $FF
 +loop:       dex
 +            bne HUND
 +END:        sta loResult    ; Store low byte
 +            rts
 +
 +hiInput: .byte $00
 +loInput: .byte $01
 +hiResult: .byte $00
 +loResult: .byte $00</code>
 +
 +
 +
 +Here a routine to convert a single BCD byte (0-99) in A, to a binary in A:
 +
 +<code>
 +BCDBIN8
 +          tay
 +          and            #%11110000 
 +          lsr
 +          ;alr            #%11110000  ;  =(And #$f0)+(Lsr), to replace the two previous instructions
 +          sta            pzTmp     
 +          lsr
 +          lsr
 +          adc            pzTmp       
 +          sta            pzTmp      
 +          tya
 +          and            #%1111     
 +          adc            pzTmp      
 +          rts
 +          
 +pzTmp .equ $02    ; $02 is a unused zeropage memory location in C64
 +
 +</code>
 +
base/decimal_to_hexadecimal_conversion.txt · Last modified: 2016-02-14 22:02 by verz