Q1. Byte and Word Move Using Different Addressing Modes
Q1. Byte and Word Move Using Different Addressing Modes
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
End
Output:
Ax=1234h
Bx=1234h
Ax=4321h…..
.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
01 01
02 02
03 03
04 04
05 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
INC di ;Increment di
inc si ;Increment si
loop up1 ;Repeat the loop
mov ah,4ch ;Terminate the program
int 21h
end
.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
d1 d2 d1 d2
01 11 11 01
02 22 22 02
03 33 33 03
04 44 44 04
05 55 55 05
.model small
.data
n1 dw 1111h,2222h,3333h,4444h,0ffffh
n2 dw 02 dup(0)
.code
mov ax,@data ;Initialize Data segment
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
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
.model small
.data
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
.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
.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
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 cx0, 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
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
.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
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
.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
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
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
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”
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
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
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
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
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]
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”
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
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
.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
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
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
.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]
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
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 ;Initialize data segment
mov ds,ax
mov ah,00h ;Clear ah
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
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
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
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)
.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
29.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
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
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
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
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
data segment
;mul1 db 12h
;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
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
ret
disp endp
sbrt ends
end
Input:12h 24h
Output:0288h, displayed on the screen
INTERFACING EXPERIMENTS
Keyboard Interface
24 keys are arranged in a 3x8 matrix fashion. The row lines are driven by pc0,pc1,pc2. The
column lines are read through port A. When no key is pressed, all the return lines are low. The
rows are driven high one after another in sequence. When a row is driven high, pressing a key in
that row causes the corresponding return lines to be read as high. Then it scans for the column
for which the key is pressed. The row and column position can be used to encode the key.
.model small
.data
res db ?
.code
mov ax,@data
mov ds,ax
mov dx,123h
mov al,90h
out dx,al
start:mov ah,00h
mov cx,03h
mov bl,01h
repeat:mov al,bl
mov dx,122h
out dx,al
mov dx,120h
in al,dx
cmp al,00h
jnz key
add ah,08h
rol bl,01h
loop repeat
jmp start
key:ror al,01h
jc store
inc ah
jmp key
;hex to ASCII conversion
;to dispaly the code of the key pressed on the screen
store: mov res,ah
mov dl,ah
mov cl,04h
and dl,0f0h
ror dl,cl
cmp dl,0ah
jc d11
add dl,07h
d11: add dl,30h
mov ah,02h
int 21h
mov dl,res
and dl,0fh
cmp dl,0ah
jc d2
add dl,07h
d2: add dl,30h
mov ah,02h
int 21h
mov ah,4ch
int 21h
end
2.Program to scan the keyboard for key closure and store
the code of the key pressed in a memory location/display on
the screen display row no and column no on the screen
.model small
.data
res db ?
.code
mov ax,@data
mov ds,ax
mov dx,123h
mov al,90h
out dx,al
start:mov ah,00h
mov cx,03h
mov bl,01h
repeat:mov al,bl
mov dx,122h
out dx,al
mov dx,120h
in al,dx
cmp al,00h
jnz key
add ah,08h
rol bl,01h
loop repeat
jmp start
key: ror al,01h
jc store
inc ah
jmp key
store: mov res,ah
mov dl,res
call disp
mov dl," "
mov ah,02h
int 21h
mov ah,res
cmp ah,10h
jc loop1
sub ah,10h
jmp loop2
loop1: cmp ah,08h
jc loop2
sub ah,08h
loop2:inc ah
mov dl,ah
mov res,ah
call disp
mov dl, " "
mov ah,02h
int 21h
cmp bl,04h
jne down
dec bl
down: mov res,bl
mov dl,bl
call disp
jmp end1
disp proc
mov cl,04h
and dl,0f0h
ror dl,cl
cmp dl,0ah
jc d11
add dl,07h
d11: add dl,30h
mov ah,02h
int 21h
mov dl,res
and dl,0fh
cmp dl,0ah
jc d2
add dl,07h
d2: add dl,30h
mov ah,02h
int 21h
ret
disp endp
end1: mov ah,4ch
int 21h
end
Seven segment display interface
This interface provides 4 digit 7 seven segment display by the output of 4 cascaded SIPO shift
registers. Data to be displayed is transmitted serially( bit by bit) to the interface over the port
line PB0. Each bit is clocked into the shift registers by providing a common clock through port
line PC0. Seven segment device used is common anode type. Hence low input must be given to
each seven segment to glow and high to blank.
3.program to implement constant display on seven segment
display
.model small
.data
code1 db 099h,0b0h,0a4h,0f9h
count db 0fh
.code
mov ax,@data
mov ds,ax
lea si, code1
mov al,80h
mov dx,123h
out dx,al
mov cx,04h
next1:mov bl,08h
mov al,[si]
next:rol al,01h
mov dx,121h
out dx,al
push ax
mov al,0ffh
mov dx,122h
out dx,al
mov al,00h
out dx,al
pop ax
dec bl
jnz next
inc si
loop next1
mov ah,4ch
int 21h
end
4.program to implement rolling display on seven segment
display
.model small
.data
code1 db 099h,0b0h,0a4h,0f9h,0ffh,0ffh,0ffh,0ffh
count db 0fh
.code
mov ax,@data
mov ds,ax
mov al,80h
mov dx,123h
out dx,al
start:lea si,code1
call disp
dec count
jnz start
mov ah,4ch
int 21h
.model small
.code
mov ax,@data
mov ds,ax
mov dx,123h
mov al,80h
out dx,al
mov dx,120h
mov al,88h
mov cx,0ffh
rotate: out dx,al
push cx
call delay
ror al,01h ;rol for anticlockwise
pop cx
loop rotate
mov ah,4ch
int 21h
delay proc
mov cx,60h ;cx=20h.. to vary the speed
outer: mov bx,0ffffh
inner: dec bx
jnz inner
loop outer
ret
delay endp
end
Program to implement half adder using logic controller
.model small
Sum db 0,1,1,2
.code
Mov ax,@data
Mov ds,ax
Mov dx,123h
Mov al,82h
Out dx,al
Mov dx,121h
Lea bx,sum
In al,dx
And al,03h
xlat
Mov dx,120h
Out dx,al
mov ah,4ch
Int 21h
End
Input Output
PB1 PB0 PA1(c) PA0(s)
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0
Program to implement full adder using logic controller
.model small
Sum db 0,1,1,2,1,1,1,3
.code
Mov ax,@data
Mov ds,ax
Mov dx,123h
Mov al,82h
Out dx,al
Mov dx,121h
Lea bx,sum
In al,dx
And al,07h ;lower 3 bits of PB are used as inputs
xlat
Mov dx,120h
Out dx,al ;lower 2 bits of PA are used as outputs
mov ah,4ch
Int 21h
End
Details of the assembler directives used
These are nothing but the commands to the assembler. These are not instructions to the 8086
microprocessor. Directive directs the assembler, reserves the memory location or reserves and
pre assigns the memory locations according to the direction. Directives are nothing but the
pseudo mnemonics. Different directives are required to perform different tasks. Assembler
directives enable us to control the way the program is assembled and listed. They do not generate
any machine code for execution.
.MODEL directive: this directive helps in providing shortcuts while defining segments. To use
this directive, we have to initialize the memory model before defining any segment. The format is
‘.model memory model’. The memory model can be SMALL, MEDIUM, COMPACT or
LARGE. The small memory model is used if the program is less than 64kbytes.
DB directive: Define byte directive: The DB directive is used to define a variable or to set aside
one or more storage locations of type byte in memory.
DW directive: Define word directive: The DD directive is used to define a variable of type double
word or to reserve memory locations which can be accessed as type double word.
END directive: The END directive is put after the last statement of a program to tell that this is
the End of the program module.
ENDP directive: This directive is used along with the name of the procedure to indicate the end
of a procedure to the assembler. This directive together with the procedure directive, ‘PROC’ is
used to ‘bracket’ a procedure.
PROC directive: The start of the procedure is indicated by the PROC directive. The general
format is
PROC_NAME PROC[DIST]
Where dist is optional and can be either NEAR of FAR. The default value for dist is NEAR. For
a procedure that is in the same segment as the calling program, we use the NEAR option.
.STACK directive: This is a simplified segment directive to define the stack segment.
The general format is
.STACK[size]
the default stack size is 1024 bytes.
.STACK 64 results in reserving 64 bytes fro the stack operations.