paul_nicholls

Untitled

Apr 3rd, 2026 (edited)
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $ = $8000
  2.  
  3. // convert number
  4.   ld de,4
  5.   call bin2BCD
  6.  
  7. // display number
  8.   ld hl,bcd
  9.   ld b,3
  10.   call printBCD
  11.  
  12. // infinite loop
  13.   jp $
  14.  
  15. bcd:    db 0,0,0
  16.  
  17. // =============================================================================
  18. // 16-Bit Binary to 5-Digit BCD via Powers-of-2 LUT
  19. // Input: DE = 16-bit binary value to convert
  20. // Output: bcd (3 bytes, packed BCD, (msb, mid, lsb))
  21. // =============================================================================
  22.  
  23. bin2BCD:
  24.     // Clear the result buffer/digits
  25.     xor a
  26.     ld (bcd),   a
  27.     ld (bcd+1), a
  28.     ld (bcd+2), a
  29.  
  30.     ld b, 16          // Loop counter: 16 bits to process
  31.     ld hl, bcdLUT     // points to the start of our LUT
  32.  
  33. bitLoop:
  34.     // Check if the lowest bit of DE is 1
  35.     srl d // Shift DE right, bit 0 falls into Carry flag
  36.     rr  e // (This handles the full 16-bit shift)
  37.    
  38.     jr nc,nextBit // carry is clear so skip
  39.  
  40.   doThisBit:
  41.     // move to lsb in LUT
  42.     inc hl
  43.     inc hl
  44.  
  45.     // Perform 3-byte BCD addition: bcd = bcd + LUT_Entry
  46.     // Byte 2 (lsb)
  47.     ld a, (bcd+2)
  48.     add a,(hl)  
  49.     daa          
  50.     ld (bcd+2), a
  51.     dec hl
  52.    
  53.     // Byte 1 (middle byte)
  54.     ld a, (bcd+1)
  55.     adc a,(hl)
  56.     daa          
  57.     ld (bcd+1), a
  58.     dec hl
  59.  
  60.     // Byte 0 (msb)
  61.     ld a, (bcd+0)
  62.     adc a,(hl)  
  63.     daa          
  64.     ld (bcd+0), a
  65. nextBit:
  66.     // next LUT row
  67.     inc hl
  68.     inc hl
  69.     inc hl
  70.  
  71.     djnz bitLoop // Decrement B, loop if not zero (16 times)
  72.     ret
  73.  
  74. bcdLUT:
  75.     db $00, $00,$01  // 2^0  = 1
  76.     db $00, $00,$02  // 2^1  = 2
  77.     db $00, $00,$04  // 2^2  = 4
  78.     db $00, $00,$08  // 2^3  = 8
  79.     db $00, $00,$16  // 2^4  = 16
  80.     db $00, $00,$32  // 2^5  = 32
  81.     db $00, $00,$64  // 2^6  = 64
  82.     db $00, $01,$28  // 2^7  = 128
  83.     db $00, $02,$56  // 2^8  = 256
  84.     db $00, $05,$12  // 2^9  = 512
  85.     db $00, $10,$24  // 2^10 = 1024
  86.     db $00, $20,$48  // 2^11 = 2048
  87.     db $00, $40,$96  // 2^12 = 4096
  88.     db $00, $81,$92  // 2^13 = 8192
  89.     db $01, $63,$84  // 2^14 = 16384
  90.     db $03, $27,$68  // 2^15 = 32768
  91.  
  92.  
  93. // HL = Pointer to Packed BCD (High-to-Low order)
  94. // B = Number of totlal BCD bytes (e.g., 4)
  95. printBCD:
  96.     // check for zero value
  97.     push bc
  98.     push hl
  99.  
  100.     ld a,(bcd)
  101.     ld hl,bcd+1
  102.     or (hl)
  103.     inc hl
  104.     or (hl)
  105.  
  106.     jr nz,bcdNotZero // keep checking
  107.     // is zero
  108.     ld a,'0'
  109.     call PrintChar
  110.    
  111.     pop hl
  112.     pop bc
  113.     ret
  114.  
  115. bcdNotZero:    
  116.     pop hl
  117.     pop bc
  118.  
  119.     ld c, 1             // C flag: 1=Suppressing zeros, 0=Printing
  120.    
  121. loopByte:
  122.     ld a, (hl)
  123.     push bc
  124.     push hl
  125.    
  126.     // --- Process High Nibble ---
  127.     push af
  128.     rrca                // Rotate right 4 times
  129.     rrca
  130.     rrca
  131.     rrca
  132.     call ProcessNibble
  133.     pop af
  134.    
  135.     // --- Process Low Nibble ---
  136.     call ProcessNibble
  137.    
  138.     pop hl
  139.     pop bc
  140.     inc hl
  141.     djnz loopByte
  142.    
  143.     // --- Final Check ---
  144.     ld a, c
  145.     or a
  146.     ret nz              // If we are still suppressing, all were zero
  147.     ret
  148.  
  149. ProcessNibble:
  150.     and $0F            // Isolate low nibble
  151.    
  152.     // If it's a zero and we are still in suppression mode
  153.    jr nz, notZero
  154.    ld a, c
  155.    or a
  156.    ret nz              // Suppress this zero
  157.    ld a, 0             // Reset a to 0
  158.    jr printDigit
  159.    
  160. notZero:
  161.    ld c, 0             // Turn off suppression
  162.    
  163. printDigit:
  164.    add a, '0'
  165.    call PrintChar      // Call your character output routine
  166.    ret
  167.  
  168. PrintChar:
  169.    // Subroutine to output char in A to screen/serial
  170.  rst $10 // output digit
  171.    ret
  172.  
  173. printBCD_pair:
  174. // input: a = BCD pair
  175.  ld b,a // save a
  176.  and $f0
  177.  rrca
  178.  rrca
  179.  rrca
  180.  rrca
  181.  
  182.  call draw_digit
  183.  
  184.  ld a,b // restore a
  185.  and $0f
  186.  
  187. draw_digit:
  188.  add a,'0'
  189.  rst $10 // output digit
  190.  
  191.  ret
Advertisement
Add Comment
Please, Sign In to add comment