Experiment No.02: LAB Manual Part A

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Course: Microprocessor Lab

Faculty: Deepthi Oommen


LAB Manual
PART A
Experiment No.02

A.1 Aim: Write assembly language program to perform Hex to BCD code conversion.

A.2 Prerequisite: knowledge about Hexadecimal, BCD numbers and instruction set of 8086

A.3 Outcome:
After successful completion of this experiment students will be able to
1. Use appropriate instructions to program microprocessor to perform
various task.
2. Develop the program in assembly/ mixed language for Intel 8086
processor
3. Demonstrate the execution and debugging of assembly/ mixed language
program

A.4 Theory
Theory:
Hexadecimal Number System:
The hexadecimal numeral system, often shortened to "hex", is a numeral system made up of 16
symbols (base 16). The standard numeral system is called decimal (base 10) and uses ten
symbols: 0,1,2,3,4,5,6,7,8,9. Hexadecimal uses the decimal numbers and six extra symbols.
There are no numerical symbols that represent values greater than ten, so letters taken from
the English alphabet are used, specifically A, B, C, D, E and F. Hexadecimal A = decimal 10,
and hexadecimal F = decimal 15. Being a Base-16 system, the hexadecimal numbering system
therefore uses 16 (sixteen) different digits with a combination of numbers from 0 through to 15.
In other words, there are 16 possible digit symbols.

Binary-Coded Decimal (BCD)


A binary-coded decimal (BCD) is a type of binary representation for decimal values where each
digit is represented by a fixed number of binary bits, usually between four and eight.The norm is
four bits, which effectively represent decimal values 0 to 9. This writing format system is used
because there is no limit to the size of a number. Four bits can simply be added as another
decimal digit, versus real binary representation, which is limited to the usual powers of two, such
as 16, 32 or 64 bits.
The following are the 4-bit binary representation of decimal values:
0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001

Example of Hexadecimal to BCD:


To convert from HEX to BCD, you have to first convert the HEX to Decimal, and then convert
the Decimal digits to BCD digits, by converting each Decimal digit to 4 binary digits.
Example: convert Hex 1A2B3C to BCD.
Convert HEX 1A2B3C to Decimal and convert the decimal to BCD.
Hexa to decimal of 1A2B3C = 1715004. We have to take this as 01715004 (add a 0 to the start)
to get even number of digits. Now convert each digit to binary.
Decimal to BCD:- 01715004 = 0000 0001 0111 0001 0101 0000 0000 0100.
So Hex to BCD of 1A2B3C:- 0000 0001 0111 0001 0101 0000 0000 0100 BCD.
A.4 Algorithm
For a 4 digit Hex number whose equivalent binary number is to be found i.e. FFFF H.
Initially we compare FFFF H with decimal 10000 ( 2710 H in Hex ). If number is greater than
10,000 we add it to DH register. Also, we subtract decimal 10,000 from FFFF H, each time
comparison is made. Then we compare the number obtained in AX by 1000 decimal. Each time
we subtract 1000 decimal from AX and add 1000 decimal to BX. Then we compare number
obtained in AX by 100 decimals. Each time we subtract 100 decimal from AX and add 100
decimal to BX to obtain BCD equivalent. Then we compare number obtained in AX with 10
decimal. Each time we subtract 10 decimal from AX and we add 10 decimal to BX. Finally we
add the result in BX with remainder in AX. The final result is present in register DH with
contains the 5th bit if present and register AX.

Algorithm For Hex to BCD Conversion:-

Step I : Initialize the data segment.


Step II : Initialize BX = 0000 H and DH = 00H.
Step III : Load the number in AX.
Step IV : Compare number with 10000 decimal. If below goto step VII else goto step V.
Step V : Subtract 10,000 decimal from AX and add 1 decimal to DH
Step VI : Jump to step IV.
Step VII : Compare number in AX with 1000, if below goto step X else goto step VIII.
Step VIII : Subtract 1000 decimal from AX and add 1000 decimal to BX.
Step IX : Jump to step VII.
Step X : Compare the number in AX with 100 decimal if below goto step XIII
Step XI : Subtract 100 decimal from AX and add 100 decimal to BX.
Step XII : Jump to step X
Step XIII : Compare number in AX with 10. If below goto step XVI
Step XIV : Subtract 10 decimal from AX and add 10 decimal to BX..
Step XV : Jump to step XIII.
Step XVI : Add remainder in AX with result in BX.
Step XVII : Display the result in DH and BX.
Step XVIII : Stop.
PART B
(PART B : TO BE COMPLETED BY STUDENTS)

(Students must submit the soft copy as per following segments within two hours of the practical. The
soft copy must be uploaded at the end of the practical)

Roll. No:31 Name: SHREE SANJAY MEHER

Class: TE-C Batch:C2

Date of Experiment:24/07/19 Date of Submission:31/07/19

Grade:

B.1 Observations and learning:

CODE: .model small

.stack 100

.code

mov ax, 0ffffh ; hex number to find it's bcd

mov bx, 0000

mov dh, 0

l9 : cmp ax, 10000 ; if ax>10000

jb l2

sub ax, 10000 ; subtract 10000

inc dh ; add 1 to dh

jmp l9
l2 : cmp ax, 1000 ; if ax>1000

jb l4

sub ax, 1000

add bx, 1000h ; add 1000h to result

jmp l2

l4 : cmp ax, 100 ; if ax>100

jb l6

sub ax, 100

add bx, 100h ; add 100h to result

jmp l4

l6 : cmp ax, 10 ; if ax>10

jb l8

sub ax, 10

add bx, 10h ; add 10h to result

jmp l6

l8 : add bx, ax ; add remainder

; to result

mov ah, 02

mov cx, 0204h ; Count to display

; 2 digits

go: rol dh, cl

mov dl, dh

and dl, 0fh


add dl, 30h ; display 2 msb digits

int 21h

dec ch

jnz go

mov ch, 04h ; Count of digits to be

; displayed

mov cl, 04h ; Count to roll by 4 bits

l12: rol bx, cl ; roll bl so that msb

; comes to lsb

mov dl, bl ; load dl with data to be

; displayed

and dl, 0fH ; get only lsb

cmp dl, 09 ; check if digit is 0-9 or letter A-F

jbe l14

add dl, 07 ; if letter add 37H else only add 30H

l14: add dl, 30H

mov ah, 02 ; Function 2 under INT 21H (Display character)

int 21H

dec ch ; Decrement Count

jnz l12

mov ah, 4cH

int 21H

end
OUTPUT:

B.2 Conclusion:
After successful completion of this experiment we are able to:
1. Use appropriate instructions to program microprocessor to perform
various task.
2. Develop the program in assembly/ mixed language for Intel 8086
processor
3. Demonstrate the execution and debugging of assembly/ mixed language
program

B.5 Question of Curiosity


Q1. List out and explain BCD and ASCII Arithmetic instruction

ANS: ASCII Arithmetic


The ASCII arithmetic instruction function with ASCII-coded numbers. These numbers range in value from 30H to
39H for the numbers 0 to 9. There are four instructions used with ASCII arithmetic operations:
1. AAA (ASCII Adjust after Addition)
2. AAD (ASCII Adjust before Division)
3. AAM (ASCII Adjust after Multiplication)
4. AAS (ASCII Adjust after Subtraction)
These instructions use register AX as the source and as destination.
1. AAA Instruction
 Adjust the sum of two unpacked BCD values to create an unpacked BCD result.
 The AL register is the implied source and destination operand for this instruction.
 The AAA instruction is only useful when its follow an ADD instruction that adds (binary addition) two unpacked
BCD values.
 The AAA instruction then adjusts the contents of AL register to contain the correct 1-digit unpacked BCD result.
 If the addition produces a decimal carry, the AH register is incremented by 1, and the CF and AF flags are set.

2. AAD Instruction
 Unlike all other adjustment instructions, the AAD instruction appears before a division.
 The AAD instruction requires that the AX register contain a two-digit unpacked BCD number (not ASCII) before
executing.
 After adjusting the AX register with AAD, it is divided by an unpacked BCD number to generate a single-digit
result in AL with any remainder in AH.
 Example:
MOV BL, 9H
MOV AX, 702H
AAD
DIV BL
 The above example show how 72 is unpacked BCD is divided by 9 to produce a quotient of 8. The 0702H loaded
into AX register is adjusted by the AAD instruction to 0048H.

3. AAM Instruction
 Adjust the result of the multiplication of two unpacked BCD values to create a pair of unpacked BCD values.
 The AX register is the implied source and destination operand for this instruction.
 The AAM instruction is only useful when it follows an MUL instruction that multiplies (binary multiplication) two
unpacked BCD values and stores a word result in AX registers.

4. AAS Instruction
 Adjust the result of the subtraction of two unpacked BCD values to create a unpacked BCD result.
 The AL register is the implied source and destination for this instructions.
 The AAS instruction is only useful when it follows a SUB instruction that subtracts (binary subtraction) one
unpacked BCD valued from another and stores a byte result in the AL register.
 If the subtraction produced a decimal carry, the AH register is decremented by 1, and the CF and AF flags are
set.
 If no decimal carry occurred, the CF and AF flags are cleared, and the AH register is unchanged.

Q2. Write a assembly language program in 8086 to add two 16 bit hexadecimal numbers.

ANS: data segment


a dw 0202
b dw 0408h
c dw ?
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov ax,a
mov bx,b
add ax,bx
mov c,ax
int 3
code ends
end start

You might also like