Chapter 10
Code Conversion, BCD Arithmetic,
and,
16-Bit Data Operations
Microprocessors & Interfacing 1
Introduction
• In microcomputer applications, various number systems and
codes are used to input data or to display results.
• The ASCII (American Standard Code for Information
Interchange) keyboard - input device for disk‑based
microcomputer systems.
• Alphanumeric characters (letters and numbers) - CRT (cathode
ray tube) terminal using the ASCII code.
• However, microprocessor - binary
Microprocessors & Interfacing 2
Introduction
• Therefore, data must be converted from one code to another
code.
• Four general categories
– Conversion based on the position of a digit in a number
(BCD to binary and vice versa).
– Conversion based on hardware consideration (binary to
seven‑segment code using table look‑up procedure)
– Conversion based on sequential order of digits (binary to
ASCII and vice versa)
– Decimal adjustment in BCD arithmetic operations. (This is
an adjustment rather than a code conversion.)
Microprocessors & Interfacing 3
BCD‑TO‑BINARY CONVERSION
• stores two 4-bit BCD numbers in an 8‑bit register or a
memory location. These numbers are called packed
BCD
• Method : positional weighting
– Example : 7210 = 7 x 10 + 2
• 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.
• Solution 7210 = 0111 0010BCD
Step 1: 0111 0010
0000 0010 Unpacked BCD1
0000 0111 Unpacked BCD2
Step 2: Multiply BCD2 by 10 (7 x 10)
Step 3: Add BCD1 to the answer in Step 2
Microprocessors & Interfacing 5
BCD‑TO‑BINARY CONVERSION…
• Program: 2‑Digit BCD‑to‑Binary Conversion
• 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…
Microprocessors & Interfacing 7
Program
Microprocessors & Interfacing 8
BINARY‑TO‑BCD CONVERSION
• Example :
– 111 1 1 1 12 (FFH) = 25510
• To represent this number in BCD requires twelve
bits or three BCD digits, labeled here as BCD3
(MSB), BCD2, and BCD1 (LSB),
= 0 0 1 0 0 1 0 1 0 1 0 1
BCD3 BCD2 BCD1
Microprocessors & Interfacing 9
BINARY‑TO‑BCD CONVERSION
Microprocessors & Interfacing 10
Program: Binary to Unpacked BCD Conversion
• PROBLEM STATEMENT
• A binary number is stored in memory location
BINBYT.
• Convert the number into BCD, and store each
BCD as two unpacked BCD digits in the Output
Buffer.
• To perform this task, write a main program and
two subroutines:
– one to supply the powers of ten, and
– the other to perform the conversion.
Microprocessors & Interfacing 11
Program: Binary to Unpacked BCD Conversion
Microprocessors & Interfacing 12
Program: Binary to Unpacked BCD Conversion
Microprocessors & Interfacing 13
Program: Binary to Unpacked BCD Conversion
Microprocessors & Interfacing 14
Program: Binary to Unpacked BCD Conversion
Microprocessors & Interfacing 15
BCD‑to‑seven‑segment‑led Code Conversion
• When a BCD number is to be displayed by a seven‑segment
LED, it is necessary to convert the BCD number to its
seven‑segment code.
• The code is determined by hardware considerations such as
common‑cathode or common‑anode LED
• The code has no direct relationship to binary numbers.
• Therefore, to display a BCD digit at a seven‑segment LED, the
table look‑up technique is used.
Microprocessors & Interfacing 16
BCD‑to‑seven‑segment‑led Code Conversion
• In the table look‑up technique,
– The codes of the digits to be displayed are stored
sequentially in memory.
– The conversion program locates the code of a digit based on
its magnitude and transfers the code to the MPU to send out
to a display port
3F 3000
06
• Example : Next slide
a
5B
3001
3002
4F 3003
66 3004
f b 7 6 5 4 3 2 1 0
g 6D 3005
h g f e d c b a
7D 3006
e c
07 3007
7F 3008
d h
6F 3009
Microprocessors & Interfacing 17
BCD‑to‑seven‑segment‑led Code Conversion
• PROBLEM STATEMENT
– A set of three packed BCD numbers (six digits) representing
time and temperature are stored in memory locations starting
at XX50H.
– The seven‑segment codes of the digits 0 to 9 for a
common‑cathode LED are stored in memory locations starting
at XX70H, and the Output‑Buffer memory is reserved at
XX90H.
– Write a main program and two subroutines, called UNPAK and
LEDCOD, to unpack the BCD numbers and select an
appropriate seven‑segment code for each digit.
– The codes should be stored in the Output‑Buffer memory.
Microprocessors & Interfacing 18
BCD‑to‑seven‑segment‑led Code Conversion
• PROGRAM :
• Common Cathod – output bit =1 else 0
• Common Anod – Output bit =0 else 1
• Main Program
LXI SP,STACK ;Initialize stack pointer
LXI H,XX50H ;Point HL where BCD digits are stored
MVI D,03H ;No of digits to be converted is placed in D
CALL UNPAK ;Call subroutine to unpack BCD numbers
HLT ;End of conversion
Microprocessors & Interfacing 19
BCD‑to‑seven‑segment‑led Code Conversion
;This subroutine unpacks the BCD number in two single digits
;Input: Starting memory address of the packed BCD numbers in HL registers
; Number of BCDs to be converted in register D
;Output: Unpacked BCD into accumulator and output
;Buffer address in BC ,Calls subroutine LEDCOD
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
Microprocessors & Interfacing 21
BCD‑to‑seven‑segment‑led Code Conversion
;This subroutine converts an unpacked BCD into its seven‑segment‑LED code
;Input: An unpacked BCD in accumulator
;Memory address of the buffer in BC register
;Output: Stores seven‑segment code in the output buffer
LEDCOD:
PUSH H ;Save HL contents of the caller
LXI H,CODE(XX70H);Point index to beginning of seven‑segment code
ADD L Add BCD digit to starting address of the code
MOV L,A ;Point HL to appropriate code
MOV A,M ;Get seven‑segment code
STAX B ;Store code in buffer
POP H
RET
Microprocessors & Interfacing 2
2
BINARY‑TO‑ASCII AND
ASCII‑TO‑BINARY CODE CONVERSION
Microprocessors & Interfacing 2
3
BINARY‑TO‑ASCII AND ASCII‑TO‑BINARY CODE
CONVERSION
• The American Standard Code for Information Interchange (known
as ASCII) is used commonly in data communication.
• It is a seven‑bit code
• Total = 128 (27) combinations
• Example
– 30H to 39H represent 0 to 9 ASCII decimal numbers
– 41H to 5AH represent capital letters A through Z; in this code,
bit D7 is zero.
– 61H to 7AH represent capital letters a through z; in this code,
bit D7 is zero
Microprocessors & Interfacing 2
4
BINARY‑TO‑ASCII AND ASCII‑TO‑BINARY CODE
CONVERSION
• The ASCII keyboard is a standard input device for entering
programs in a microcomputer.
• When an ASCII character is entered, the microprocessor
receives the binary equivalent of the ASCII Hex number.
• Example
– ASCII key for digit 9 is pressed, the microprocessor receives
the binary equivalent of 39H, which must be converted to the
binary 1001 for arithmetic operations.
– Similarly, to display digit 9 at the terminal, the microprocessor
must send out the ASCII Hex code (39H).
Microprocessors & Interfacing 2
5
BINARY‑TO‑ASCII CONVERSION
• 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.
Microprocessors & Interfacing 2
6
BINARY‑TO‑ASCII CONVERSION
Digit Hex Decimal Comment
0 30 48
1 31 49
2 32
3 33
4 34
5 35
6 36
7 37
8 38
9 39 57 Difference
is of
A 41 65 7
B 42
Microprocessors & Interfacing 2
7
BINARY‑TO‑ASCII CONVERSION
• MAIN PROGRAM
LXI SP,STACK;Initialize stack pointer
LXI H,XX50H ;Point index where binary number is stored
LXI D,XX60H ;Point index where ASCII code is to be stored
MOV A,M ;Transfer byte
MOV B,A ;Save byte
RRC ;Shift high order nibble to the position of low order
RRC ; nibble
RRC
RRC
CALL ASCII ;Call conversion routine
Microprocessors & Interfacing 2
8
BINARY‑TO‑ASCII CONVERSION
• MAIN PROGRAM
STAX D ;Store first ASCII Hex in XX60H
INX D ;Point to next memory location, get ready to store
; next byte
MOV A,B ;Get number again for second digit
CALL ASCII
STAX D
HLT
Microprocessors & Interfacing 2
9
BINARY‑TO‑ASCII CONVERSION
;This subroutine converts a binary digit between 0 and F to ASCII Hex code
;Input: Single binary number 0 to F in the accumulator
;Output: ASCII Hex code in the accumulator
ASCII:
ANI 0FH ;Mask high‑order nibble
CPI 0AH ;Is digit less than 1010?
JC CODE ;If digit is less than 1010, go to CODE to add 30H
ADI 07H ;Add 7H to obtain code for digits from A to F
CODE: ADI 30H ;Add base number 30H
RET
Microprocessors & Interfacing 3
0
ASCII Hex‑to‑Binary Conversion
• PROBLEM STATEMENT
– Write a subroutine to convert an ASCII Hex number
into its binary equivalent.
– A calling program places the ASCII number in the
accumulator, and the subroutine should pass the
conversion back to the accumulator.
Microprocessors & Interfacing 31
ASCII Hex‑to‑Binary Conversion
SUBROUTINE
;This subroutine converts an ASCII Hex number into its binary equivalent
;Input: ASCII Hex number in the accumulator
;Output: Binary equivalent in the accumulator
ASCBIN:
SUI 30H ;Subtract 0 bias from the number
CPI 0AH ;Check whether number is between 0 and 9
RC ;If yes, return to main program
SUI 07H ;If not, subtract 7 to find number between A & F
RET
Microprocessors & Interfacing 3
2
BCD ADDITION
• In BCD Addition, any number larger than 9 (from A to
F) is invalid and needs to be adjusted by adding 6 in
binary
Microprocessors & Interfacing 33
BCD ADDITION
Microprocessors & Interfacing 3
4
Example : BCD ADDITION
• Example :
– Add two packed BCD numbers: 77 and 48.
• Solution
Microprocessors & Interfacing
DAA (Decimal Adjust Accumulator)
• DAA : Decimal Adjust Accumulator
– This is a 1‑byte instruction
– It adjusts an 8‑bit number in the accumulator to form
two BCD numbers by using the process described in
previous slides
– It uses the AC and the CY flags to perform the
adjustment
– All flags are affected
Microprocessors & Interfacing
Example : Addition of Unsigned BCD Numbers
• PROBLEM STATEMENT
A set of ten packed BCD numbers is stored in the memory
location starting at XX50H.
1. Write a program with a subroutine to add these numbers in
BCD. If a carry is generated, save it in register B, and adjust it
for BCD. The final sum will be less than 9999BCD.
2. Write a second subroutine to unpack the BCD sum stored in
registers A and B, and store them in the output‑buffer memory
starting at XX60H. The most significant digit (BCD4) should be
stored at XX60H and the least significant digit (BCD1) at
XX63H.
Microprocessors & Interfacing
Example : Addition of Unsigned BCD Numbers
START: LXI SP,STACK ;Initialize stack pointer
LXI H,XX50H ;Point index to XX50H
MVI C,COUNT ;Load register C with the count of BCD numbers
to be added
XRA A ;Clear accumulator (sum)
MOV B,A ;Clear register B to save carry
NXTBCD: CALL BCDADD ;Call subroutine to add BCD nos
INX H ;Point to next memory location
DCR C ;One addition of BCD number is complete,
decrement the counter
JNZ NXTBCD ;If all numbers are added go to next step,
otherwise go back
Microprocessors & Interfacing
Example : Addition of Unsigned BCD Numbers
LXI H,XX63H ;Point index to store BCD1 first
CALL UNPAK ;Unpack the BCD stored in the
accumulator
MOV A,B ;Get ready to store high‑order
BCD‑BCD3 and BCD4
CALL UNPAK ;Unpack and store BCD3 and BCD4 at
XX61H and XX60
HLT
Microprocessors & Interfacing
Example : Addition of Unsigned BCD Numbers
;This subroutine adds the BCD number from the memory to the accumulator and
decimal adjusts it. If the sum is larger than eight bits, it saves the carry and
decimal‑adjusts the carry and sum
;Input: The memory address in HL register where the BCD number is stored
;Output: Decimal‑adjusted BCD number in the accumulator and the carry in
register B
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
Microprocessors & Interfacing
Example : Addition of Unsigned BCD Numbers
MOV A,B ;Transfer CY sum from register B and add 01
ADI 01H
DAA ;Decimal‑adjust BCD from B
MOV B,A ;Save adjusted BCD in B
MOV A,D ;Place BCD1 and BCD2 in the accumulator
RET
Microprocessors & Interfacing
Example : Addition of Unsigned BCD Numbers
;This subroutine unpacks the BCD in the accumulator and the carry register and stores
them in the output buffer
;Input: BCD number in the accumulator, and the buffer address in HL registers
;Output: Unpacked BCD in the output buffer
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
Microprocessors & Interfacing
Example : Addition of Unsigned BCD Numbers
RRC ;Convert the most significant four bits into unpacked
BCD
RRC
RRC
RRC
MOV M,A ;Store high‑order BCD
DCX H ;Point to the next memory location
RET
Microprocessors & Interfacing
BCD SUBTRACTION
• Two BCD numbers can be subtracted by using the procedure
of 100's complement (also known as 10's complement), similar
to 2's complement.
• The 100's complement of a subtrahend can be added to a
minuend as illustrated:
Microprocessors & Interfacing 4
4
BCD SUBTRACTION
• Example,
– 82 ‑ 48 (= 34) can be performed as follows
100's complement of subtrahend 52 (100 ‑ 48 = 52)
Add minuend + 82
1/34
• In an 8‑bit microprocessor, it is not a simple process to
find 100's complement of a subtrahend (100BCD requires
twelve bits).
• Therefore, in writing a program, 100's complement is
obtained by finding 99's complement and adding 01.
Microprocessors & Interfacing 4
5
Subtraction of Two Packed BCD Numbers
• 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.
Microprocessors & Interfacing 4
6
Subtraction of Two Packed BCD Numbers
SUBROUTINE
;This subroutine subtracts two BCD numbers and adjusts the result to
; BCD values by using the 100's complement method
;Input: A minuend in register B and a subtrahend in register C
;Output: The result is placed in the accumulator
SUBBCD: MVI A,99H
SUB C ;Find 99's complement of subtrahend
INR A ;Find 100's complement of subtrahend
ADD B ;Add minuend to 100's complement of
subtrahend
DAA ;Adjust for BCD
RET
Microprocessors & Interfacing 4
7
ADVANCED INSTRUCTIONS
• 16‑Bit Data Transfer (Copy) and Data Exchange Group
• LHLD : Load HL registers direct
– This is a 3‑byte instruction
– The second and third bytes specify a memory location (the
second byte is a line number and the third byte is a page
number)
– Transfers the contents of the specified memory location to L
register
– Transfers the contents of the next memory location to H
register
Microprocessors & Interfacing 4
8
ADVANCED INSTRUCTIONS
• SHLD : Store HL registers direct
– This is a 3‑byte instruction
– The second and third bytes specify a memory location (the
second byte is a line number and the third byte is a page
number)
– Stores the contents of L register in the specified memory
location
– Stores the contents of H register in the next memory
location
Microprocessors & Interfacing 4
9
ADVANCED INSTRUCTIONS
• XCHG : Exchange the contents of HL and DE
– This is a 1‑byte instruction
– The contents of H register are exchanged with the contents of D
register, and the contents of L register are exchanged with the
contents of E register
Microprocessors & Interfacing 5
0
ADVANCED INSTRUCTIONS
• 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.
Microprocessors & Interfacing 51
ADVANCED INSTRUCTIONS
Microprocessors & Interfacing 5
2
Arithmetic Group
• Operation: Addition with Carry
– These instructions add the contents of the operand,
the carry, and the accumulator.
– All flags are affected
• Syntax :
– ADC R
– ADC M
– ACI 8‑bit
Microprocessors & Interfacing 5
3
Arithmetic Group
• 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.
Microprocessors & Interfacing 5
4
Arithmetic Group
Microprocessors & Interfacing 5
5
Arithmetic Group
• Operation: Subtraction with Carry
– These instructions subtract the contents of the operand
and the borrow from the contents of the accumulator
• Syntax :
– SBB R
– SBB, M
– SBI 8‑bit
Microprocessors & Interfacing 5
6
Arithmetic Group
• 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.
Microprocessors & Interfacing 5
7
Arithmetic Group
Microprocessors & Interfacing 5
8
Arithmetic Group
• Double Register ADD (DAD)
– Add register pair to register HL
– This is a 1‑byte instruction
– Adds the contents of the operand (register pair or stack
pointer) to the contents of HL registers
– The result is placed in HL registers
– The Carry flag is altered to reflect the result of the 16‑bit
addition.
– No other flags are affected
– The instruction set includes four instructions
• Syntax :
– DAD Rp
– DAD D - DAD H - DAD SP - DAD B
Microprocessors & Interfacing 5
– □ 9
Arithmetic Group
• Example
– Write instructions to display the contents of the stack
pointer register at output ports.
LXI H,0000H ;Clear HL
DAD SP ;Place the stack pointer contents in HL
MOV A,H ;Place high order address of the stack pointer in
the accumulator
OUT PORT1
MOV A,L ;Place low‑order address of the stack pointer in the
accumulator
OUT PORT2
Microprocessors & Interfacing 6
0
Instructions Related to the Stack Pointer and the
Program Counter
• XTHL: Exchange Top of the Stack with H and L
– The contents of L are exchanged with the contents of the
memory location shown by the stack pointer,
– the contents of H are exchanged with the contents of memory
location of the stack pointer +1.
Microprocessors & Interfacing 61
Instructions Related to the Stack Pointer and the
Program Counter
• Example :
– Write a subroutine to set the Zero flag and check
whether the instruction JZ (Jump on Zero) functions
properly, without modifying any register contents
other than flags.
Microprocessors & Interfacing 62
Instructions Related to the Stack Pointer and the
Program Counter
• Subroutine
CHECK:PUSH H
MVI L,FFH;Set all bits in L to logic 1
PUSH PSW ;Save flags on the top of the stack
XTHL ;Set all bits in the top stack location
POP PSW ;Set Zero flag
JZ NOEROR F
H L
JMP ERROR A
XX FF
NOEROR: POP H L
FF H
RET
H L XX
L
A F
H
Microprocessors & Interfacing 63
Instructions Related to the Stack Pointer and the
Program Counter
• SPHL: Copy H and L registers into the Stack Pointer Register
– The contents of H specify the high‑order byte and the
contents of L specify the low‑order byte
– The contents of HL registers are not affected
• PCHL: Copy H and L registers into the Program Counter
– The contents of H specify the high‑order byte and the
contents of L specify the low‑order byte
Microprocessors & Interfacing 6
4
Instructions Related to the Stack Pointer and the
Program Counter
• Example :
– Assume that the HL registers hold address 2075H. Transfer
the program to location 2075H
• Solution :
PCHL
Microprocessors & Interfacing 6
5
Miscellaneous Instruction
• CMC: Complement the Carry Flag (CY)
– If the Carry flag is 1, it is reset; and if it is 0, it is set
• STC: Set the Carry Flag
Microprocessors & Interfacing 6
6
MUTIPLICATION
Microprocessors & Interfacing 6
7
Program: Multiplication of Two 8‑Bit Unsigned Numbers
• 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
1. multiply two unsigned numbers placed in registers H and L.
2. return the result into the HL pair.
Microprocessors & Interfacing 6
8
Program: Multiplication of Two 8‑Bit Unsigned Numbers
• 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
Microprocessors & Interfacing 6
9
Program: Multiplication of Two 8‑Bit Unsigned Numbers
Subroutine
MLTPLY: This subroutine multiplies two 8‑bit unsigned numbers
;Input: Multiplicand in register E and multiplier in register D
;Output: Results in HL register
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
NOADD: XCHG :Place multiplicand in HL
DAD H ;And shift left
XCHG ;Retrieve shifted multiplicand
DCR B ;One operation is complete, decrement counter
JNZ NXTBIT ;Go back to next bit
RET
Microprocessors & Interfacing 71