microprocessor(8086) lab

41
Connexions You are here: Home » Content » Microprocessor(8086) Lab Microprocessor(8086) Lab Module by: Rajeshwari Hegde. User rating (?): (1 rating) Summary: This module contains the programs involving Data transfer instructions, Arithmetic and logical instructions, string manipulation instructions etc Q1. Byte and word move using different addressing modes Program: .model small .data Num dw 4321h .code Mov ax,@data Mov ds,ax Mov ax,1234h ;immediate addressing Mov bx,ax ;register addressing Mov ax,num ;direct addressing Mov si,1000h Mov al,[si] ;indirect addressing Mov bl,[si+100h] ;relative addressing Mov bx,1000h Mov ax,[si+bx] ;base index addressing Mov cx,[si+bx+100h] ;relative base index addressing Mov ah,4ch Int 21h Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/ 1 of 41 2/19/2011 2:32 PM

Upload: manjunatha-siddappa

Post on 31-Mar-2015

374 views

Category:

Documents


17 download

TRANSCRIPT

Page 1: Microprocessor(8086) Lab

ConnexionsYou are here: Home » Content » Microprocessor(8086) Lab

Microprocessor(8086) Lab

Module by: Rajeshwari Hegde.

User rating (?): (1 rating)

Summary: This module contains the programs involving Data transfer instructions, Arithmetic and logicalinstructions, string manipulation instructions etc

Q1. Byte and word move using different addressing modes

Program:

.model small

.data

Num dw 4321h

.code

Mov ax,@data

Mov ds,ax

Mov ax,1234h ;immediate addressing

Mov bx,ax ;register addressing

Mov ax,num ;direct addressing

Mov si,1000h

Mov al,[si] ;indirect addressing

Mov bl,[si+100h] ;relative addressing

Mov bx,1000h

Mov ax,[si+bx] ;base index addressing

Mov cx,[si+bx+100h] ;relative base index addressing

Mov ah,4ch

Int 21h

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

1 of 41 2/19/2011 2:32 PM

Page 2: Microprocessor(8086) Lab

End

Output:

Ax=1234h

Bx=1234h

Ax=4321h…..

2. Program to transfer a block of data without

overlap

.model small

.data

d1 db 1,2,3,4,5

d2 db 10 dup(0)

.code

mov ax,@data ;Initialize the data segment

mov ds,ax

lea si,d1 ;Load offset address of d1 in si

lea di,d2 ;Load offset address of d2 in di

mov cx,05 ;load cx with count

up:mov al,[si] ;Move the 1st element of d1 to al

mov [di],al ;Move to d2

inc si ;Increment si

inc di ;Increment di

dec cx ;decrement the counter

jnz up ;Repeat till cx becomes zero

mov ah,4ch ;Terminate the program

int 21h

end

d1 d2

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

2 of 41 2/19/2011 2:32 PM

Page 3: Microprocessor(8086) Lab

TABLE

1

01

02

03

04

05

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

3 of 41 2/19/2011 2:32 PM

Page 4: Microprocessor(8086) Lab

TABLE

2

01

02

03

04

05

3. Program to transfer a block of data with overlap

.model small

.data

d1 db 1,2,3,4,5

d2 db 10 dup(0)

.code

mov ax,@data ;Initilize the data segment

mov ds,ax

lea si,d1 ;Load the offset address of d1 in si

lea di,d2 ;Load the offset address of d2 in di

mov cx,05h ;load cx with count

up:mov al,[si] ;Move the first element of d1 to al

mov [di],al ;Move to d2

inc si ;Increment si

inc di ;Increment di

dec cx ;decrement the counter

jnz up ;Repeat till cx becomes zero

mov cx,05h ;Load cx with count

lea si,d1+2 ;Load si with offset address of d1+2

lea di,d2 ;Load the offset address of d2 in di

up1:mov al,[di] ;Move the first element of d2 to al

mov [si],al ;Move al to d1

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

4 of 41 2/19/2011 2:32 PM

Page 5: Microprocessor(8086) Lab

INC di ;Increment di

inc si ;Increment si

loop up1 ;Repeat the loop

mov ah,4ch ;Terminate the program

int 21h

end

d1 d2(temp location) (after block transfer)

(with overlap)

TABLE

3

01

02

03

04

05

TABLE

4

01

02

03

04

05

TABLE

5

01

02

01

02

03

04

05

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

5 of 41 2/19/2011 2:32 PM

Page 6: Microprocessor(8086) Lab

4. write an ALP to interchange 2 blocks of data

.model small

.data

d1 db 1,2,3,4,5

d2 db 22h,33h,44h,55h,66h

.code

mov ax,@data ;Initilize the data segment

mov ds,ax

lea si,d1 ;Load offset address of d1 in si

lea di,d2 ;Load offset address of d2 in di

mov cx,05h ;load cx with count

up:mov al,[si] ;Move the first element of d1 to al

mov bl,[di] ;Move the first element of d2 to bl

mov [di],al ;Move al to d2

mov[si],bl ;Move bl to d1

inc si ;Increment si

inc di ;Increment di

dec cx ;decrement the count

jnz up ;If not=0, Jump to label up

mov ah,4ch ;Terminate the program

int 21h

end

TABLE

6

01

02

03

04

05

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

6 of 41 2/19/2011 2:32 PM

Page 7: Microprocessor(8086) Lab

TABLE

7

11

22

33

44

55

d1 d2 d1 d2

TABLE

8

01

02

03

04

05

TABLE

9

11

22

33

44

55

Before block exchange After block exchange

5 . Addition and subtraction of N numbers

Program to add N data words

.model small

.data

n1 dw 1111h,2222h,3333h,4444h,0ffffh

n2 dw 02 dup(0)

.code

mov ax,@data ;Initialize Data segment

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

7 of 41 2/19/2011 2:32 PM

Page 8: Microprocessor(8086) Lab

mov ds,ax

lea si,n1 ;Load si with the offset address of n1

mov ax,00h ;Clear accumulator

mov cx,05h ;Load cx with count

mov dx,00 ;Clear dx

clc ;clear carry

up:add ax,[si] ;Add the first element with accumulator

inc si ;Increment si twice

inc si ;to point to the next data word

dec cx ;Decrement the count

jnz up ;If cx≠0, repeat the loop

jnc down ;else if carry =0, jump to label down

inc dx ;if carry=1, increment dx

down:mov [si],ax ;Store the result in memory pointed to ;by si

mov [si+2],dx ;Store carry in next memory location

mov ah,4ch ;Terminate the program

int 21h

end

I/p:1111h,2222h,3333h,4444h,0ffffh

o/p:1aaa9h

6 . Write an ALP to add/subract 2 32 bit signed/unsigned

numbers.

.model small

.data

d1 dd 0f2222222h

d2 dd 33333333h

d3 dd 10 dup(0)

.code

mov ax,@data ;Initialize the data segment

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

8 of 41 2/19/2011 2:32 PM

Page 9: Microprocessor(8086) Lab

mov ds,ax

mov ax,00h ;Clear ax

lea si,d1 ;Load si with offset address of d1

lea di,d2 ;Load di with offset address of d2

lea bx,d3+5 ;Load bx with offset address of d3+5

mov cx,04h ;Load cx with with the count

mov dl,00h ;clear dl

clc ;clear carry

up:mov al,[si] ;Move the lower byte of Ist number to al

adc al,[di] ;Add with carry the second no.

mov [bx],al ;Store the result in memory pointed to

;by bx

inc si ;Increment si

inc di ;Increment di

dec bx ;decrement bx

dec cx ;Decrement cx

jnz up ;if cx is not equal to zero, repeat ;the loop

jnc down ;If no carry,jmp to label down

inc dl ;else increment carry counter

down:mov [bx],dl ;Move the carry to memory location

mov ah,4ch ;terminate the program

int 21h

end

Input: 0f2222222h and 33333333h( unsigned number addition)

Output:125555555h

6.Program to add/subtract 2 64 unsigned/signed numbers

.model small

.data

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

9 of 41 2/19/2011 2:32 PM

Page 10: Microprocessor(8086) Lab

d1 dq 1234567812345678h

d2 dq 1234567812345678h

res db 09 dup(0)

.code

mov ax,@data ;Initialize data segment

mov ds,ax

clc ;Clear carry

mov cx,08h ;Load cx with count

lea si,d1 ;Load si with offset of d1

lea di,d2 ;Load di with offset of d2

lea bx,res+8 ;Load bx with offset of the result

mov ah,00 ;Mov 00 to ah

up:mov al,[si] ;Mov the LS byte of first no. to al

adc al,[di] ;Add with carry to the LS byte of the ;second number

mov [bx],al ;Move the result to memory pointed by bx

dec bx ;Decrement bx

inc si ;Increment si

inc di ;Increment di

dec cx ;Decrement count

jnz up ;If cx≠0, repeat the loop

jnc exit ;else if carry=0,store 00 in memory

inc ah ;else increment carry

exit:mov [bx],ah ;Store carry in memory location

mov ah,4ch ;Terminate the program

int 21h

end

Input: 1234567812345678h,1234567812345678h

Output:2468ACF02468ACF0H

7 Program to divide a 32 bit number by a 16 bit number

.model small

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

10 of 41 2/19/2011 2:32 PM

Page 11: Microprocessor(8086) Lab

.data

num dd 12345678h

num1 dw 2345h

res dw 02 dup(0)

.code

mov ax,@data ;Initialize data segment

mov ds,ax

lea si,num ;Load si with EA of num

mov dx,[si+2] ;load dx with higher word of dividend

mov ax,[si] ;load ax with lower word

mov bx,num1 ;move the divisor into bx

div bx ;dx:ax/bx

lea di,res ;load di with EA of res

mov [di],dx ;move remainder to memory

mov [di+2],ax ;move quotient to memory

mov ah,4ch ;terminate the program

int 21h

end

Input: dividend-12345678h , divisor-2345h

Output:Quotient:8422h, Remainder:134Eh

8 . To find the Largest/smallest number in an array of 16 bit numbers

.model small

.data

array dw 1111h,2222h,8567h,4589h,5555h

res dw 02 dup(0)

.code

mov ax,@data

mov ds,ax ;Initialize Data segment

lea bx, array ;Load BX with EA of array

mov cx,05h ;cx=number of elements in the array

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

11 of 41 2/19/2011 2:32 PM

Page 12: Microprocessor(8086) Lab

mov ax,[bx] ;move the 1st word of the array to ax

up:cmp ax,[bx+2] ;compare the 1st and 2nd element

jnc below ;if 1st number>2nd number compare 1st with ;3rd number and so on.

;Jc below ;for smallest number

mov ax,[bx+2]

below:inc bx

inc bx

dec cx

jnz up ;if cx≠0, repeat

mov res,ax ;store the largest number in location res

mov ah,4ch ;terminate the program

int 21h

end

Input: 1111h,2222h,8567h,4589h,5555h

Output:8567h stored in location res

9 . Program to sort a given set of 16 bit unsigned integers in ascending/descending order

.model small

.data

n1 dw 0ffffh,5555h,3333h,7777h

count dw 04h

.code

mov ax,@data ;Initialize Data segment

mov ds,ax

mov bx,count ;bx=number of elements to be sorted

dec bx

l2: mov cx,bx ;number of iterations

mov si,00h ;si=0

l1: mov ax,n1[si] ;move the 1st number of n1 to ax

inc si

inc si

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

12 of 41 2/19/2011 2:32 PM

Page 13: Microprocessor(8086) Lab

cmp ax,n1[si] ;compare with the next number

jc rep1 ;if 1st no.>2nd no, no exchange

;jnc rep1 ;jnc for descending order.

xchg ax,n1[si] ;else exchange 1st and 2nd no

mov n1[si-2],ax

rep1:loop l1 ;repeat till cx=0

dec bx

jnz l2 ;all the iterations over?

mov ah,4ch

int 21h

end

Input: 0ffffh, 5555h, 3333h, 7777h

Output: 3333h, 5555h, 7777h, 0ffffh,

10 . Program to search a character in a given string

.model small

.data

string db "bmscollege$"

enter db "j"

msg db 0ah,0dh,"character found$"

msg1 db 0ah,0dh,"charcter not found$"

char db 02 dup(0)

.code

mov ax,@data ;initialize DS and ES

mov ds,ax

mov es,ax

mov cl,0ah ;CL=count

lea si,string ;Load the EA of string into si

up:mov al,[si] ;mov the first character to al

cmp al,enter ;compare with the character to be ;searched

jz found ;jump to found

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

13 of 41 2/19/2011 2:32 PM

Page 14: Microprocessor(8086) Lab

inc si ;else repeat the loop

dec cl

jnz up

nfound:lea dx,msg1 ;display, char not found

mov ah,09h

int 21h

jmp end1

found:lea dx,msg ;display, char found

mov ah,09h

int 21h

end1:mov ah,4ch ;terminate

int 21h

end

Output: character not found

11 Program to find the factorial of a given number(no<=8)

.model small

.data

no dw 08h

res dw 02 dup(0)

.code

mov ax,@data ;initialize DS

mov ds,ax

mov ax,no ;move the number to ax and bx

mov bx,no

cmp ax,02h ;compare with 02h

jz down ;if no=2,factorial=2,jump to down

up:dec bx ;else decrement bx

mul bx ;multiply ax and bx

cmp bx,01h ;compare bx with 01

jz down ;if 0, store the result

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

14 of 41 2/19/2011 2:32 PM

Page 15: Microprocessor(8086) Lab

jmp up

down:mov res,dx

mov res+2,ax

mov ah,4ch ;end of the program

int 21h

end

I/P=08h

O/P=9D80h

12. Program to check whether the given data byte is positive or negative

.model small

.data

n1 db 09H

n2 db 0ah,0dh,"the data is positive $"

n3 db 0ah,0dh,"the data is negative $"

.code

mov ax,@data ;Initialize data segment

mov ds,ax

clc ;Clear carry

mov al,n1

and al,10h ;Check the MSB

jz d1 ;If 0, display the no is positive

mov dx,offset n3

mov ah,09h

int 21h

jmp end1

d1:mov dx,offset n2 ;Else display the no is negative

mov ah,09h

int 21h

end1:mov ah,4ch

int 21h

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

15 of 41 2/19/2011 2:32 PM

Page 16: Microprocessor(8086) Lab

end

Input:09h

Output:the data is positive

Input:0ffh

Output:the data is negative

13. Program to check whether data byte is odd or even

.model small

.data

n1 db 0feH

n2 db 0ah,0dh,"the given data is odd $"

n3 db 0ah,0dh,"the given data is even$"

n4 db 04 dup(0)

.code

mov ax,@data ;Initialize data segment

mov ds,ax

mov ah,00h

mov bl,02h

mov al,n1 ;move the number to al

div bl ;divide by 2

cmp ah,00h ;compare the remainder with 0.

jz down ;if 0, the number is positive

lea dx,n2

mov ah,09h ;Display “the given …odd”

int 21h

jmp end1 ;jump to end1

down:lea dx,n3 ;display” the given …even”

mov ah,09h

int 21h

end1:mov ah,4ch ;end of the program

int 21h

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

16 of 41 2/19/2011 2:32 PM

Page 17: Microprocessor(8086) Lab

end

Input:0feh

Output displayed on the screen:“the given data is even”

Input:09h

Output displayed on the screen:”the given data is odd”

14.Program to check whether the given word is a bitwise palindrome

.model small

.data

msg1 db 0ah,0dh,'it is a palindrome$'

msg2 db 0ah,0dh,'it is not a palindrome$'

num dw 4421h

.code

mov ax,@data ;Initialize data segment

mov ds,ax

mov ax,num ;Move number to ax

mov cl,10h ;Load cx with the count

mov bx,00 ;Move 00 to bx

clc ;Clear carry

back:rol ax,01 ;Rotate left ax once, through carry

rcr bx,01 ;Rotate bx once without carry

dec cl ;Decrement the count

jnz back ;If cl≠0, repeat the loop

cmp ax,bx ;if cl=0, compare ax and bx

jz pali ;If ax=bx, jump to label pali

mov ah,09h

lea dx,msg2 ;Display “it is not a palindrome”

int 21h

jmp last

pali:mov ah,09h

lea dx,msg1 ;Display ”it is a palindrome”

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

17 of 41 2/19/2011 2:32 PM

Page 18: Microprocessor(8086) Lab

int 21h

last:mov ah,4ch ;Terminate the program

int 21h

end

15. Program to check whether the given data word is a nibble wise palindrome

.model small

.data

num dw 8448h

msg1 db 0ah,0dh,"it is a palindrome$"

msg2 db 0ah,0dh,"it is not a palindrome$"

.code

mov ax,@data ; Initialize data segment

mov ds,ax

mov ax,num ;Move number to ax

mov cl,04 ;Move 04 to cl

mov bx,ax ;Move ax to bx

clc ;Clear carry

up:ror bl,01 ; Rotate right bl once, through carry

dec cl ;Decrement cl

jnz up ;Repeat the loop if cl≠0,

cmp bh,bl ;if cl=0, compare bh with bl

jz pali ;If bh=bl, jump to label pali

mov ah,09h

lea dx,msg2

int 21h

jmp end1 ;Jump to label end1

pali:mov ah,09h ;Display “it is a palindrome”

lea dx,msg1

int 21h

end1:mov ah,4ch ;Terminate the program

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

18 of 41 2/19/2011 2:32 PM

Page 19: Microprocessor(8086) Lab

int 21h

end

16. Program to add 2 ASCII numbers

.model small

.data

;N1 db ‘9’

;N2 db ‘9’

N1 db ‘5’

N2 db ‘8’

N3 db 02 dup(0)

.code

Mov ax,@data ;Initialize data segment

Mov ds,ax

Mov al,n1 ;move the first no to al

Mov bl,n2 ;second number to bl

Mov ah,00h ;clear ah

Add al,bl ; add al and bl

Aaa ;ASCII adjust after addition

Or ax,3030h ;Or ax with 3030h for display

Push ax

Mov dl,ah ;display the content of ah

Mov ah,02h

Int 21h

Pop ax

Mov dl,al ;display the content of al

Mov ah,02h

Int 21h

Mov ah,4ch ;end of the program

Int 21h

End

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

19 of 41 2/19/2011 2:32 PM

Page 20: Microprocessor(8086) Lab

Input : N1=’9’

N2=’9’

Output: 18

Input: N1 = ‘5’

N2 = ‘8’

Output: 13

17. Program to convert a hex number to ascii number

.model small

.data

num db 35h

;num db 0AAh

res db 2 dup(0)

.code

Mov ax,@data

Mov ds,ax ; Initialize data segment

Mov al,num ;move the number to al

And al,0f0h ;mask the lower nibble of the digit

Mov cl,04h

Rol al,cl ;bring the digit to LSB position

Cmp al,0ah ;if the number is < 0ah,jump to D1

Jc d1

Add al,07h ;if the number is>0ah, add 37h

D1:add al,30h

Lea si,res ;store the result in memory

Mov [si],al

Mov al,num

And al,0fh ;mask the upper nibble

Cmp al,0ah ;compare with 0ah

Jc d2 ;if the number is<0ah, jump to D2

Add al,07h ;if the number is >0ah, add 37h

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

20 of 41 2/19/2011 2:32 PM

Page 21: Microprocessor(8086) Lab

D2:add al,30h

Mov [si+1],al ;store the result in memory

Mov ah,4ch ;end of the program

Int 21h

End

Input:35h

Output:33h and 35h, stored in memory locations [si] &

[si+1]

Input:0AAh

Output: 41h and 41h stored in memory locations [si] &

[si+1]

18. Program to convert an 8 bit BCD number to binary number

.model small

.data

D1 db 99h

;D1 db 85h

D2 db 02 dup(0)

.code

Mov ax,@data

Mov ds,ax ; Initialize data segment

Mov al,D1 ;move the BCD number to al

And al,0f0h ;mask the lower nibble

Mov cl,04h

Rol al,cl ; bring the upper nibble to LS position

Mov bl,al

Mov al,0ah

Mul bl ;multiply with 0ah

Mov bl,al ;store the result in bl

Mov al,D1 ;get the BCD number to al

And al,0fh ;mask the upper nibble

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

21 of 41 2/19/2011 2:32 PM

Page 22: Microprocessor(8086) Lab

Add al,bl ;add al and bl

Mov D2,al ;store the binary no in D2

Mov ah,4ch ;end of the program

Int 21h

End

Input : 99

Output: 63h

Input : 85

Output: 55h

19.Program to convert an 8 bit binary number to BCD number

.model small

.data

num db 0ffh

;num db 63h

res db 03 dup(0)

.code

Mov ax,@data

Mov ds,ax ; Initialize data segment

Mov al,num ;move the binary number to al

Mov ah,00h ;clear ah

Mov bl,64h

div bl ;divide ax by 64h

Lea si,res

Mov [si],al ;move the quotient to [si]

Mov al,ah ;move the remainder to al

mov ah,0h ;clear ah

mov bl,0ah

div bl ;divide ax by bl

mov [si+1],al ;move the quotient to [si+1]

mov [si+2],ah ;move the remainder to [si+2]

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

22 of 41 2/19/2011 2:32 PM

Page 23: Microprocessor(8086) Lab

mov ah,4ch ;end of the program

int 21h

end

Input :0ffh

Output:02 05 05 stored in locations [si,si+1,si+2]

Input :063h

Output:09 09 stored in locations [si+1, si+2]

20. Program to check whether the code belongs to 2/5 code

.model small

.data

n1 db 0ah,0dh,"the code is 2 out of 5 code$"

n2 db 0ah,0dh,"the code is not a 2 out of 5 code$"

n3 db 0fh

.code

mov ax,@data

mov ds,ax ;initialize the data segment

mov al,n3 ;move the number to al

mov dl,00h

and al,0e0h ;and it with 0eh

jnz invalid ;if 1st three MSBs are not zero jump to ;invalid

mov bl,05h

mov al, n3 ;if 1st three MSBs are non-zero, move the ;number to al

up:ror al,01h ;check for number of ones in remaining 5 ;bits

jnc d1 ;check for carry

inc dl ;increment dl if carry=1

d1:dec bl ;decrement the count

jnz up ;if bl≠0, repeat

cmp dl,02h ;compare dl with 02.

jnz invalid ;if dl≠0, jump to invalid

lea dx,n1 ;display ”the code is 2 out of 5 code”

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

23 of 41 2/19/2011 2:32 PM

Page 24: Microprocessor(8086) Lab

mov ah,09h

int 21h

jmp end1 ;jump to end1

invalid:lea dx,n2; display “the code is not a

mov ah,09h ; 2 out of 5 code

int 21h

end1:mov ah,4ch ;end of the program

int 21h

end

Input:0fh

Output: the code is not a 2 out of 5 code

Input:09h

Output: the code is a 2 out of 5 code

21. Program to find the LCM of two data bytes

.stack 64

.data

;num dw 05h,02h

num dw 09h,2dh

lcm dw 2 dup(0)

.code

mov ax,@data

mov ds,ax ;Initiliaze data segment

mov ax,num ;move the 1st number to ax

mov bx,num+2 ;and 2nd number to bx

mov dx,0 ;clear dx

cmp ax,bx ;compare ax and bx

jnc again ;if ax>bx, jump to again

xchg ax,bx ;otherwise exchange ax and bx

mov num,ax

mov num+2,bx

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

24 of 41 2/19/2011 2:32 PM

Page 25: Microprocessor(8086) Lab

again: push ax ;push ax and dx onto the stack

push dx

div bx ;divide ax by bx

cmp dx,00h ;compare the remainder with 0

je store

pop dx

pop ax

add ax,num

jnc no_increment

inc dx

no_increment: jmp again

store: pop lcm+2

pop lcm

mov ah,4ch

int 21h

end

Input:05h, 02h

Output:0ah

Input:

22. Program to find the GCD of two numbers

.model small

.data

num dw 1bh,09h

Gcd dw ?

.code

Mov ax,@data

Mov ds,ax ;Initiliaze data segment

Mov ax,num ;move the 1st number to ax

Mov bx,num+2 ;and 2nd number to bx

Again:cmp ax,bx ;

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

25 of 41 2/19/2011 2:32 PM

Page 26: Microprocessor(8086) Lab

Je exit

Jb down

Divaxbx:mov dx,0

Div bx

Cmp dx,0

Je exit

Mov ax,dx

Jmp again

Down:xchg ax,bx

Jmp divaxbx

Exit:mov gcd,bx

Mov gcd+2,ax

Mov ah,4ch

Int 21h

End

23. Program to divide a packed bcd number by an unpacked bcd number and display the quotient

and remainder on the screen

.model small

.data

N1 db 6

N2 db 7

N3 db 02 dup(0)

.code

Mov ax,@data

Mov ds,ax

Mov ah,n1

Mov al,n2

mov ch,09h

Aad

div ch

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

26 of 41 2/19/2011 2:32 PM

Page 27: Microprocessor(8086) Lab

Or ax,3030h

Push ax

Mov dl,ah

Mov ah,02h

Int 21h

mov dl, " "

mov ah,02h

int 21h

pop ax

Mov dl,al

Mov ah,02h

Int 21h

Mov ah,4ch

Int 21h

End

Input:6 and 7

Output: Quotient 7, Remainder 4 displayed on the screen

24.Program to multiply two unpacked BCD numbers and display the result

.model small

.data

N1 db 9

N2 db 5

N3 db 02 dup(0)

.code

Mov ax,@data

Mov ds,ax

Mov al,n1

Mov bl,n2

mul bl

Aam

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

27 of 41 2/19/2011 2:32 PM

Page 28: Microprocessor(8086) Lab

Or ax,3030h

Push ax

Mov dl,ah

Mov ah,02h

Int 21h

pop ax

Mov dl,al

Mov ah,02h

Int 21h

Mov ah,4ch

Int 21h

End

Input: N1=9, N2=5

Output:45 displayed on the screen

2 5.program to multiply two 32 bit unsigned numbers

.model small

.data

num1 dd 0ff0aff0h

num2 dd 03304002h

res dq 00h

.stack 100h

.code

Mov ax,@data

Mov ds,ax

Lea si,num1

Lea di,num2

Lea bx,res

clc

mov ax,[si]

mul word ptr[di]

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

28 of 41 2/19/2011 2:32 PM

Page 29: Microprocessor(8086) Lab

mov[bx],ax

mov cx,dx

mov ax[si+2]

mul word ptr[di]

add ax,cx

adc dx,00h

push dx

push ax

mov ax,[si]

mul word ptr[di+2]

pop cx

add ax,cx

adc dx,00h

mov[bx+2],ax

mov cx,dx

mov ax,[si+2]

mul word ptr[di+2]

add ax,cx

adc dx,00h

pop cx

add ax,cx

adc dx,00h

mov[bx+4],ax

mov[bx+6],dx

mov ah,4ch

int 21h

end

Input:

Num1:0ff0aff0h

Num2:03304002h

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

29 of 41 2/19/2011 2:32 PM

Page 30: Microprocessor(8086) Lab

Output:32d32d18dd5fe0h

26.Program to find the square of a given data byte

.model small

.data

num db 0ffh

res db 04 dup(0)

.code

mov ax,@data

mov ds,ax

mov ah,00h

mov al,num

mov bl,al

mul bl

lea si,res

mov[si],ah

mov[si+1],al

mov ch,02

up:mov dl,[si]

and dl,0f0h

mov cl,04h

ror dl,cl

cmp dl,0ah

jb down

add dl,07h

down:add dl,30h

mov ah,02h

int 21h

mov dl,[si]

and dl,0fh

cmp dl,0ah

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

30 of 41 2/19/2011 2:32 PM

Page 31: Microprocessor(8086) Lab

jc down1

add dl,07h

down1:add dl,30h

mov ah,02h

int 21h

inc si

dec ch

jnz up

mov ah,4ch

int 21h

end

Input:04h

Output:10h is displayed on the screen

Input:0ffh

Output:FE01h is displayed on the screen

27 . Program to find the Cube of a given number

.model small

.data

num dw 05h

;num dw 0ffh

cube dw 02 dup(0)

.code

Mov ax,@data

Mov ds,ax

Mov dx,0h

Mov ax,num

Mul num

Mov cx,dx

Mul num

Add ax,cx

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

31 of 41 2/19/2011 2:32 PM

Page 32: Microprocessor(8086) Lab

Mov cube,ax

Jnc skip

Inc dx

Skip:mov cube+2,dx

Mov ah,4ch

Int 21h

End

Input:0ffh

Output:fd02ffh in locations cube and cube+2

Input:05h

Output:7d in locations cube and cube+2

28. Program to find number of ones in a given data word and display the count on the screen

.model small

.data

n1 dw 0fff0h

n3 db 04 dup(0)

mes1 db 0ah,0dh,"no of 1's$"

.code

mov ax,@data

mov ds,ax

mov bx,00h

lea si,n3

clc

mov cl,10h

mov ax,n1

up:rcl ax,01

jnc d1

inc bl

d1:mov [si],bl

dec cx

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

32 of 41 2/19/2011 2:32 PM

Page 33: Microprocessor(8086) Lab

jnz up

mov dl,[si]

and dl,0f0h

mov cl,04h

ror dl,cl

cmp dl,0ah

jc d11

add dl,07h

d11:add dl,30h

mov ah,02h

int 21h

mov dl,[si]

and dl,0fh

cmp dl,0ah

jc d12

add dl,07h

d12:add dl,30h

mov ah,02h

int 21h

mov ah,4ch

int 21h

end

Input:0fff0h

Output:0c displayed on the screen

29.ascii multiplication

.model small

.data

N1 db 6

N2 db 7

N3 db 02 dup(0)

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

33 of 41 2/19/2011 2:32 PM

Page 34: Microprocessor(8086) Lab

.code

Mov ax,@data

Mov ds,ax

Mov ah,n1

Mov al,n2

mov ch,09h

;Mov ah,00h

Aad

div ch

Or ax,3030h

Push ax

Mov dl,ah

Mov ah,02h

Int 21h

pop ax

Mov dl,al

Mov ah,02h

Int 21h

Mov ah,4ch

Int 21h

End

2 9 . Program to reverse the string

.model small

.data

mes1 db 0ah,0dh,"enter the string",0ah,0dh,"$"

blank db " ", 0ah,0dh,"$"

string db 18 dup(0)

.code

mov ax,@data

mov ds,ax

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

34 of 41 2/19/2011 2:32 PM

Page 35: Microprocessor(8086) Lab

mov es,ax

mov dx,offset mes1

mov ah,09h

int 21h

mov si,offset string

add si,14h

mov dl,24h

mov [si],dl

cops:mov ah,01h

int 21h

cmp al,0dh

jz rev

dec si

mov [si],al

jmp cops

rev:mov dx,offset blank

mov ah,09h

int 21h

mov ah,09h

mov dx,si

int 21h

mov ah,4ch

int 21h

end

30.Program to transfer the string

.model small

.data

src db 'string transfer$'

dst db 25 dup(0)

.code

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

35 of 41 2/19/2011 2:32 PM

Page 36: Microprocessor(8086) Lab

mov ax,@data

mov ds,ax

mov es,ax

lea si,src

lea di,dst

cld

mov cx,0fh

rep movsb

mov ah,4ch

int 21h

end

31. program to search a sub string in a main string

data segment

mainstr db 80 dup(0)

subst db 80 dup(0)

mes1 db "enter the string",0ah,0dh,"$"

mes2 db "enter the string to be searched",0ah,0dh,"$"

sfound db "string found$"

nfound db "string not found$"

null equ 0dh

cr db 0ah,0dh,"$"

data ends

code segment

assume cs:code,ds:data

start:mov ax,data

mov ds,ax

mov es,ax

mov ah,09h

lea dx,mes1

int 21h

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

36 of 41 2/19/2011 2:32 PM

Page 37: Microprocessor(8086) Lab

mov si,00

up:mov ah,01h

int 21h

cmp al,0dh

jz acc

mov mainstr[si],al

inc si

jmp up

acc:mov mainstr[si],al

lea dx,cr

mov ah,09h

int 21h

lea dx,mes2

int 21h

mov di,00

t3:mov ah,01h

int 21h

cmp al,0dh

jz out

mov subst[di],al

inc di

jmp t3

out:mov subst[di],al

lea dx,cr

mov ah,09h

int 21h

mov si,00

mov di,00

again:mov al,mainstr[si]

cmp al,null

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

37 of 41 2/19/2011 2:32 PM

Page 38: Microprocessor(8086) Lab

jz t2

cmp al,subst[di]

jz down

mov di,00

inc si

jmp again

down:inc si

inc di

mov al,subst[di]

cmp al,null

jz found

jmp again

found:mov ah,09h

lea dx,sfound

int 21h

jmp quit

t2:mov ah,09h

lea dx,nfound

int 21h

quit:mov ah,4ch

int 21h

code ends

end start

Output on the screen

Enter the string: bmscollege

Enter the string to be searched: college

Output: string found

3 2. Program to multiply two data words and display the answer by calling a far procedure

data segment

;mul1 db 12h

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

38 of 41 2/19/2011 2:32 PM

Page 39: Microprocessor(8086) Lab

;mul2 db 24h

mul1 db 0ffh

mul2 db 0ffh

data ends

stack_seg segment

dw 40 dup(0)

tps label word

stack_seg ends

sbrt segment public

extrn disp:far

sbrt ends

code segment

assume cs:code,ds:data

start:mov ax,data

mov ds,ax

mov sp,offset tps

mov al,mul1

mul mul2

mov cx,ax

mov al,ah

call disp

mov al,cl

call disp

mov ah,4ch

int 21h

code ends

end start

Far procedure to display the number on the screen

public disp

sbrt segment public

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

39 of 41 2/19/2011 2:32 PM

Page 40: Microprocessor(8086) Lab

disp proc far

assume cs:sbrt

push ax

push dx

push cx

mov dl,al

and dl,0f0h

mov cl,04h

ror dl,cl

cmp dl,0ah

jb down

add dl,07h

down:add dl,30h

push ax

mov ah,02h

int 21h

pop ax

mov dl,al

and dl,0fh

cmp dl,0ah

jc down1

add dl,07h

down1:add dl,30h

push ax

mov ah,02h

int 21h

pop ax

pop cx

pop dx

pop ax

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

40 of 41 2/19/2011 2:32 PM

Page 41: Microprocessor(8086) Lab

ret

disp endp

sbrt ends

end

Input:12h 24h

Output:0288h, displayed on the screen

More about this module: Metadata | Downloads | Version History

How to reuse and attribute this contentHow to cite and attribute this content

This work is licensed by Rajeshwari Hegde under a Creative Commons Attribution License (CC-BY 3.0), and is an Open Educational Resource.

Last edited by Rajeshwari Hegde on Nov 16, 2009 11:22 pm US/Central.

Microprocessor(8086) Lab http://cnx.org/content/m32857/latest/

41 of 41 2/19/2011 2:32 PM