Vous êtes sur la page 1sur 4

Subject: Microprocessor and Interfacing Experiment No.

09 DATE:

Title: To Study Code Conversion

Rev. No.

1.00

AIM: - To Study code Conversion THEORY: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 is a commonly used input device for disk-based microcomputer systems. Similarly, alphanumeric characters (letters and numbers) are displayed on a CRT (cathode ray tube) terminal using the ASCII code. However, inside the microprocessor, data processing is usually performed in binary. In some instances, arithmetic operations are performed in BCD numbers. Therefore, data must be converted from one code to another code. The programming techniques used for code conversion fall into four general categories: 1) 2) 3) 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 look up table procedure) conversion based on sequential order of digits (binary to ASCII and vice versa)

BCD to BINARY Conversion In most microprocessor based products, data are entered and displayed in decimal numbers. For example, in an instrumentation laboratory, readings such as voltage and current are maintained in decimal numbers, and data are entered through a decimal keyboard, The system monitor program of the instrument converts each key into an equivalent 4 bit binary number and stores two BCD numbers in an 8 bit register or a memory location. These numbers are called packed BCD. Even if data are entered in decimal digits, it is inefficient to process data in BCD numbers because, in each 4 bit combination, digits A through F are unused. Therefore, BCD numbers are generally converted into binary numbers for data processing. Following are the steps to convert BCD number into equivalent binary. 1) 2) 3) 4) 5) Separate an 8 bit packed BCD number(72H) into two 4 bit unpacked BCD digits: BCD1(upper nibble)(70H) and BCD2(lower nibble) (02H). Store BCD2 in register. (Register C 02H) Rotate BCD1 (70H) four times so upper nibble digit comes at lower nibble position. (07H) Multiply result 3 (07H) with 10 using add instruction. (44H) Add BCD2 with above result in step 4

Write a program to convert BCD number stored in register A to its equivalent Binary. Store the result in register A. mvi a,72 mov b,a ani 0fh mov c,a

ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE, RAJKOT

9.1

sum:

mov a,b ani 0f0h rrc rrc rrc rrc mov d,a xra a mvi e,0ah add e dcr d jnz sum add c hlt

BINARY to BCD Conversion In most microprocessor based products, numbers are displayed in decimal. However, if data processing inside the microprocessor is performed in binary, it is necessary to convert the binary results into their equivalent BCD numbers just before they are displayed. Following are the steps to convert binary number into equivalent BCD. 1) 2) 3) If number is less than 100, go to step 2, otherwise, subtract 100 repeatedly until the remainder is less than 100. The quotient is the most significant BCD digit, BCD3 If the number is less than 10, go to step 3, otherwise subract by 10 repeatedly until the remainder is less than 10. The quotient is BCD2. The remainder from step 2 is BCD1.

Write a program to convert Binary number into equivalent BCD(maximum 255d), stored in register A. Store the result BCD1, BCD2 and BCD3 in registers C, D and E respectively, or store result at memory location starting from 2000H. mvi a,048h mvi b,64h mvi c,00h mvi d,00h mvi e,00h sub b jc ans inr c jmp next add b mvi b,0ah sub b jc an1 inr d jmp ag1 add b
9.2

next:

ans: ag1:

an1:

ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE, RAJKOT

mov e,a lxi h,2000h mov m,c inx h mov m,d inx h mov m,e hlt 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 look up table technique is used. In 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. Following are the steps to convert BCD number into equivalent seven segment code. 1) 2) 3) Unpack the packed BCD number into BCD1 and BCD2. Store the equivalent seven segment codes into memory locations. Find the equivalent seven segment code for BCD1 and BCD2 using look up table technique and store them on memory locations.

Write a program to convert BCD number into equivalent seven segment code (2 digit) and store the result at memory locations starting from 2000H. BCD number is stored in registers A. lxi h,2000h mvi a,73 mov d,a ani f0h rrc rrc rrc rrc call sse mov m,a mov a,d ani 0fh call sse inx h mov m,a hlt sse: lxi b,2030h add c
9.3

ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE, RAJKOT

mov c,a ldax b ret hlt BINARY to ASCII CODE Conversion The American Standard Code for Information Interchange (known as ASCII) is used commonly in data communications. It is a seven-bit code, and its 128(2 7) combinations are assigned different alphanumeric characters. For example, the hexadecimal numbers 30H to 39H represent 0 to 9 ASCII decimal numbers, and 41H to 5AH represent capital letters A through Z, in this code bit D7 is zero. Following are the steps to convert BCD number into equivalent seven segment code. 1) Separate the two nibbles 2) Add 30 h to get its equivalent ASCII Write a program to convert BCD number (2 digit) into equivalent ASCII Hex Code. Store the result at memory locations starting from 2000H. lxi h,2000h mvi a,73 mov d,a ani f0h rrc rrc rrc rrc adi 30h mov m,a inx h mov a,d ani 0fh adi 30h mov m,a hlt

Grade

Lab-In-Charge

H.O.D.

ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE, RAJKOT

9.4

Vous aimerez peut-être aussi