Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
220 views

Code Conversion

This document discusses code conversion programs for converting between BCD, binary, and ASCII formats. BCD to binary conversion is achieved by separating the BCD digits, multiplying one digit by 10, and adding the results. Binary to BCD conversion divides the binary number by powers of 10 to extract the BCD digits. BCD to ASCII conversion relies on the fact that the ASCII codes for the decimal digits 0-9 are 48-57 in hexadecimal.

Uploaded by

Akhil Kumawat
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
220 views

Code Conversion

This document discusses code conversion programs for converting between BCD, binary, and ASCII formats. BCD to binary conversion is achieved by separating the BCD digits, multiplying one digit by 10, and adding the results. Binary to BCD conversion divides the binary number by powers of 10 to extract the BCD digits. BCD to ASCII conversion relies on the fact that the ASCII codes for the decimal digits 0-9 are 48-57 in hexadecimal.

Uploaded by

Akhil Kumawat
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

5.

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.

5.16.1 BCD to Binary Conversion

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.

There is a difference between binary and BCD equivalent of a decimal number.


For example
Binary equivalent of 3510 is 0010 00112
And BCD of 3510 is 0011 0101BCD

The principal of positional weighting is used for the conversion of a BCD number into its
binary equivalent.

For example, 3510 can be written as 3 x 10 + 5.

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.

These steps will be more clear after following example.

Example : Convert 3510 form BCD to its binary equivalent.

Solution :

3510 = 0011 0101BCD


Step 1: Separate two BCD digits in unpacked form.
BCD1- 0000 0101 and BCD2 - 0000 0011
Step 2: Multiply BCD2 with 1010 (i.e., 10102).
0000 0011 x 1010 = 0001 11102.
Step 3: Add the result with BCD1
0001 1110 + 0000 0101 = 0010 00112.
Which is binary equivalent of 3510.

5.16.2 Program to Convert 2-digit BCD to Binary

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.

5.16.3 Binary to BCD conversion

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.

These steps are used in the following program.

5.16.4 Program to Convert 8-bit binary to BCD

This program converts the 8-bit binary number to its equivalent BCD, using the method
explained above.

Example : Write an assembly language program, using subroutine, to convert a 8-bit


binary number stored at memory location 4000H and store the result as three unpacked
BCD numbers at memory locations 4001H, 4002 and 4003H.

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

Label Mnemonics Remarks


BINBCD: MVI M, FFH Store -1 (in 2’s Complement form) in memory location
specified by HL register pair.
LOOP: INR M Increment the contents of memory location, pointed by HL
pair, by 1, to store the quotient (BCD2/BCD3).
SUB B Subtract 10x from the Accumulator.
JNC LOOP Goto ‘LOOP’ until the contents of A are not less than or equal
to B.
ADD B Else, Add 10x to Accumulator to get the actual remainder.
INX H Increment memory pointer HL.
RET Return to the calling program.

5.16.5 BCD to ASCII Code Conversion

ASCII (American Standard Code for Information Interchange) is commonly used in


computation to represent a character. It is a one-byte code; hence it can represent 256
characters and codes ranges from 0-255 (see Appendix --).

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.

Table 5.1 ASCII Codes for Decimal Numbers


Decimal Numbers ASCII (Hex) ASCII (Decimal)
(in BCD)
0 30H 48
1 31H 49
2 32H 50
3 33H 51
4 34H 52
5 35H 53
6 36H 54
7 37H 55
8 38H 56
9 39H 57
5.16.6 Program to convert BCD (0-9) to ASCII Hex code
Example : An 2-digit BCD number (e.g., 35BCD) is stored at memory location 3050H.
Write a program to
i. Separate the two decimal digits (as 03 and 05)
ii. Call subroutine to convert each digit into ASCII Hex code by adding 30H,
if the digit is more then 9, return 00H, showing invalid input.
iii. Store the codes at memory locations 3051H and 3052H
Write a subroutine to convert a BCD digit (0 to 9) into 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

Label Mnemonics Remarks


ASCII: ANI 0FH Mask 4 most significant bits of the Accumulator.
CPI 09H Check, whether the digit is more then 9.
JNC If it is more than 9, then it is an invalid input, jump to the
INVALID instruction labelled ‘INVALID’
ADI 30H Else, input is valid, Add 30H to get the desired ASCII code.
RET Return to the main program.
INVALID: XRA A In case of invalid input, clear the Accumulator.
RET And return to the main program.

5.16.7 ASCII to BCD Code Conversion

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

Label Mnemonics Remarks


ASCBCD: SUI 30H Subtract 30H from the ASCII code in the Accumulator, to get
equivalent BCD.
CPI 0AH Compare the result in the Accumulator with 0AH (1010).
RC If the contents of Accumulator are less than 10 10, return to the
calling program.
MVI A, FFH Else, store FFH in Accumulator,
RET Return to the calling program.

Above program is an example of multiple-ending subroutine, as it has two return


instructions.

More Examples (for Chapter 4)

Program to find the square of a number from lookup table

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.

Program to find the square-root of a given number

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.

For example, to find the square-root of 25 following steps are performed:

Number in Accumulator No. of subtraction


25
25 – 1 = 24 1
24 – 3 = 21 2
21 – 5 = 16 3
16 – 7 = 9 4
9–9=0 5 (Ans.)

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.

SOD SDE X R7.5 MSE M7.5 M6.5 M5.5


1 1 0 0 0 0 0 0 =C0H

After loading the above bit pattern in Accumulator, if SIM instruction is executed, ‘1’ will
sent to SOD line of the microprocessor.

Label Mnemonics Remarks


LXI H, 2000H Initialise memory pointer (HL register pair) with the starting
memory address of the data list.
XRA A Clear Accumulator.
MVI C, 0AH Initialise counter with number of data bytes in the list.
LOOP: ADD M Add data from memory location pointed by HL register pair.
JNC AHEAD If carry is not generated then jump to location labelled
‘AHEAD’
MOV B, A Else, save the sum in register B.
MVI A, C0H Load Accumulator with the byte according to format for SIM
instruction.
SIM Send ‘1’ at Serial Output data (SOD) line of the
microprocessor.
MOV A, B Restore the sum in the Accumulator.
AHEAD: INX H Increment memory pointer.
DCR C Decrement counter.
JNZ LOOP If counter is not zero then jump to location labelled ‘LOOP’
HLT Else, stop the program

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.

You might also like