Code Conversion
BCD to Binary, Binary to BCD, ASCII.
Digital systems frequently need to translate numbers between different representations such as BCD, binary, and ASCII. In 8085 programming, code conversion routines form the bridge between human-readable decimal formats and machine-efficient binary formats, and they appear consistently in both practical applications and GATE examination problems.
Core Concept: Why Code Conversion is Needed
Microprocessors process data in binary, but users interact with systems using decimal and text-based inputs and outputs. A keypad sends ASCII codes, a seven-segment display expects BCD or binary digit values, and computations require pure binary. Without code conversion routines, the processor cannot bridge these representations correctly.
The three most important conversions in 8085 programming are BCD to Binary, Binary to BCD, and ASCII to/from BCD or binary. Each uses specific arithmetic and logical instructions, and the underlying method follows a well-defined mathematical relationship between the number systems.
BCD to Binary Conversion
A packed BCD number stores two decimal digits per byte. To convert it to binary, the tens digit (upper nibble) must be multiplied by 10 and added to the units digit (lower nibble). The tens digit is extracted by masking with F0H and rotating right four times, giving its value in the lower nibble. The units digit is extracted by masking with 0FH.
Multiplication by 10 is achieved as: x10 = (x * 8) + (x * 2) which translates to three left-rotates or shifts for x8 and one for x2, then an ADD. Alternatively, with HL pair arithmetic, DAD H doubles HL; performing DAD H three times gives 8x, and adding the original twice more gives 10x. In practice, the most compact 8085 method uses the register and ADD instructions directly.
Binary to BCD Conversion
To convert a binary number to BCD, the standard approach is repeated division by 10. Dividing the binary number by 10 gives a quotient (tens digit) and remainder (units digit). If the number can exceed 99, divide again by 10 to separate hundreds.
Since 8085 has no hardware divide instruction, division is implemented through repeated subtraction or the double dabble algorithm (shift-and-adjust). In the double dabble method, the binary number is shifted left one bit at a time into a BCD register. After each shift, if any BCD nibble exceeds 4, it is increased by 3 before the next shift. After 8 shifts, the BCD register holds the BCD equivalent.
ASCII Conversion
ASCII codes for decimal digits 0 through 9 are 30H through 39H. To convert a BCD digit to its ASCII representation, simply add 30H. To convert ASCII back to a BCD digit, subtract 30H. For letters (A through F in hexadecimal), ASCII codes are 41H through 46H, so the offset from digit 9 requires adding 07H extra after 30H, making the full adjustment 37H for hex digits above 9.
In 8085, the conversion is done using ADI 30H (add immediate 30H) for digit to ASCII, and SUI 30H (subtract immediate 30H) for ASCII to digit. The register A holds the digit or ASCII value during conversion. After conversion, the result is stored or displayed.
Given:
Packed BCD number = 47H (represents decimal 47)
Convert to binary (pure hexadecimal binary value)
Why this formula applies:
BCD to binary: Binary = (Tens digit x 10) + Units digit
Formula:
Binary = ((BCD & F0H) >> 4) x 10 + (BCD & 0FH)
Substitution:
MVI A, 47H ; Load packed BCD 47
ANI F0H ; Mask lower nibble: A = 40H
RRC ; A = 20H
RRC ; A = 10H
RRC ; A = 08H
RRC ; A = 04H (tens digit = 4)
MOV B, A ; B = 04H
; Multiply B by 10: 4 x 10 = 40 = 28H
MOV C, B ; C = 4
ADD B ; A = 08H (x2)
ADD B ; A = 0CH, repeat to build x10... simplified:
; Direct: A = 04, x10 = 28H (use DAD or repeated ADD)
; After multiply: A = 28H
MVI A, 47H ; Reload original
ANI 0FH ; A = 07H (units digit)
ADD B ; A = 28H + 07H = 2FH
Calculation:
Tens = 4, Units = 7
Binary = 4 x 10 + 7 = 40 + 7 = 47 decimal = 2FH
Final Answer with units:
Binary result in accumulator = 2FH = 47 decimal. Correct conversion.Exam Tip: In GATE and university exams, BCD to binary and binary to BCD are commonly asked as trace-the-program questions. Remember: ASCII digit = BCD digit + 30H always. For hex digits A-F, the offset is 37H. Mixing up 30H and 37H is the most frequent mistake in ASCII conversion questions.
Summary of Conversion Methods
- BCD to Binary: extract tens nibble, multiply by 10 using shifts and adds, add units nibble.
- Binary to BCD: use repeated subtraction by 10 or the double dabble (shift-and-add-3) algorithm.
- ASCII to BCD digit: subtract 30H from ASCII code; valid for 30H through 39H.
- BCD digit to ASCII: add 30H; for hex digits A-F, add 37H instead.
- Packed BCD nibble extraction: upper nibble via ANI F0H + four RRC; lower nibble via ANI 0FH.
Quick Revision
- BCD to Binary formula: result = (tens x 10) + units; tens extracted by RRC x4 after ANI F0H.
- Binary to BCD: divide by 10 repeatedly using subtraction; or use double dabble for hardware-style conversion.
- ASCII digit offset: 30H for 0-9; 37H for A-F (hex digits).
- Conversion instructions: ANI (mask), RRC/RLC (rotate), ADI, SUI for constant adjustments.
- Packed BCD = two digits per byte; unpacked BCD = one digit per byte (upper nibble zero).
- Trap: Do not confuse BCD value 4 (binary 0100) with ASCII '4' (34H = 0011 0100).
Code Conversion Techniques
Evaluate data format transformations in assembly.
Q1.Which logical operation converts an unpacked BCD digit in AL to its ASCII equivalent?
Related Articles
BCD Arithmetic
DAA instruction usage.
6 min read
Arithmetic Programs
Addition, subtraction, multiplication buffer.
12 min read
Looping and Indexing
Counters, pointers, array processing.
11 min read
Stack and Subroutines
PUSH, POP, CALL, RET instructions.
12 min read
Assembly Language Basics
Labels, mnemonics, operands.
9 min read