Beruflich Dokumente
Kultur Dokumente
Contents
1. Part A
2. Part B
1. Search a key element in a list of n numbers using the Binary Search algorithm
assume cs:code,ds:data
data segment
a db 10h,20h,30h,40h,50h ; Sorting only bytes. Try for words also.
n db n-a
key db 20h
msg1 db "key not found$"
msg2 db "key found at position: "
pos db ?,"$"
; msg2 continues till here!!!
data ends
code segment
start:
mov ax,data
mov ds,ax
mov al,0
mov dl,n
dec dl
again:
; low
; high
Page 1 of 20
Microprocessors Lab
cmp al,dl
ja failed
mov cl,al
add al,dl
shr al,1
mov ah,00h
mov si,ax
mov bl,[si]
cmp bl,key
jae loc1
inc al
jmp again
; mid
; [mid] in bl
; [mid] >= key ?
; no, low = mid+1, to search in second half
loc1:
je success
dec al
mov dl,al
mov al,cl
jmp again
failed:
lea dx,msg1
jmp display
success:
inc al
add al,30h
mov pos,al
lea dx,msg2
; [mid] = key ?
; no, high = mid 1, to search in first half
display:
mov ah,9
int 21h
mov ah,4ch
int 21h
code ends
end start
2) Sort a given set of n numbers in ascending and descending order using the Bubble Sort
algorithm
Assume cs:code,ds:data
data segment
x db 10h,05h,03h,15h,01h
n dw n-x
data ends
code segment
start:
Page 2 of 20
Microprocessors Lab
mov ax,data
mov ds,ax
mov bx,n
dec bx
next_pass:
mov cx,bx
mov ah,00
lea si,x
next_comp:
mov al,[si]
cmp al,[si+1]
jle do_nothing
xchg al,[si+1]
mov [si],al
mov ah,1
do_nothing:
inc si
loop next_comp
cmp ah, 0
je finish
dec bx
jnz next_pass
finish:
mov ah,4ch
int 21h
code ends
end start
assume cs:code,ds:data
data segment
str1 db 'madam'
n dw n str
str2 db 5 dup(?)
msg1 db "pallindrome$"
msg2 db "not a palindrome$"
data ends
code segment
start:
mov ax,data
mov ds,ax
mov es,ax
; please observe this !!!!!!
mov cx,n
by - 4th sem students of Information Science and Engineering Feb 2009
Page 3 of 20
Microprocessors Lab
Mohan Kumar S Dept. of ISE
lea si,n
; a simple trick to make si pointing to last character of main string
dec si
lea di,str2
nextchar:mov al,[si]
mov [di],al
dec si
inc di
loop nextchar
lea si,str1
lea di,str2
cld
; so that si and di will be incremented
mov cx,n
rep cmpsb
jnz unsuccess
lea dx,msg1
jmp disp
unsuccess:lea dx,msg2
disp:mov ah,9h
int 21h
mov ah,07h
int 21h
code ends
end start
4) Read two strings, store them in locations STR1 and STR2 . Check whether they are equal or
not and display appropriated messages. Also display the length of the stored strings.
disp macro msg
lea dx,msg
mov ah,09h
int 21h
endm
assume cs:code,ds:data
data segment
str1 db 100 dup(?)
str2 db 100 dup(?)
msg1 db "Enter 1st string : $"
msg2 db 0ah,0dh,"The length of 1st string is : "
by - 4th sem students of Information Science and Engineering Feb 2009
Page 4 of 20
Microprocessors Lab
len1 db ?,"$"
; to hold length
disp msg3
lea si,str2
mov cl,00h
next_char1:
mov ah,01h
int 21h
cmp al,0dh
je disp_length
mov [si],al
inc si
inc cl
jmp next_char1
by - 4th sem students of Information Science and Engineering Feb 2009
Page 5 of 20
Microprocessors Lab
disp_length:
add cl,30h
mov len2,cl
disp msg2
disp msg4
cmp len1,cl
jne not_equal
lea si,str1
lea di,str2
cld
sub cl,30h
rep cmpsb
jnz not_equal
disp msg5
jmp exit
not_equal:
disp msg6
exit: mov ah,4ch
int 21h
code ends
end start
5) Compute the factorial of a positive integer n using recursive procedure.
assume cs:code,ds:data
data segment
n db 5
res db ?
data ends
code segment
start:
mov ax,data
mov ds,ax
mov al,n
call fact
mov ah,4ch
int 21h
fact proc
cmp al,00
je cal
push ax
dec al
call fact
by - 4th sem students of Information Science and Engineering Feb 2009
Page 6 of 20
Microprocessors Lab
Mohan Kumar S Dept. of ISE
pop ax
; after first return ie. from cal label, control comes here not to main program
mul res
mov res,al
ret
cal: mov res,01
ret
fact endp
code ends
end start
6) Compute nCr using recursive procedure. Assume that n and r are non-negative integers.
Recurcive procedure: if r = 0 and r = n then nCr = 1
Else if r = 1 and r = n 1, then nCr = n
Else nCr = n-1Cr + n-1Cr-1
assume cs:code,ds:data
data segment
n db 5
r db 5
res db ?
data ends
code segment
start:
mov ax,data
mov ds,ax
mov al,n
mov bl,r
call encear
mov ah,4ch
int 21h
encear proc
cmp bl,00
je ncr_1
cmp bl,al
je ncr_1
cmp bl,01
je ncr_n
by - 4th sem students of Information Science and Engineering Feb 2009
Page 7 of 20
Microprocessors Lab
dec al
cmp bl,al
je ncr_n_1
push ax
push bx
call encear
pop bx
pop ax
dec bl
push ax
push bx
call encear
pop bx
pop ax
ret
ncr_1:
mov res,01
ret
ncr_n_1:
inc al
ncr_n:
add res,al
ret
encear endp
code ends
end start
7) Generate the first n Fibonacci numbers
Assume cs:code,ds:data
Data segment
Fibo db 10 dup(?)
N db 0ah
Data ends
Code segment
Start:
mov ax, data
mov ds, ax
lea si, fibo
th
Page 8 of 20
Microprocessors Lab
mov al, 00h ; fib(n-2)
mov [si], al
inc si
mov bl, 01h ; fib(n-1)
mov [si], bl
inc si
mov cl, n
sub cl,2
; already two numbers generated and stored
mov ch, 00
; so that I can use loop instruction
next:number:
add al, bl
; fib(n) = fib(n-1) + fib(n-2)
mov [si], al
in si
xchg al, bl
; al suppose to contain fib(n-2) but it has fib(n-1), so exchange with bl
loop next_number
mov ah,4ch
int 21h
code ends
end start
Part B
1. Read the status of eight input bits from the Logic Controller Interface and display FF if it is
even parity bits otherwise display 00. Also display number of 1s in the input data.
assume cs:code,ds:data
data segment
pa equ 44A0h
pb equ 44A1h
pc equ 44A2h
cr equ 44A3h
msg db "No. of 1's : "
ones db ?,"$"
data ends
code segment
start:
mov ax,data
mov ds,ax
mov dx,cr
mov al,82h
out dx,al
Page 9 of 20
Microprocessors Lab
mov dx,pb
in al,dx
mov cx,8
mov ah,00
rot_again:
ror al,1
jnc next
inc ah
next:
loop rot_again
mov bl,ah
add ah,30h
mov ones,ah
; read from PB
; [ah] = no. of 1s
; store ASCII value of [ah]
lea dx,msg
mov ah,09h
int 21h
mov al,00h
ror bl,1
jc disp
mov al,0ffh
disp:
mov dx,pa
out dx,al
mov ah,4ch
int 21h
code ends
end start
2 ) Read the status of two 8-bits input (X & Y) from the logic Controller interface and display X*Y.
assume cs:code,ds:data
data segment
pa equ 44A0h
pb equ 44A1h
pc equ 44A2h
cr equ 44A3h
msg1 db "Read 1st num from the port followed by carriage return$
msg2 db 0ah,0dh,"Read 2nd num from the port followed by carriage return$
data ends
code segment
by - 4th sem students of Information Science and Engineering Feb 2009
Page 10 of 20
Microprocessors Lab
start:
mov ax,data
mov ds,ax
lea dx,msg1
mov ah,09h
int 21h
mov ah,07
int 21h
mov dx,pb
in al,dx
mov bl,al
lea dx,msg2
mov ah,09h
int 21h
mov ah,7
int 21h
mov dx,pb
in al,dx
mul bl
mov dx,pa
out dx,al
mov al,ah
mov dx,pc
out dx,al
mov ah,4ch
int 21h
code ends
end start
3) Drive a Stepper Motor interface to rotate the motor in clockwise direction by N steps (N
specified by the examiner). Introduce suitable delay between successive steps. (Any arbitrary
value for the delay may be assumes by the student).
by - 4th sem students of Information Science and Engineering Feb 2009
Page 11 of 20
Microprocessors Lab
assume cs:code,ds:data
data segment
pa equ 44A0h
pb equ 44A1h
pc equ 44A2h
cr equ 44A3h
data ends
code segment
start:
mov ax,data
mov ds,ax
mov dx,cr
mov al,80h
out dx,al
;mov cx,64h (or) mov cx,100d(for 180 degrees rotation 180/1.8)
;mov cx,32h (or) mov cx,50d(for 90 degrees rotation 90/1.8)
mov cx,64h
mov al,77h
mov dx,pc
rot_clock:
out dx,al
ror al,1
call delay
loop rot_clock
mov ah,4ch
int 21h
delay proc
mov bx,02fffh
l2: mov di,0ffffh
l1: dec di
jnz l1
dec bx
jnz l2
ret
delay endp
code ends
end start
Page 12 of 20
Microprocessors Lab
4) Drive a Stepper Motor interface to rotate the motor in anticlockwise direction by N steps (N is
specified by the examiner). Introduce suitable delay between successive steps (Any arbitrary
value for the delay may be assumed by the student).
assume cs:code,ds:data
data segment
pa equ 44A0h
pb equ 44A1h
pc equ 44A2h
cr equ 44A3h
data ends
code segment
start:
mov ax,data
mov ds,ax
mov dx,cr
mov al,80h
out dx,al
;mov cx,64h (or) mov cx,100d(for 180 degrees rotation 180/1.8)
;mov cx,32h (or) mov cx,50d(for 90 degrees rotation 90/1.8)
mov cx,64h
mov al,77h
mov dx,pc
rot_anticlock:
out dx,al
rol al,1
; only this instruction changes compared to previous program
call delay
loop rot_anticlock
mov ah,4ch
int 21h
delay proc
mov bx,02fffh
l2: mov di,0ffffh
l1: dec di
jnz l1
dec bx
jnz l2
ret
delay endp
code ends
by - 4th sem students of Information Science and Engineering Feb 2009
Page 13 of 20
Microprocessors Lab
end start
5) Drive a Stepper Motor interface to rotate the motor by N steps left direction and N steps right
direction (N is specified by the examiner). Introduce suitable delay between successive steps
(Any arbitrary value for the delay may be assumed by the student).
assume cs:code,ds:data
data segment
pa equ 44A0h
pb equ 44A1h
pc equ 44A2h
cr equ 44A3h
data ends
code segment
start:
mov ax,data
mov ds,ax
mov dx,cr
mov al,80h
out dx,al
mov cx, 64h
mov al, 77h
mov dx,pc
rot_clock:
out dx,al
ror al,1
call delay
loop rot_clock
mov cx,32h
mov al,77h
mov dx,pc
rot_anticlock:
out dx,al
rol al,1
call delay
loop rot_anticlock
mov ah,4ch
int 21h
delay proc
mov bx,02fffh
l2: mov di,0ffffh
by - 4th sem students of Information Science and Engineering Feb 2009
Page 14 of 20
Microprocessors Lab
l1: dec di
jnz l1
dec bx
jnz l2
ret
delay endp
code ends
end start
6) Scan a 8x3 keypad for a key closure and to store the code for the key pressed in a memory
location or display on screen. Also display row and column numbers of the key pressed.
assume cs:code,ds:data
data segment
pa equ 44A0h
pb equ 44A1h
pc equ 44A2h
cr equ 44A3h
rowval db ?
colval db ?
scode db ?
data ends
code segment
start:
mov ax,data
mov ds,ax
mov dx,cr
mov al,90h
out dx,al
try_again:
mov bl,01h
mov bh,03h
mov cl,00h
mov ah,01h
next_row:
mov dx,pc
mov al,bl
out dx,al
mov dx,pa
in al,dx
cmp al,00h
th
Page 15 of 20
Microprocessors Lab
jne calculate
add cl,08h
inc ah
shl bl,01
dec bh
jnz next_row
jmp try_again
calculate:
mov rowval,ah
mov ah,00h
rot_again:
ror al,01
jnc next
inc ah
inc cl
jmp rot_again
next:
mov scode,cl
mov colval,ah
mov al,cl
call disp
mov ah,4ch
int 21h
disp proc
mov bl,al
mov cl,4
shr al,cl
cmp al,09
jle add_30
add al,07
add_30:
add al,30h
mov dl,al
mov ah,02
int 21h
mov al,bl
and al,0fh
cmp al,09
jle add_30_1
add al,07
add_30_1:
add al,30h
mov dl,al
mov ah,02
int 21h
ret
by - 4th sem students of Information Science and Engineering Feb 2009
Page 16 of 20
Microprocessors Lab
disp endp
code ends
end start
6) Read the current time from the system and display it in the standard format on the screen
assume cs:code
code segment
start:
mov ah,2ch
int 21h
mov al,ch
call hex_bcd
call disp
mov dl,':'
mov ah,2
int 21h
mov al,cl
call hex_bcd
call disp
mov ah,4ch
int 21h
disp proc
push cx
mov ah,00h
mov cx,4
shl ax,4
shr al,4
add ax,3030h
push ax
mov dl,ah
mov ah,2
int 21h
pop ax
mov ah,2
mov dl,al
int 21h
pop cx
ret
endp
Page 17 of 20
Microprocessors Lab
hex_bcd proc
; procedure to convert hex to bcd
push cx
mov cl,al
mov ch,0
mov al,0
next:
add al,1
daa
loop next
pop cx
ret
endp
code ends
end start
7) Drive an Elevator interface in the following way to move an elevator from ground to top floor
and top to ground floor
assume cs:code,ds:data
data segment
pa equ 44A0h
pb equ 44A1h
pc equ 44A2h
cr equ 44A3h
data ends
code segment
start:
mov ax,data
mov ds,ax
mov al, 82h
mov dx,cr
out dx,al
mov dx,pa
mov al,00h
out dx,al
mov al,0f0h
out dx,al
mov dx, pb
scan_again:
in al, dx
and al,0fh
cmp al,0fh
; masking MS 4 bits
; is there any request at all?
Page 18 of 20
Microprocessors Lab
Mohan Kumar S Dept. of ISE
je scan_again ; no, then please give one.
mov cl,01
; up to this LED the elevator should move
rot_again:
ror al,1
; checking for floor from which the request has come ( a 0 represents request)
jc next
; If there is carry, then there is no request
jmp start_mov ; there was no carry (ie. CF = 0), we identified the floor from which the
; request is made
next:
add cl,03h
jmp rot_again
start_mov:
mov dx,pa
mov al,0f0
; f in MS nibble says, dont clear the request
next_led:
out dx,al
; at last elevator started moving
call delay
inc al
dec cl
jnz next_led
call delay
; wait for some time to pick passenger
call delay
dec al
and al,0fh
; now clear the request, but keep led number intact
come_down:
out dx,al
call delay
dec al
cmp al,00h
; have I reached ground floor?
jge come_down ; no, then come down still
mov ah,4ch
int 21h
delay proc
mov bx,02fffh
l2: mov di,0ffffh
l1: dec di
jnz l1
dec bx
jnz l2
ret
delay endp
code ends
end start
Page 19 of 20
Microprocessors Lab
Page 20 of 20