UNIT6 - Code Conversion
UNIT6 - Code Conversion
• Procedure :
1. Separate an 8‑bit packed BCD number into two 4‑bit
unpacked BCD digits: BCD1 and BCD2.
2. Convert each digit into its binary value according to
its position.
3. Add both binary numbers to obtain the binary
equivalent of the BCD number.
Microprocessors & Interfacing 4
BCD‑TO‑BINARY CONVERSION…
• Convert 7BCD into its binary equivalent.
• PROBLEM STATEMENT
• A BCD number between 0 and 99 is stored in an
R/W memory location called the Input
Buffer(INBUF).
• Write a main program and a conversion
subroutine (BCDBIN) to convert the BCD number
into its equivalent binary number.
• Store the result in a memory location defined as
the Output Buffer (OUTBUF). 6
Microprocessors & Interfacing
BCD‑TO‑BINARY CONVERSION…
= 0 0 1 0 0 1 0 1 0 1 0 1
BCD3 BCD2 BCD1
UNPAK: LXI B,BUFFER(XX90H) ;Point BC index to the buffer memory, O/p code
NXTBCD:MOV A,M ;Get packed BCD number
ANI F0H ;Masked BCD,
RRC ;Rotate four times to place BCD
RRC
RRC
RRC
CALL LEDCOD ;Find seven‑segment code
INX B
Microprocessors & Interfacing 2
0
BCD‑to‑seven‑segment‑led Code Conversion
MOV A,M ;Get BCD number again
ANI 0FH ;Separate BCD,
CALL LEDCOD
INX B
INX H ;Point to next BCD
DCR D ;One conversion complete, reduce BCD count
JNZ NXTBCD ;If all BCDs are not yet converted, go back to
convert next BCD
RET
• PROBLEM STATEMENT
– An 8‑bit binary number (e.g., 9FH) is stored in memory
location XX50H.
1. Write a program to
a. Transfer the byte to the accumulator.
b. Separate the two nibbles (as 09 and 0F).
c. Call the subroutine to convert each nibble into ASCII Hex
code.
d. Store the codes in memory locations XX60H and XX61H.
2. Write a subroutine to convert a binary digit (0 to F) into ASCII Hex
code.
• MAIN PROGRAM
• MAIN PROGRAM
• Example :
– Add two packed BCD numbers: 77 and 48.
• Solution
BCDADD:
ADD M ;Add packed BCD byte and adjust it for BCD sum
DAA
RNC ;If no carry, go back to next BCD
MOV D,A ;If carry is generated, save the sum from the
accumulator
UNPAK:
MOV D,A ;Save BCD number
ANI 0FH ;Mask high‑order BCD
MOV M,A ;Store low‑order BCD
DCX H ;Point to next memory location
MOV A,D ;Get BCD again
ANI F0H ;Mask low‑order BCD
• Example,
– 82 ‑ 48 (= 34) can be performed as follows
• PROBLEM STATEMENT
– Write a subroutine to subtract one packed BCD number
from another BCD number.
– The minuend is placed in register B, and the subtrahend is
placed is register C by the calling program.
– Return the answer into the accumulator.
• Example
– Memory locations 2050H and 2051 H contain 3FH and 42H,
respectively, and register pair DE contains 856FH.
– Write instructions to exchange the contents of DE with the
contents of the memory locations.
• Syntax :
– ADC R
– ADC M
– ACI 8‑bit
• Example
– Registers BC contain 2793H, and registers DE contain
3182H.
– Write instructions to add these two 16‑bit numbers, and
place the sum in memory locations 2050H and 2051H.
• Syntax :
– SBB R
– SBB, M
– SBI 8‑bit
• Example
– Registers BC contain 8538H and registers DE contain
62A5H.
– Write instructions to subtract the contents of DE from the
contents of BC, and place the result in BC.
• Example
– Write instructions to display the contents of the stack
pointer register at output ports.
• Solution :
PCHL
• PROBLEM STATEMENT
– A multiplicand is stored in memory location XX50H and a
multiplier is stored in location XX51H. Write a main program
to
1. transfer the two numbers from memory locations to the
HL registers.
2. store the product in the Output Buffer at XX90H.
Write a subroutine to
• MAIN PROGRAM
LXI SP,STACK
LHLD XX50H ;Place contents of XX50 in L register and
contents of XX51 in H register
XCHG ;Place multiplier in D and multiplicand in E
CALL MLTPLY ;Multiply the two numbers
SHLD XX90H ;Store the product in locations XX90 and 91H
HLT
MLTPLY:
MOV A,D ;Transfer multiplier to accumulator
MVI D,00H ;Clear D to use in DAD instruction
LXI H,0000H ;Clear HL
MVI B,08H;Set up register B to count eight rotations
NXTBIT: RAR ;Check if multiplier bit is 1
JNC NOADD ;If not, skip adding multiplicand.
DAD D ;If multiplier is 1, add multiplicand to HL and place
partial result in HL
Microprocessors & Interfacing 7
0
Program: Multiplication of Two 8‑Bit Unsigned Numbers