Code Conversion
Code Conversion
16 Code Conversion
Some code conversion programs are discussed in this section, such as, BCD to Binary,
Binary to BCD, Binary to ASCII and ASCII to Binary. Since, in many microprocessor
based applications such conversions are required.
Microprocessor processes in binary and real world uses decimal. Hence it is necessary to
convert the decimal number received from to real world into binary before being
processed in the microprocessor. In this section we will discuss the method and assembly
language program to convert a 2-Digit BCD number into equivalent binary number.
BCD is abbreviation of Binary Coded Decimal. There are two different forms of BCD
numbers: packed BCD and unpacked BCD.
In packed BCD form each digit of a decimal number is written in its equivalent 4-bit
binary number. For example, 4-bit packed BCD form of a decimal number 32 10 is
00110010BCD.
In unpacked BCD form each digit of a decimal number is written in its equivalent 8-bit
binary number. For example, 8-bit packed BCD form of a decimal number 510 is
00000101BCD.
The principal of positional weighting is used for the conversion of a BCD number into its
binary equivalent.
Therefore, to convert BCD of 3510 (i.e., 0011 0101BCD) into Binary of 3510 (i.e., 0010
00112) multiply the second digit from right (i.e., 3) by10 and adding the first digit from
right (i.e., 5). For doing this following step are performed.
1. Separate 4-bit packed BCD numbers in 8-bit unpacked form in two different
registers. For example, packed BCD form of 3510 (i.e., 0011 0101BCD) is to be
converted into two unpacked BCDs.
BCD1 - 510 (0000 0101) and
BCD2 - 310 (0000 0011)
2. Multiply BCD2 by 10, multiplication is performed using multiple addition
method discussed in example 4.--.
3. Add the result with the BCD1.
Solution :
Example : A 2-digit BCD number (i.e., between 0 and 99) is stored at memory location
3050H. Write a subroutine program to convert the 2-digit BCD number into binary
number. Also write the main program to read the BCD number from the memory, call
the subroutine program for BCD-to-Binary conversion and store the result at memory
location 3051H.
Solution:
In this program the technique of example 5.—is used to convert the BCD number into the
binary number. (i.e., BCD2 x 10 + BCD1). The program is written in the form of
subroutine and called from the main program.
Main Program:
Mnemonics Remarks
LXI SP, 5000H Initialise Stack Pointer
LXI H, 3050H Initialise memory pointer with the memory address 3050H.
MOV A, M Load the BCD number from the memory location pointed by HL pair.
CALL BCDBIN Call subroutine stored at memory location labelled ‘BCDBIN’
INX H Increment memory pointer (HL register pair)
MOV M, A Store the result at memory location 3051H.
HLT Stop the program.
Subroutine:
; Name: BCDBIN
; Function: Converts 2-Digit BCD to Binary equivalent
; Input: 2-digit packed BCD number in Accumulator.
; Output: A binary equivalent in Accumulator
; Register Modified: Accumulator
Label Mnemonics Remarks
BCDBIN: PUSH B Save the contents of BC register pair on the stack.
PUSH D Save the contents of DE register pair on the stack.
MOV B, A Copy the contents of Accumulator in register B
ANI 0FH Mask 4 most significant bits of the Accumulator.
MOV C, A Save unpacked BCD1 in register C.
MOV A, B Reload the original BCD back in Accumulator.
ANI F0H Mask 4 least significant bits of the Accumulator.
RRC Rotate right the contents of the accumulator 4 times to get
RRC unpacked BCD2.
RRC
RRC
MOV D, A Save unpacked BCD2 in register D.
XRA A Clear Accumulator.
MVI E, 0AH Load 1010 (=0AH) in register E.
SUM: ADD D Add D 10 times to multiply BCD2 with 10 (step 2).
DCR E
JNZ SUM
ADD C Add BCD1 with the result of above multiplication.
POP D Restore the previous contents of DE register pair.
POP B Restore the previous contents of BC register pair.
RET Return to the calling program.
As said earlier, Microprocessor processes in binary and real world uses decimal. Hence it
is necessary to convert the binary result obtained after processing in the microprocessor
into decimal number (in BCD form) before being send to the real world. In this section
we will discuss the method and assembly language program to convert a binary number
into equivalent 2-Digit BCD number.
The conversion of binary to BCD is performed by dividing the number by the powers of
ten; the division is performed by multiple subtraction method, discussed in example 4.--.
A microprocessor register is of 8-bits and may contain a 8-bit binary number ranges from
0000 0000 to 1111 1111 (i.e., 0-25510). Hence the 8-bit binary number may be equal to
maximum 3-digits of decimal.
The BCD equivalent of the maximum 8-bit binary number (i.e., 1111 1111 = 25510) is
= 0 0 1 0 0 1 0 1 0 1 0 1 (12 bits)
BCD3 BCD2 BCD1
Following step are used to convert a 8-bit binary number into its equivalent BCD number.
1. If number is less than 100 then goto step 2. Else, divide the given binary number
by 100 (by subtracting 100 repeatedly until the remainder is less than 100). The
quotient is the most significant BCD digit (i.e., BCD3). For example, if the given
number is 255 then
255 – 100 = 155 – 100 = 55 (which is less than 100). Since the subtraction is done
twice, hence the quotient is 2, which is the most significant digit. Its equivalent
binary, i.e., BCD3 is 0010.
2. Divide the remainder 10 (by subtracting 10 repeatedly until the remainder is less
than 10). Then equivalent binary of the quotient will be BCD2.
In our example 5510 is the previous remainder. Dividing it by 10, the quotient will
be 5. Its equivalent binary, i.e., BCD2 is 0101.
3. The equivalent binary of the remainder of above division will be BCD1.
In our example, the remainder of above division is 5. Its equivalent binary, i.e.,
BCD1 is 0101.
This program converts the 8-bit binary number to its equivalent BCD, using the method
explained above.
Solution:
Main Program:
Mnemonics Remarks
LXI SP, 5000H Initialise Stack Pointer.
LHI H, 4000H Initialise Memory Pointer (HL register pair) with memory address
4000H.
MOV A, M Load data into Accumulator from memory location pointed HL register
pair.
INX H Increment memory pointer HL.
MVI B, 64H Load register B with 102 (=10010=64H).
CALL BINBCD Call subroutine stored at memory location labelled ‘BINBCD’
MVI B, 0AH Load register B with 101 (=1010=0AH).
CALL BINBCD Call subroutine stored at memory location labelled ‘BINBCD’
MOV M, A Store BCD1 at memory location pointed by HL register pair.
HLT Stop the program.
Subroutine:
; Name: BINBCD
; Function: Converts 8-bit binary number into BCD; stores BCD2 and BCD3 in memory.
; Input: Binary number in Accumulator and Power of 10 (10x) in register B
; Output: BCD2 and BCD3 in memory. Returns remainder in Accumulator.
; Register Modified: Accumulator
The ASCII Hex codes of decimal numbers 0-9 are 30H-39H (i.e., 48 10-5710) see Table 5.1.
ASCII Hex codes 41H-5AH represent uppercase alphabets A-Z and 61H-7AH represent
lowercase alphabets a-z. When we enter ‘A’ from the keyboard, microprocessor coverts it
to the equivalent ASCII code ‘41H’. Similarly, to display ‘A’ on the monitor
microprocessor has to send 41H. This conversion is done by a program. This is illustrated
in the following examples, which converts an 8-bit binary number to its equivalent ASCII
Hex code.
Solution :
The main program separates the two digits and calls the subroutine ASCII for each digit.
It also stores the result obtained from the subroutine in the memory.
The subroutine converts the digit (0-9), sent from the main program in the Accumulator,
into its equivalent ASCII code, by adding 30H to it and returns the result to the calling
program. If the input digit is more then 9 (i.e., A-F) then it returns 00H.
Main Program:
Mnemonics Remarks
LXI SP, 5000H Initialise Stack Pointer.
LXI H, 3050H Initialise memory pointer, i.e., HL register
pair.
MOV A, M Load the data from the memory location
pointed by HL register pair.
MOV B, A Save the data in register B.
RRC Rotate the contents of Accumulator right 4
RRC times to shift 4 most significant bits to
RRC right.
RRC
CALL ASCII Call the subroutine ‘ASCII’ to convert first
digit to ASCII.
INX H Increment memory pointer; now pointing to
memory location 3051H.
MOV M, A Store the ASCII of the first digit in
memory.
MOV A, B Get the original data back in Accumulator.
CALL ASCII Call the subroutine ‘ASCII’ to convert next
digit to ASCII.
INX H Increment memory pointer.
MOV M, A Store the ASCII of the second digit in
memory.
HLT Stop the program.
Subroutine:
; Name: ASCII
; Function: Converts a BCD (0-9) to ASCII.
; Input: BCD number (0-9) in Accumulator.
; Output: ASCII code in Accumulator.
; Register Modified: Accumulator
Example : Write a program to convert ASCII Hex code, stored at memory location
3060H, into its BCD equivalent, using subroutine. Store the result at memory location
3061H.
Solution:
The main program will read the ASCII Hex code from the specified memory location,
call the subroutine to convert the ASCII to its equivalent BCD, store the result in the
memory.
The subroutine program will convert the ASCII Hex code to BCD equivalent by
subtracting 30H. It also checks whether the result is between 0-9 or not. If the result is
more than 9 then the subroutine returns FFH (showing invalid input) otherwise returns
the BCD (0-9).
Main Program:
Mnemonics Remarks
LXI SP, 5000H Initialise Stack Pointer.
LXI H, 3060H Initialise memory pointer, i.e., HL register
pair.
MOV A, M Load the data from the memory location
pointed by HL register pair.
CALL ASCBCD Call the subroutine ‘ASCBCD’ to convert
ASCII to BCD.
MOV M, A Store the ASCII of the second digit in
memory.
HLT Stop the program.
Subroutine:
; Name: ASCBCD
; Function: Converts ASCII to BCD.
; Input: ASCII code in Accumulator.
; Output: BCD number (0-9) in Accumulator. Returns FFH in case of invalid input.
; Register Modified: Accumulator
Example : Find the square of a decimal number, stored at memory location 2401H, using
lookup table. Store the result in the memory location 2402H.
Solution :
The squares of decimal numbers 0-9 are stored in the memory locations, from 2500H to
2509H, in BCD form. The memory location 2500H to 2509H is called lookup-table,
shown below.
Memory Squares of
Location decimal numbers (0-9)
(in BCD)
2500H 00
2501H 01
2502H 02
2503H 09
2504H 16
2505H 25
2506H 36
2507H 49
2508H 64
2509H 81
Observe the above table, memory location 2500H contains the square of 0, memory
location 2501H contains the square of 01, 2502H contains the square of 02. The higher
order address (i.e., 25H) is same in all memory addresses and lower order address is same
as the decimal number, whose square is stored in that location. For example, to find the
square of 05, we must see memory location 2505H in the lookup-table. This concept is
used in the following program. By storing 25H in register H and the data whose square is
to found in register L, we get the required memory address in HL register pair. The data
read from the memory address stored in HL register pair using MOV A, M gives the
square value of the data.
The limitation of the above program is that the data should be in the range 0-9.
Main Program:
Mnemonics Remarks
LDA 2401H Load the data in Accumulator from memory location 2401H
MOV L, A Copy the contents of Accumulator in register L
MVI H, 25H Load 25H in register H.
MOV A, M Read the square value from the memory address stored in HL register pair.
STA 2402H Store the result at memory location 2402H.
HLT Stop the program.
To calculate the square-root of a number, natural odd numbers (1, 3, 5, 7, 9, 11, …) are
subtracted in a sequence from the given number starting from 1, till the remainder is more
than the next odd number. For a perfect square the remainder will be zero after certain
subtractions. Square-root of a given number will be equal to the number of times
subtraction is performed.
Example : Find the square-root of the number stored at memory location 2550H. Store
the result at memory location 2551H.
Solution:
The concept of calculation of square-root, explained above, is implemented in the
following program.
Program:
Label Mnemonics Remarks
MVI C, 01 Initialise Counter with 01, to count number of subtractions.
MVI B, 01 Store 1st odd number, i.e. 01, in register C.
LDA 2550H Load the data from memory location 2550H, whose square-root
is to be calculated.
LOOP: SUB B Subtract odd number from the data.
INR B Increment register B twice to get the next odd number.
INR B
CMP B Compare the remainder in Accumulator with next odd number.
JC END If the contents of Accumulator are less than next odd number
then jump to location labelled ‘END’
INR C Else, increment counter.
JMP LOOP Jump to location labelled ‘LOOP’.
END: MOV A, C Copy the result in Accumulator.
STA 2551H Store it at memory location 2551H.
HLT Stop the program.
If the number is the perfect square the answer will be correct square-root of the number.
But, if the data is not the perfect square then the result will nearest but less then the
correct square-root of the number, since the microprocessor register can not store floating
point numbers.
For example, if the input data is 36 (=24H), the answer in register C will be 6, which is
the exact square-root of the number. But, if the input data is 37 (=25H), whose exact
square-root is 6.083, but the answer in register C will be 6.
Programs for Chapter 8.
Example : Write a program in assembly language of 8085 which adds 10 numbers stored
from memory location 2000H onwards. The carry generated at each stage should be
neglected in the addition process. However, the same should be transmitted out on the
serial output data (SOD) line of the microprocessor. (Raj. Univ., III BE (EE), 1993)
Solution :
The program will perform addition of data bytes stored in the memory location starting
from 2000H. In case of carry the partial sum is stored in register B and the following data
byte (C0H) according to format (also see fig. 8.4) for SIM instruction is loaded into the
Accumulator.
After loading the above bit pattern in Accumulator, if SIM instruction is executed, ‘1’ will
sent to SOD line of the microprocessor.
Example : Show which interrupt will be masked, if the following instructions are
executed.
MVI A, 10H
SIM
Solution:
According to the format (see fig. 8.4) for SIM instruction the bit pattern for 10H stored in
Accumulator, as given in the example, will be as follows:
D7 D6 D5 D4 D3 D2 D1 D0
SOD SDE X R7.5 MSE M7.5 M6.5 M5.5
0 0 0 1 0 0 0 0 =10H
which indicated that the Interrupt RST 7.5 will be reset, as D4 bit is 1.