Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ = $8000
- // convert number
- ld de,4
- call bin2BCD
- // display number
- ld hl,bcd
- ld b,3
- call printBCD
- // infinite loop
- jp $
- bcd: db 0,0,0
- // =============================================================================
- // 16-Bit Binary to 5-Digit BCD via Powers-of-2 LUT
- // Input: DE = 16-bit binary value to convert
- // Output: bcd (3 bytes, packed BCD, (msb, mid, lsb))
- // =============================================================================
- bin2BCD:
- // Clear the result buffer/digits
- xor a
- ld (bcd), a
- ld (bcd+1), a
- ld (bcd+2), a
- ld b, 16 // Loop counter: 16 bits to process
- ld hl, bcdLUT // points to the start of our LUT
- bitLoop:
- // Check if the lowest bit of DE is 1
- srl d // Shift DE right, bit 0 falls into Carry flag
- rr e // (This handles the full 16-bit shift)
- jr nc,nextBit // carry is clear so skip
- doThisBit:
- // move to lsb in LUT
- inc hl
- inc hl
- // Perform 3-byte BCD addition: bcd = bcd + LUT_Entry
- // Byte 2 (lsb)
- ld a, (bcd+2)
- add a,(hl)
- daa
- ld (bcd+2), a
- dec hl
- // Byte 1 (middle byte)
- ld a, (bcd+1)
- adc a,(hl)
- daa
- ld (bcd+1), a
- dec hl
- // Byte 0 (msb)
- ld a, (bcd+0)
- adc a,(hl)
- daa
- ld (bcd+0), a
- nextBit:
- // next LUT row
- inc hl
- inc hl
- inc hl
- djnz bitLoop // Decrement B, loop if not zero (16 times)
- ret
- bcdLUT:
- db $00, $00,$01 // 2^0 = 1
- db $00, $00,$02 // 2^1 = 2
- db $00, $00,$04 // 2^2 = 4
- db $00, $00,$08 // 2^3 = 8
- db $00, $00,$16 // 2^4 = 16
- db $00, $00,$32 // 2^5 = 32
- db $00, $00,$64 // 2^6 = 64
- db $00, $01,$28 // 2^7 = 128
- db $00, $02,$56 // 2^8 = 256
- db $00, $05,$12 // 2^9 = 512
- db $00, $10,$24 // 2^10 = 1024
- db $00, $20,$48 // 2^11 = 2048
- db $00, $40,$96 // 2^12 = 4096
- db $00, $81,$92 // 2^13 = 8192
- db $01, $63,$84 // 2^14 = 16384
- db $03, $27,$68 // 2^15 = 32768
- // HL = Pointer to Packed BCD (High-to-Low order)
- // B = Number of totlal BCD bytes (e.g., 4)
- printBCD:
- // check for zero value
- push bc
- push hl
- ld a,(bcd)
- ld hl,bcd+1
- or (hl)
- inc hl
- or (hl)
- jr nz,bcdNotZero // keep checking
- // is zero
- ld a,'0'
- call PrintChar
- pop hl
- pop bc
- ret
- bcdNotZero:
- pop hl
- pop bc
- ld c, 1 // C flag: 1=Suppressing zeros, 0=Printing
- loopByte:
- ld a, (hl)
- push bc
- push hl
- // --- Process High Nibble ---
- push af
- rrca // Rotate right 4 times
- rrca
- rrca
- rrca
- call ProcessNibble
- pop af
- // --- Process Low Nibble ---
- call ProcessNibble
- pop hl
- pop bc
- inc hl
- djnz loopByte
- // --- Final Check ---
- ld a, c
- or a
- ret nz // If we are still suppressing, all were zero
- ret
- ProcessNibble:
- and $0F // Isolate low nibble
- // If it's a zero and we are still in suppression mode
- jr nz, notZero
- ld a, c
- or a
- ret nz // Suppress this zero
- ld a, 0 // Reset a to 0
- jr printDigit
- notZero:
- ld c, 0 // Turn off suppression
- printDigit:
- add a, '0'
- call PrintChar // Call your character output routine
- ret
- PrintChar:
- // Subroutine to output char in A to screen/serial
- rst $10 // output digit
- ret
- printBCD_pair:
- // input: a = BCD pair
- ld b,a // save a
- and $f0
- rrca
- rrca
- rrca
- rrca
- call draw_digit
- ld a,b // restore a
- and $0f
- draw_digit:
- add a,'0'
- rst $10 // output digit
- ret
Advertisement
Add Comment
Please, Sign In to add comment