Skip to main content

8085 ALP: BCD to binary conversion

Short answer

Extract the high nibble (AND F0H, rotate right 4 times), multiply by 10 using additions (×2, ×4, ×5, then ×2 to get ×10). Add the low nibble obtained via ANI 0FH. Convert ten bytes from 4350H; e.g., 47H becomes 2FH.

Write a program to convert ten BCD numbers stored at 4350H to binary and store the result at 4360H.

Each packed BCD byte holds two decimal digits, so binary value = (high digit × 10) + low digit. The high digit is isolated, multiplied by 10 using shifts-and-adds (×10 = ×4 + ×1, then ×2), and added to the low digit.

Algorithm

  1. HL → source (4350H), DE → destination (4360H), C = 10 (count).
  2. Take the byte, mask the upper nibble (F0H) and rotate right four times to bring it to the lower nibble.
  3. Multiply by 10: A×2 → A×4, add original (A×5), double (A×10).
  4. Add the lower nibble (byte AND 0FH). Store the result and advance the pointers.
        LXI  H,4350H      ; HL = source address of BCD numbers
        LXI  D,4360H      ; DE = destination address
        MVI  C,0AH        ; counter = 10 numbers
NEXT:   MOV  A,M          ; A = packed BCD byte
        ANI  0F0H         ; keep high digit only
        RRC               ; rotate right 4 times -> high digit in
        RRC               ; the lower nibble
        RRC
        RRC
        MOV  B,A          ; B = high digit (x)
        ADD  A            ; A = 2x
        ADD  A            ; A = 4x
        ADD  B            ; A = 5x
        ADD  A            ; A = 10x
        MOV  B,A          ; B = 10 x high digit
        MOV  A,M          ; get the byte again
        ANI  0FH          ; A = low digit
        ADD  B            ; A = 10 x high + low = binary
        STAX D            ; store binary result
        INX  H            ; next BCD number
        INX  D            ; next destination
        DCR  C            ; one number done
        JNZ  NEXT         ; repeat until all ten are converted
        HLT

Example: BCD 47H → high digit 4 → 40 (28H); low digit 7; result 28H + 07H = 2FH (47 decimal). BCD 99H → 63H.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Microprocessor chapter 2 questions →