8086 Assembly Language Programs |
;11. Write an 8086 ALP to check for the password using DOS interrupt.If
; entry does not match password display "Wrong Password! Try
Again" and
; remain in the loop,else display "You are authorized
person" and come
; out.
name checkpassword
page 60,80
title to check for password using DOS function call
.model small
.stack 64
.data
cr equ 13
lf equ 10
password db 'INDIA$'
prompt db 'Enter Password & then <cr> (Max 40 chars)',cr,lf,'$'
entry db 41 dup(?)
msgsuc db 'You are Authorized person',cr,lf,'$'
msgfail db 'Wrong Password! Try Again',cr,lf,'$'
.code
pass:
mov ax,@data
mov ds,ax
mov es,ax
lea dx,prompt
mov ah,09h
int 21h
mov bp,0
tryagain:
mov cx,40
mov bx,0
again:
mov ah,08h ;read a char from KB w/o
echoing on screen
int 21h ;stores the
char read in al
cmp al,0dh ;cmp al with <cr>(0dh)
je action
mov entry[bx],al ;store the chars read in
entry[]
inc bx
loop again ;to read next char from KB
action:
mov entry[bx],'$' ;store $ at the
end of the array
lea si,password
lea di,entry
mov
cx,06 ;cx=6 length of the
password
repe
cmpsb ;cmp si(password) &
di(entry)
je sucmsg
lea dx,msgfail
mov ah,09h
int 21h
jmp tryagain
sucmsg:
lea dx,msgsuc
mov ah,09h
int 21h
mov ah,4ch
int 21h
end pass
;12. Write an 8086 alp to compute factorial of a given 8 bit
integer at a
; byte location using recursion.The Program should display the
number
; and its factorial(4 digit hex value) with an appropriate message
on
; the CRT.
name factorial
page 60,80
title recursive computation of factorial
.model small
.stack 64
.data
num equ 3
msg db 'Factorial of ',num+'0',' is:'
ascres db 4 dup(?),'H',0dh,0ah,'$'
res dw ?
hexcode db '0123456789abcdef'
.code
hex_asc proc
mov dl,10h
mov ah,0
mov bx,0
div
dl ;div al/dl where al=char &
dl=10h
mov bl,al
;al=quotient
mov dh,hexcode[bx]
mov bl,ah
;ah=remainder
mov dl,hexcode[bx]
ret
hex_asc endp
fact proc
cmp ax,01 ;if n=1,
fact=1 else fact=n*fact(n-1)
je exit
push ax
dec
ax ;n-1
call fact ;fact(n-1)
pop ax
mul res
;n*fact(n-1)
mov res,ax ;res=factorial
ret
exit:
mov res,01
ret
fact endp
main:
mov ax,@data
mov ds,ax
mov ax,num ;ax=n
call fact
mov al,byte ptr res+1 ;convert msb
of result to ascii
call hex_asc
mov ascres,dh
mov ascres+1,dl
mov al,byte ptr res
;convert lsb of result to ascii
call hex_asc
mov ascres+2,dh
mov ascres+3,dl
mov ah,09h
mov dx,offset
msg ;display msg
int 21h
mov
ah,4ch ;exit
int 21h
align 16
end main
;13. Write an 8086 alp to rename a file,if it exists,using DOS
interrupt.
; Otherwise display an error message.
name rename_file
page 60,80
title program to rename a file using DOS function 56h
.model small
.stack 64
.data
old db 'bubsort.asm',0
new db 'bubble.asm',0
sucmsg db 'bubsort.asm renamed as bubble.asm','$'
failmsg db 'Error! bubsort.asm could not be renamed','$'
.code
main:
mov ax,@data
mov ds,ax
mov es,ax
lea dx,old ;ds:dx points to the ASCIIZ
string 'bubsort.asm',0
lea di,new ;es:di points to the ASCIIZ
string 'bubble.asm',0
mov ah,56h ;DOS function 56h is used for
renaming
int 21h
jc error ;if there is an
error carry flag is set
lea dx,sucmsg
jmp display
error:
lea dx,failmsg
display:
mov ah,09h
int 21h
mov ah,4ch
int 21h
end main
;14. Write an 8086 alp to search for a given 8 bit field using
linear
; search in an array of records with 2 fields.The searchkey
is the first
; byte of the record.Message should be displayed on CRT
indicating
; whether the search was a success or a failure.If it is a
success case,
; the position of the record in the array is to be
displayed.
name linear_records
page 60,80
title linear search on an array of records
.model small
.stack 64
.data
array db 55h,22h,33h,55h,45h,11h,66h,44h
len dw ($-array)/2
scrkey equ 66h
asc1 equ (scrkey/10h)+'0'
asc2 equ (scrkey mod 10h)+'0'
msgsuc db 'Record with first byte as ',asc1,asc2
db ' Found at position: '
result db ?,0dh,0ah,'$'
failmsg db 'Record with first byte as ',asc1,asc2
db ' Not found ',0dh,0ah,'$'
.code
main:
mov ax,@data
mov ds,ax
mov cx,len
mov bx,0
mov al,scrkey
again:
cmp al,array[bx]
je sucmsg
inc bx
inc bx
loop again
failure:
lea dx,failmsg
jmp display
sucmsg:
ror bx,1
inc
bx ;position=(bx/2)+1
add bl,'0'
mov result,bl
lea dx,msgsuc
display:
mov ah,09h
int 21h
mov ah,4ch
int 21h
end main
;15. Write an 8086 alp to multiply two 3x3 matrices of signed 8 bit
; integers.Display result using DEBUG or CODEVIEW.Assume
that each
; of the elements of the product matrix can be stored in 8
bits.
name matrixmul
page 60,80
title 8086 alp for matrix multiplication of 3x3 matrices
.model small
.stack 64
.data
ar1 db 2,2,2 ;row1 of array A
ar2 db 2,2,2 ;row2 ,,
ar3 db 2,2,2 ;row3 ,,
bc1 db 1,1,1 ;column1 of array B
bc2 db 1,1,1 ;column2 ,,
bc3 db 1,1,1 ;column3 ,,
c db 9 dup(?) ;result matrix
l2 db ?
l1 db ?
.code
main:
mov ax,@data
mov ds,ax
mov es,ax
mov bp,0
mov l2,3
lea si,ar1
rep2:
lea di,bc1
mov l1,3
rep1:
call matmul
mov ds:c[bp],dl
inc bp
add di,3
dec l1
jnz rep1
add si,3
dec l2
jnz rep2
int 3
matmul proc
mov cx,3
mov bx,0
mov dl,0
again:
mov al,[si][bx]
imul byte ptr [di][bx]
add dl,al
inc bx
loop again
ret
matmul endp
align 16
end main
;16. Write an 8086 alp to compute nCr,given n and r,using
recursion.
; Dislpay result using DEBUG.
name nCr
page 60,80
title computation of nCr using recursion
.model small
.stack 64
.data
n db 4
r db 2
res db ?
.code
main:
mov ax,@data
mov ds,ax
mov al,n
mov bl,r
call ncr
int 3
ncr proc
cmp al,bl ;if n=r then ncr=1
je p8
cmp bl,0 ;if r=0 then
ncr=1
je p8
cmp bl,1 ;if r=1 then
ncr=n
je p10
dec al ;n-1
cmp bl,al ;if r=n-1 then
ncr=n
je p9
push ax ;(n-1)Cr
push bx ;
call ncr ;
pop bx
pop ax
dec bl ;
push ax ;
push bx ;
call ncr ;(n-1)C(r-1)
pop bx
pop ax
ret
p8: inc res
ret
p9: inc res
p10: add res,al
ret
align 16
ncr endp
end main
;17. Write an 8086 alp to read a string of 8 characters on screen
at(x1,y1)
; and display the same at (x2,y2) using BIOS interrupts.
name movestring
page 60,80
title to move a string of 8 chars on CRT from location (15,25) to (18,35)
.model small
.stack 64
.data
str db 'ABCDEFGH'
oldrow db 15
oldcol db 25
newrow db 18
newcol db 35
.code
main:
mov ax,@data
mov ds,ax
mov bh,0 ;bh=page 0
mov si,0
mov dh,oldrow
mov dl,oldcol
repeat:
mov ah,02h ;set cursor position at
(dh,dl)
int 10h
mov al,str[si]
mov bl,07h
mov cx,1
mov ah,09h
int 10h
inc dl
inc si
cmp si,08
jl repeat
mov si,08
again:
call movchar
inc oldcol
inc newcol
dec si
jnz again
mov ah,4ch
int 21h
movchar proc
mov dh,oldrow
mov dl,oldcol
mov ah,02h
int 10h
mov ah,08h ;to
read a char and its attribute
int 10h
mov bl,ah
;bl=attribute byte(07h)
mov dh,newrow
mov dl,newcol
mov ah,02h ;set
cursor position at (dh,dl)
int 10h
mov ah,09h
mov cx,1
int 10h
ret
movchar endp
end main
;18. Write an 8086 alp which checks whether the printer is
online.If it
; is online,print a message on the printer using DOS
interrupt,else
; display printer status on CRT.
name printmsg
page 60,80
title program to send a message to printer
.model small
.stack 64
.data
msg db 'If this is Printed on paper',0dh,0ah
db 'Then Program is Working',0dh,0ah
len equ $-msg
errmsg db 'Error! Printer is not connected or switched off',0dh,0ah,'$'
.code
main:
mov ax,@data
mov ds,ax
mov ah,02h ;get printer status
mov dx,0 ;printer 0
int 17h ;returns with
ah=status
rol ah,01 ;if ah7=1 then printer is
ready | mov ah7 to carry flag
jc online
offline:
lea dx,errmsg
mov ah,09h ;displays errmsg
int 21h
jmp exit
online:
mov cx,len
mov si,00h
mov ah,05h ;prints the char in dl
on printer
again:
mov dl,msg[si]
int 21h
inc si
loop again ;dec cx,until cx=0
exit:
mov ah,4ch
int 21h
end main
;19. Write an 8086 alp to display the command line parameters,and
the total
; length of the parameters using DOS interrupts.
name commdlinepara
page 60,80
title to display command line parameters
.model small
.stack 64
.data
hexcode db '0123456789abcdef'
msg db 'Total length of parameters (in Hex) is:'
len db ?,?,0dh,0ah,'The parameters are: $'
.code
hex_asc proc
mov dl,10h
mov ah,0
mov bx,0
div dl
mov bl,al
mov dh,hexcode[bx]
mov bl,ah
mov dl,hexcode[bx]
ret
hex_asc endp
main:
mov bx,80h
mov cl,[bx]
mov ax,@data
mov ds,ax
mov al,cl
call hex_asc
mov len,dh
mov len+1,dl
lea dx,msg
mov ah,09h
int 21h
mov ah,62h ;returns with bx=segment
address of PSP
int 21h
mov ds,bx
mov dx,81h ;[starting from 81h in
the PSP the cmd line parameters
mov bx,dx ; are stored] |
bx=81h or bl=81h |
add bl,cl ;81h+(length of cmd
line parameters)
mov byte ptr[bx],'$' ;mov '$' at the end
of cmd line parameters
mov ah,09h
int 21h ;displays
the cmd line parameters pointed by dx
mov ah,4ch ;exit
int 21h
end main
All the above Programs were executed on Microsoft's Macro Assembler Version 5.0
© Copyrights Madhu Sudan Rao G.K