Sursa: https://www.geocities.com/gkmsr007/a1.htm
8086 Assembly Language Programs
Contents
1. Bubble Sort
2. Selection sort
3. Insertion Sort
4. Multi byte Addition/Subtraction
5. Clear Screen Using BIOS Interrupt
6. GCD of 4 unsigned 16 bit numbers
7. LCM of 2 16 bit unsigned numbers
8. Linear Search
9. Binary Search
10.Memory size of the PC you are using,using BIOS
interrupt
11.To Check for Password using DOS interrupt
12.Factorial of a number using Recursion
13.To Rename a File using DOS interrupt
14.Linear Search in an array of records with 2 fields
15.Multiplication of two 3x3 matrices
16.Computation of nCr using Recursion
17.Move a string of characters on the CRT
18.To Check if the Printer is online and print a
message
19.Display the Command line parameters using DOS
interrupts
;1 Write an
alp to sort in ascending order using bubble sort algorithm
; 141q1621b ; a given set of byte sized unsigned numbers in
memory.The sorted
; 141q1621b ; elements should replace the original unsorted
elements in memory.
141q1621b ; name bubblesort
141q1621b ; page 60,80
title ascending order using bubble sort
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .data
a db 34h,78h,56h,47h
si_ze dw $-a ;si_ze=no of elements
141q1621b ; .code
bubsort:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov bx,si_ze
141q1621b ; dec bx 141q1621b ;
;bx=no of passes needed to complete sorting(n-1)
outlup:
141q1621b ; mov cx,bx ;cx=no of
comparisions to be performed in a pass
141q1621b ; mov si,0
inlup:
141q1621b ; mov al,a[si]
141q1621b ; inc si
141q1621b ; cmp al,a[si]
141q1621b ; jb go_on
141q1621b ; xchg al,a[si]
141q1621b ; mov a[si-1],al
go_on:
141q1621b ; loop inlup ;dec cx,until cx=0
141q1621b ; dec bx
141q1621b ; jnz outlup
141q1621b ; int
3 141q1621b ; 141q1621b ; ;breakpoint
interrupt
141q1621b ; align 16
end bubsort
;2. Write an 8086 alp
to sort in descending order,using selestion sort
; algorithm a given set of 8 bit unsigned numbers in
memory.The sorted elements
; should replace the original unsorted elements in
memory.Store in a memory
; location the number of comparisions made.
141q1621b ; name selectionsort
141q1621b ; page 60,80
title descending order using selection sort
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .data
a db 44h,11h,22h,66h
si_ze dw $-a 141q1621b ; 141q1621b ;
;si_ze=4
nc dw
? 141q1621b ; 141q1621b ; 141q1621b ; 141q1621b ;
;total no of comparisions made
141q1621b ; .code
selsort:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov dx,si_ze ;in
selsort for n elements to sort we need n-1 passes
141q1621b ; dec
dx 141q1621b ; 141q1621b ; ;dx=3 no of passes
required
141q1621b ; mov nc,0
outlup:
141q1621b ; mov
cx,dx 141q1621b ; ;cx=no of comparisions to be performed
in a pass
141q1621b ; mov si,0
141q1621b ; mov ah,a[si]
141q1621b ; mov bx,si
inlup:
141q1621b ; inc si
141q1621b ; inc nc
141q1621b ; cmp ah,a[si]
141q1621b ; jb go_on
141q1621b ; mov ah,a[si]
;ah=smallest element in the vector
141q1621b ; mov
bx,si 141q1621b ; ;bx=position of the smallest element
go_on:
141q1621b ; loop
inlup 141q1621b ; ;untill cx=0
141q1621b ; xchg ah,a[si] ;xchg the
last element pointed by si,with the
141q1621b ; mov a[bx],ah
;smallest element pointed by bx
141q1621b ; dec dx
141q1621b ; jnz outlup
141q1621b ; int
3 141q1621b ; 141q1621b ; ;breakpoint
interrupt
141q1621b ; align 16
end selsort
;3. Write an 8086
alp to sort in ascending order using Insertion Sort
; 141q1621b ; algorithm,a given set of 16 bit unsigned numbers in
memory.The sorted
; 141q1621b ; elements should replace the original unsorted
elements in memory.
141q1621b ; name insertionsort
141q1621b ; page 60,80
title ascending order using insertion sort algorithm
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .data
a dw 78h,34h,12h,56h
si_ze dw ($-a)/2 141q1621b ; ;si_ze=4(no of
elements)
141q1621b ; .code
insort:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov
cx,2 141q1621b ; ;cx=2,insert the second element
in the proper position
outlup:
141q1621b ; mov dx,cx
141q1621b ; dec
dx 141q1621b ; ;dx=cx-1,max no of comparisions needed
to insert element
141q1621b ; mov si,dx
141q1621b ; add si,si
141q1621b ; mov ax,a[si]
inlup:
141q1621b ; cmp a[si-2],ax
141q1621b ; jbe inlupexit
141q1621b ; mov di,a[si-2]
141q1621b ; mov a[si],di
141q1621b ; dec si
141q1621b ; dec si
141q1621b ; dec dx
141q1621b ; jnz inlup
inlupexit:
141q1621b ; mov a[si],ax
141q1621b ; inc
cx 141q1621b ; ;inc cx to insert the next element in
proper position
141q1621b ; cmp cx,si_ze
141q1621b ; jbe outlup
exit:
141q1621b ; int
3 141q1621b ; ;breakpoint interrupt
141q1621b ; align 16
end insort
;4. Add/Sub of
multiword
141q1621b ; name addsub
141q1621b ; page 60,80
title 8086 alp for multi word addition/subtraction
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .data
n1 db 12h,34h,56h,78h,9ah,0bch,0deh,0f0h
n2 db 0bch,12h,78h,34h,56h,0deh,0f0h,9ah
s db 8 dup(?)
d db 8 dup(?)
141q1621b ; .code
addsub:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov
cx,8 141q1621b ; ;cx is used as loop counter
141q1621b ; mov
bx,7 141q1621b ; ;bx contains the offset address
of byte in n1 & n2
141q1621b ; clc
addagn:
141q1621b ; mov al,n1[bx]
141q1621b ; adc al,n2[bx]
141q1621b ; mov s[bx],al
;store sum in s[]
141q1621b ; dec bx
141q1621b ; loop addagn
141q1621b ; mov cx,8
141q1621b ; mov bx,7
141q1621b ; clc
subagn:
141q1621b ; mov al,n1[bx]
141q1621b ; sbb al,n2[bx]
141q1621b ; mov d[bx],al
;store difference in d[]
141q1621b ; dec bx
141q1621b ; loop subagn
141q1621b ; int 3
141q1621b ; align 16
end addsub
;5. Write an 8086
alp to get the screen width(no of cols) using BIOS
; 141q1621b ; interrupt,and calculate the no of rows from the
appropriate word
; 141q1621b ; location in BIOS data area,and clear the screen using
BIOS interrupt.
141q1621b ; name clearscreen1
141q1621b ; page 60,80
title clear screen using bios interrupt
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .data
bytes dd 0040004ch
rows db ?
cols db ?
msg1 db 0dh,0ah,'Total no of rows(in hex)=','$'
msg2 db 0dh,0ah,'Total no of columns(in hex)=','$'
msg3 db 0dh,0ah,'Press any key to clear screen','$'
hexcode db '0123456789abcdef'
141q1621b ; .code
display proc
141q1621b ; push ax
141q1621b ; push bx
141q1621b ; push cx
141q1621b ; push dx
141q1621b ; lea dx,msg1 141q1621b ;
;displays msg1
141q1621b ; mov ah,09h
141q1621b ; int 21h
141q1621b ; mov al,rows 141q1621b ;
;al=no of rows[25]
141q1621b ; mov cl,10h
141q1621b ; mov ah,00h
141q1621b ; div
cl 141q1621b ; 141q1621b ; ;25/10 |
al=quotient[2] | ah=remainder[5]
141q1621b ; mov
bl,al 141q1621b ; ;al=2
141q1621b ; mov dl,hexcode[bx] ;dl=8 bit ascii
code of char to be displayed[2]
141q1621b ; push ax
141q1621b ; mov
ah,02h 141q1621b ; ;display char to std o/p dev
141q1621b ; int 21h
141q1621b ; pop ax
141q1621b ; mov
bl,ah 141q1621b ; ;ah=5
141q1621b ; mov dl,hexcode[bx] ;dl=8 bit ascii
code of char to be displayed[5]
141q1621b ; mov
ah,02h 141q1621b ; ;display char to std o/p dev
141q1621b ; int 21h
141q1621b ; lea dx,msg2 141q1621b ;
;displays msg2
141q1621b ; mov ah,09h
141q1621b ; int 21h
141q1621b ; mov al,cols 141q1621b ;
;al=no of cols[80]
141q1621b ; mov cl,10h
141q1621b ; mov ah,00h
141q1621b ; mov bh,00h
141q1621b ; div
cl 141q1621b ; 141q1621b ; ;80/10 |
al=quotient[8] | ah=remainder[0]
141q1621b ; mov
bl,al 141q1621b ; ;al=8
141q1621b ; mov dl,hexcode[bx] ;dl=8 bit
ascii code of char to be displayed[8]
141q1621b ; push ax
141q1621b ; mov
ah,02h 141q1621b ; ;display char to std o/p dev
141q1621b ; int 21h
141q1621b ; pop ax
141q1621b ; mov
bl,ah 141q1621b ; ;ah=0
141q1621b ; mov dl,hexcode[bx] ;dl=8 bit
ascii code of char to be displayed[0]
141q1621b ; mov
ah,02h 141q1621b ; ;display char to std o/p dev
141q1621b ; int 21h
141q1621b ; pop dx
141q1621b ; pop cx
141q1621b ; pop bx
141q1621b ; pop ax
141q1621b ; ret
display endp 141q1621b ; 141q1621b ;
;end display procedure
main:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov ah,0fh
141q1621b ; int 10h
141q1621b ; mov cols,ah 141q1621b ;
;ah=no of char cols on screen
141q1621b ; mov cl,ah
141q1621b ; mov ch,0
141q1621b ; push ds
141q1621b ; lds si,bytes
141q1621b ; mov ax,[si] 141q1621b ;
;ax=total no of bytes on video page(1b ascii+1b AB)
141q1621b ; pop ds
141q1621b ; shr
ax,1 141q1621b ; ;divides ax by 2 to get total no
of chars on page
141q1621b ; div cl
141q1621b ; mov rows,al
141q1621b ; call display
141q1621b ; lea dx,msg3
141q1621b ; mov ah,09h ;displays msg3
141q1621b ; int 21h
141q1621b ; mov ah,01h ;i/p char from std i/p
dev & echo to std o/p dev
141q1621b ; int 21h
141q1621b ; mov dh,0 ;initialize row coordinate
to 0
again:
141q1621b ; mov bh,0 ;bh=page 0
141q1621b ; mov dl,0 ;initialize column
coordinate to 0
141q1621b ; mov ah,02h ;set cursor position to
(dl,dh)
141q1621b ; int 10h
141q1621b ; mov bl,0 ;colour(set foregnd &
bckgnd of char with same colour)
141q1621b ; mov al,'x' ;char to be displayed
141q1621b ; mov ah,09h ;write char at cursor
position
141q1621b ; int 10h
141q1621b ; inc dh ;inc row coordinate by
one position
141q1621b ; cmp dh,rows
141q1621b ; jb again
141q1621b ; mov ah,4ch ;exit
141q1621b ; int 21h
end main
;6. GCD of 4 unsigned
16 bit numbers
141q1621b ; name gcd
141q1621b ; page 60,80
title program to find gcd of 4 unsigned 16 bits numbers
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .data
values dw 0090,0120,4bh,0019h
gcd dw ?
141q1621b ; .code
hcf proc
again:
141q1621b ; cmp ax,bx
141q1621b ; je exit
141q1621b ; jb bigbx
divaxbx:
141q1621b ; mov dx,0
141q1621b ; div bx
141q1621b ; cmp dx,0
141q1621b ; je exit
141q1621b ; mov ax,dx
141q1621b ; jmp again
bigbx:
141q1621b ; xchg ax,bx
141q1621b ; jmp divaxbx
exit:
141q1621b ; mov gcd,bx
141q1621b ; ret
hcf endp
;Main program
gcd4:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov ax,values
141q1621b ; mov bx,values+2
141q1621b ; call hcf
141q1621b ; mov ax,gcd
141q1621b ; mov bx,values+4
141q1621b ; call hcf
141q1621b ; mov ax,gcd
141q1621b ; mov bx,values+6
141q1621b ; call hcf
141q1621b ; int 3
141q1621b ; align 16
end gcd4
;7.
LCM of 2 16 bit unsigned numbers
141q1621b ; name lcm
141q1621b ; page 60,80
title program to find lcm of 2 16 bit unsigned numbers
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .data
values dw 0025,0015
lcm dw 2 dup(?)
141q1621b ; .code
l_c_m:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov dx,0
141q1621b ; mov ax,values ;dx_ax=25
141q1621b ; mov bx,values+2 ;bx=15
again:
141q1621b ; push ax
141q1621b ; push dx
141q1621b ; div bx
141q1621b ; cmp
dx,0 141q1621b ; ;remainder of the division is
stored in dx
141q1621b ; je exit
141q1621b ; pop dx
141q1621b ; pop ax
141q1621b ; add ax,values
141q1621b ; jnc noincdx
141q1621b ; inc dx
noincdx:
141q1621b ; jmp again
exit:
141q1621b ; pop lcm+2
141q1621b ; pop lcm
141q1621b ; int 3
141q1621b ; align 16
end l_c_m
;8.
Write an 8086 alp to search for a given 8 bit value using linear search
; 141q1621b ; in an array of 8 bit numbers.Message should be
displayed on crt
; 141q1621b ; indicating whether the search was a failure or a
success.If it is a
; 141q1621b ; success case,the position of the element in the array
is to be displayed
141q1621b ; name linearsearch
141q1621b ; page 60,80
title linear search program
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .data
array db 55h,33h,44h,66h,22h
len dw $-array ;length=5
scrkey equ 33h
asc1 equ (scrkey/10h)+'0' 141q1621b ; ;asc1='3'
asc2 equ (scrkey mod 10h)+'0' ;asc2='5'
sucmsg db 'Element ',asc1,asc2,' found at position:'
result db ?,0ch,0ah,'$'
failmsg db 'Element ',asc1,asc2,' Not found',0ch,0ah,'$'
141q1621b ; .code
lin:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov es,ax
141q1621b ; cld 141q1621b ;
;direction flag D=0
141q1621b ; mov
di,0 141q1621b ; ;di=0,autoincrement di
141q1621b ; mov al,scrkey ;al=35
141q1621b ; mov cx,len
141q1621b ; repne scasb 141q1621b ;
;scasb==>[al]-[[di]]
141q1621b ; jz success
141q1621b ; lea dx,failmsg
141q1621b ; jmp display
success:
141q1621b ; mov
bx,di 141q1621b ; ;bx=position of the scrkey found in
array
141q1621b ; add
bl,'0' 141q1621b ; ;convert this position into ascii for
display purpose
141q1621b ; mov result,bl
141q1621b ; lea dx,sucmsg
display:
141q1621b ; mov ah,09h
141q1621b ; int 21h
141q1621b ; mov ah,4ch
141q1621b ; int 21h
141q1621b ; align 16
end lin
;9.
Write an 8086 alp to search for a given 16 bit value using binary search
; 141q1621b ; in an array of 16 bit numbers,which are in ascending
order.Message
; 141q1621b ; should be displayed on CRT indicating whether the
search was a failure
; 141q1621b ; or a success.If it is a success case,the position of
the element in the
; 141q1621b ; array is to be displayed.
141q1621b ; name binarysearch
141q1621b ; page 60,80
title binary search program to search a 16 bit value
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .data
cr equ 13
lf equ 10
array dw 1122h,2345h,3344h,4455h,5566h
len dw ($-array)/2 141q1621b ; ;length=5
scrkey equ 2345h
asc1 equ (scrkey/1000h)+'0' 141q1621b ; ;asc1='2'
asc2 equ (scrkey/100h) mod 10h + '0' ;asc2='3'
asc3 equ (scrkey/10h) mod 10h " '0' 141q1621b ; ;asc3='4'
asc4 equ (scrkey mod 10h) + '0' 141q1621b ; 141q1621b ;
;asc4='5'
sucmsg db 'Given Element '
141q1621b ; db ' Found at position:'
result db ?,cr,lf,'$'
failmsg db 'Given Element '
141q1621b ; db ' Not found',cr,lf,'$'
141q1621b ; .code
binscr:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov bx,1
141q1621b ; mov
dx,len 141q1621b ; ;dx=5
141q1621b ; mov cx,scrkey ;cx=2345
again:
141q1621b ; cmp bx,dx
141q1621b ; ja failure
141q1621b ; mov ax,bx
141q1621b ; add ax,dx
141q1621b ; shr ax,1
141q1621b ; mov si,ax
141q1621b ; dec si
141q1621b ; add si,si
141q1621b ; cmp cx,array[si]
141q1621b ; jae biger
141q1621b ; dec ax
141q1621b ; mov dx,ax
141q1621b ; jmp again
biger:
141q1621b ; je success
141q1621b ; inc ax
141q1621b ; mov bx,ax
141q1621b ; jmp again
success:
141q1621b ; add al,'0'
141q1621b ; mov result,al
141q1621b ; lea dx,sucmsg
141q1621b ; jmp display
failure:
141q1621b ; lea dx,failmsg
display:
141q1621b ; mov ah,09h
141q1621b ; int 21h
quit:
141q1621b ; mov ah,4ch
141q1621b ; int 21h
141q1621b ; align 16
end binscr
;10.
Using BIOS routine,write an 8086 alp to find memory size of the PC you
; 141q1621b ; are using.Using appropriate message,the display
should indicate memory
; 141q1621b ; size in Kilo bytes using 4 hex digits.Also check the
result with the
; 141q1621b ; appropriate word in BIOS data area using
debug/codeview.
141q1621b ; name memorysize
141q1621b ; page 60,80
title program to find memory size using int 12h
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .data
msg db 'Memory size in Kilo bytes='
ascres db 4 dup(?),'Hex',0ch,0ah,'$'
res dw ?
hexcode db '0123456789abcdef'
141q1621b ; .code
hex_asc proc
141q1621b ; mov dl,10h
141q1621b ; mov ah,0
141q1621b ; mov bx,0
141q1621b ; div dl
141q1621b ; mov bl,al
141q1621b ; mov dh,hexcode[bx]
141q1621b ; mov bl,ah
141q1621b ; mov dl,hexcode[bx]
141q1621b ; ret
hex_asc endp
main:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; int 12h
141q1621b ; mov res,ax
141q1621b ; mov al,byte ptr res
141q1621b ; call hex_asc
141q1621b ; mov ascres+2,dh
141q1621b ; mov ascres+3,dl
141q1621b ; mov al,byte ptr res+1
141q1621b ; call hex_asc
141q1621b ; mov ascres,dh
141q1621b ; mov ascres+1,dl
141q1621b ; mov dx,offset msg
141q1621b ; mov ah,09h
141q1621b ; int 21h
141q1621b ; mov ah,4ch
141q1621b ; int 21h
141q1621b ; align 16
end main
;11. Write an 8086 ALP
to check for the password using DOS interrupt.If
; 141q1621b ; entry does not match password display "Wrong
Password! Try Again" and
; 141q1621b ; remain in the loop,else display "You are
authorized person" and come
; 141q1621b ; out.
141q1621b ; name checkpassword
141q1621b ; page 60,80
title to check for password using DOS function call
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .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,'$'
141q1621b ; .code
pass:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov es,ax
141q1621b ; lea dx,prompt
141q1621b ; mov ah,09h
141q1621b ; int 21h
141q1621b ; mov bp,0
tryagain:
141q1621b ; mov cx,40
141q1621b ; mov bx,0
again:
141q1621b ; mov ah,08h ;read a char from
KB w/o echoing on screen
141q1621b ; int 21h 141q1621b ;
;stores the char read in al
141q1621b ; cmp al,0dh ;cmp al with
<cr>(0dh)
141q1621b ; je action
141q1621b ; mov entry[bx],al ;store the
chars read in entry[]
141q1621b ; inc bx
141q1621b ; loop again ;to read next char
from KB
action:
141q1621b ; mov entry[bx],'$' ;store
$ at the end of the array
141q1621b ; lea si,password
141q1621b ; lea di,entry
141q1621b ; mov
cx,06 141q1621b ; 141q1621b ; ;cx=6 length
of the password
141q1621b ; repe
cmpsb 141q1621b ; 141q1621b ; ;cmp si(password)
& di(entry)
141q1621b ; je sucmsg
141q1621b ; lea dx,msgfail
141q1621b ; mov ah,09h
141q1621b ; int 21h
141q1621b ; jmp tryagain
sucmsg:
141q1621b ; lea dx,msgsuc
141q1621b ; mov ah,09h
141q1621b ; int 21h
141q1621b ; mov ah,4ch
141q1621b ; int 21h
end pass
;12.
Write an 8086 alp to compute factorial of a given 8 bit integer at a
; 141q1621b ; byte location using recursion.The Program should
display the number
; 141q1621b ; and its factorial(4 digit hex value) with an
appropriate message on
; 141q1621b ; the CRT.
141q1621b ; name factorial
141q1621b ; page 60,80
title recursive computation of factorial
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .data
num equ 3
msg db 'Factorial of ',num+'0',' is:'
ascres db 4 dup(?),'H',0dh,0ah,'$'
res dw ?
hexcode db '0123456789abcdef'
141q1621b ; .code
hex_asc proc
141q1621b ; mov dl,10h
141q1621b ; mov ah,0
141q1621b ; mov bx,0
141q1621b ; div
dl 141q1621b ; 141q1621b ; ;div al/dl where
al=char & dl=10h
141q1621b ; mov
bl,al 141q1621b ; ;al=quotient
141q1621b ; mov dh,hexcode[bx]
141q1621b ; mov
bl,ah 141q1621b ; ;ah=remainder
141q1621b ; mov dl,hexcode[bx]
141q1621b ; ret
hex_asc endp
fact proc
141q1621b ; cmp ax,01 141q1621b ;
;if n=1, fact=1 else fact=n*fact(n-1)
141q1621b ; je exit
141q1621b ; push ax
141q1621b ; dec
ax 141q1621b ; ;n-1
141q1621b ; call fact 141q1621b ;
;fact(n-1)
141q1621b ; pop ax
141q1621b ; mul
res 141q1621b ; ;n*fact(n-1)
141q1621b ; mov res,ax
;res=factorial
141q1621b ; ret
exit:
141q1621b ; mov res,01
141q1621b ; ret
fact endp
main:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov ax,num ;ax=n
141q1621b ; call fact
141q1621b ; mov al,byte ptr res+1
;convert msb of result to ascii
141q1621b ; call hex_asc
141q1621b ; mov ascres,dh
141q1621b ; mov ascres+1,dl
141q1621b ; mov al,byte ptr
res 141q1621b ; ;convert lsb of result to ascii
141q1621b ; call hex_asc
141q1621b ; mov ascres+2,dh
141q1621b ; mov ascres+3,dl
141q1621b ; mov ah,09h
141q1621b ; mov dx,offset
msg 141q1621b ; ;display msg
141q1621b ; int 21h
141q1621b ; mov
ah,4ch 141q1621b ; 141q1621b ; 141q1621b ;
;exit
141q1621b ; int 21h
141q1621b ; align 16
end main
;13.
Write an 8086 alp to rename a file,if it exists,using DOS interrupt.
; 141q1621b ; Otherwise display an error message.
141q1621b ; name rename_file
141q1621b ; page 60,80
title program to rename a file using DOS function 56h
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .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','$'
141q1621b ; .code
main:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov es,ax
141q1621b ; lea dx,old ;ds:dx points to
the ASCIIZ string 'bubsort.asm',0
141q1621b ; lea di,new ;es:di points to
the ASCIIZ string 'bubble.asm',0
141q1621b ; mov ah,56h ;DOS function 56h
is used for renaming
141q1621b ; int 21h
141q1621b ; jc error ;if there
is an error carry flag is set
141q1621b ; lea dx,sucmsg
141q1621b ; jmp display
error:
141q1621b ; lea dx,failmsg
display:
141q1621b ; mov ah,09h
141q1621b ; int 21h
141q1621b ; mov ah,4ch
141q1621b ; int 21h
end main
;14.
Write an 8086 alp to search for a given 8 bit field using linear
; 141q1621b ; search in an array of records with 2 fields.The
searchkey is the first
; 141q1621b ; byte of the record.Message should be displayed
on CRT indicating
; 141q1621b ; whether the search was a success or a
failure.If it is a success case,
; 141q1621b ; the position of the record in the array is to
be displayed.
141q1621b ; name linear_records
141q1621b ; page 60,80
title linear search on an array of records
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .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
141q1621b ; db ' Found at position: '
result db ?,0dh,0ah,'$'
failmsg db 'Record with first byte as ',asc1,asc2
141q1621b ; db ' Not found ',0dh,0ah,'$'
141q1621b ; .code
main:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov cx,len
141q1621b ; mov bx,0
141q1621b ; mov al,scrkey
again:
141q1621b ; cmp al,array[bx]
141q1621b ; je sucmsg
141q1621b ; inc bx
141q1621b ; inc bx
141q1621b ; loop again
failure:
141q1621b ; lea dx,failmsg
141q1621b ; jmp display
sucmsg:
141q1621b ; ror bx,1
141q1621b ; inc
bx 141q1621b ; 141q1621b ; ;position=(bx/2)+1
141q1621b ; add bl,'0'
141q1621b ; mov result,bl
141q1621b ; lea dx,msgsuc
display:
141q1621b ; mov ah,09h
141q1621b ; int 21h
141q1621b ; mov ah,4ch
141q1621b ; int 21h
end main
;15. Write an 8086 alp to multiply two 3x3 matrices of
signed 8 bit
; 141q1621b ; integers.Display result using DEBUG or
CODEVIEW.Assume that each
; 141q1621b ; of the elements of the product matrix can be
stored in 8 bits.
141q1621b ; name matrixmul
141q1621b ; page 60,80
title 8086 alp for matrix multiplication of 3x3 matrices
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .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 ?
141q1621b ; .code
main:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov es,ax
141q1621b ; mov bp,0
141q1621b ; mov l2,3
141q1621b ; lea si,ar1
rep2:
141q1621b ; lea di,bc1
141q1621b ; mov l1,3
rep1:
141q1621b ; call matmul
141q1621b ; mov ds:c[bp],dl
141q1621b ; inc bp
141q1621b ; add di,3
141q1621b ; dec l1
141q1621b ; jnz rep1
141q1621b ; add si,3
141q1621b ; dec l2
141q1621b ; jnz rep2
141q1621b ; int 3
matmul proc
141q1621b ; mov cx,3
141q1621b ; mov bx,0
141q1621b ; mov dl,0
again:
141q1621b ; mov al,[si][bx]
141q1621b ; imul byte ptr [di][bx]
141q1621b ; add dl,al
141q1621b ; inc bx
141q1621b ; loop again
141q1621b ; ret
matmul endp
141q1621b ; align 16
end main
;16.
Write an 8086 alp to compute nCr,given n and r,using recursion.
; 141q1621b ; Dislpay result using DEBUG.
141q1621b ; name nCr
141q1621b ; page 60,80
title computation of nCr using recursion
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .data
n db 4
r db 2
res db ?
141q1621b ; .code
main:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov al,n
141q1621b ; mov bl,r
141q1621b ; call ncr
141q1621b ; int 3
ncr proc
141q1621b ; cmp al,bl ;if n=r
then ncr=1
141q1621b ; je p8
141q1621b ; cmp bl,0 141q1621b ; ;if
r=0 then ncr=1
141q1621b ; je p8
141q1621b ; cmp bl,1 141q1621b ; ;if
r=1 then ncr=n
141q1621b ; je p10
141q1621b ; dec
al 141q1621b ; ;n-1
141q1621b ; cmp bl,al ;if
r=n-1 then ncr=n
141q1621b ; je p9
141q1621b ; push
ax 141q1621b ; ;(n-1)Cr
141q1621b ; push
bx 141q1621b ; ;
141q1621b ; call ncr 141q1621b ; ;
141q1621b ; pop bx
141q1621b ; pop ax
141q1621b ; dec
bl 141q1621b ; ;
141q1621b ; push
ax 141q1621b ; ;
141q1621b ; push
bx 141q1621b ; ;
141q1621b ; call ncr 141q1621b ;
;(n-1)C(r-1)
141q1621b ; pop bx
141q1621b ; pop ax
141q1621b ; ret
p8: 141q1621b ; inc res
141q1621b ; ret
p9: 141q1621b ; inc res
p10: add res,al
141q1621b ; ret
141q1621b ; align 16
ncr endp
end main
;17.
Write an 8086 alp to read a string of 8 characters on screen at(x1,y1)
; 141q1621b ; and display the same at (x2,y2) using BIOS
interrupts.
141q1621b ; name movestring
141q1621b ; page 60,80
title to move a string of 8 chars on CRT from location (15,25) to (18,35)
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .data
str db 'ABCDEFGH'
oldrow db 15
oldcol db 25
newrow db 18
newcol db 35
141q1621b ; .code
main:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov bh,0 141q1621b ;
;bh=page 0
141q1621b ; mov si,0
141q1621b ; mov dh,oldrow
141q1621b ; mov dl,oldcol
repeat:
141q1621b ; mov ah,02h ;set cursor
position at (dh,dl)
141q1621b ; int 10h
141q1621b ; mov al,str[si]
141q1621b ; mov bl,07h
141q1621b ; mov cx,1
141q1621b ; mov ah,09h
141q1621b ; int 10h
141q1621b ; inc dl
141q1621b ; inc si
141q1621b ; cmp si,08
141q1621b ; jl repeat
141q1621b ; mov si,08
again:
141q1621b ; call movchar
141q1621b ; inc oldcol
141q1621b ; inc newcol
141q1621b ; dec si
141q1621b ; jnz again
141q1621b ; mov ah,4ch
141q1621b ; int 21h
movchar proc
141q1621b ; mov dh,oldrow
141q1621b ; mov dl,oldcol
141q1621b ; mov ah,02h
141q1621b ; int 10h
141q1621b ; mov
ah,08h 141q1621b ; ;to read a char and its attribute
141q1621b ; int 10h
141q1621b ; mov
bl,ah 141q1621b ; ;bl=attribute byte(07h)
141q1621b ; mov dh,newrow
141q1621b ; mov dl,newcol
141q1621b ; mov
ah,02h 141q1621b ; ;set cursor position at (dh,dl)
141q1621b ; int 10h
141q1621b ; mov ah,09h
141q1621b ; mov cx,1
141q1621b ; int 10h
141q1621b ; ret
movchar endp
end main
;18.
Write an 8086 alp which checks whether the printer is online.If it
; 141q1621b ; is online,print a message on the printer using
DOS interrupt,else
; 141q1621b ; display printer status on CRT.
141q1621b ; name printmsg
141q1621b ; page 60,80
title program to send a message to printer
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .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,'$'
141q1621b ; .code
main:
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov ah,02h ;get printer status
141q1621b ; mov dx,0 ;printer
0
141q1621b ; int 17h 141q1621b ;
;returns with ah=status
141q1621b ; rol ah,01 ;if ah7=1 then
printer is ready | mov ah7 to carry flag
141q1621b ; jc online
offline:
141q1621b ; lea dx,errmsg
141q1621b ; mov ah,09h
;displays errmsg
141q1621b ; int 21h
141q1621b ; jmp exit
online:
141q1621b ; mov cx,len
141q1621b ; mov si,00h
141q1621b ; mov ah,05h ;prints the
char in dl on printer
again:
141q1621b ; mov dl,msg[si]
141q1621b ; int 21h
141q1621b ; inc si
141q1621b ; loop again ;dec cx,until
cx=0
exit:
141q1621b ; mov ah,4ch
141q1621b ; int 21h
end main
;19.
Write an 8086 alp to display the command line parameters,and the total
; 141q1621b ; length of the parameters using DOS interrupts.
141q1621b ; name commdlinepara
141q1621b ; page 60,80
title to display command line parameters
141q1621b ; .model small
141q1621b ; .stack 64
141q1621b ; .data
hexcode db '0123456789abcdef'
msg db 'Total length of parameters (in Hex) is:'
len db ?,?,0dh,0ah,'The parameters are: $'
141q1621b ; .code
hex_asc proc
141q1621b ; mov dl,10h
141q1621b ; mov ah,0
141q1621b ; mov bx,0
141q1621b ; div dl
141q1621b ; mov bl,al
141q1621b ; mov dh,hexcode[bx]
141q1621b ; mov bl,ah
141q1621b ; mov dl,hexcode[bx]
141q1621b ; ret
hex_asc endp
main:
141q1621b ; mov bx,80h
141q1621b ; mov cl,[bx]
141q1621b ; mov ax,@data
141q1621b ; mov ds,ax
141q1621b ; mov al,cl
141q1621b ; call hex_asc
141q1621b ; mov len,dh
141q1621b ; mov len+1,dl
141q1621b ; lea dx,msg
141q1621b ; mov ah,09h
141q1621b ; int 21h
141q1621b ; mov ah,62h ;returns with
bx=segment address of PSP
141q1621b ; int 21h
141q1621b ; mov ds,bx
141q1621b ; mov dx,81h ;[starting
from 81h in the PSP the cmd line parameters
141q1621b ; mov bx,dx ; are
stored] | bx=81h or bl=81h |
141q1621b ; add bl,cl
;81h+(length of cmd line parameters)
141q1621b ; mov byte ptr[bx],'$' ;mov '$'
at the end of cmd line parameters
141q1621b ; mov ah,09h
141q1621b ; int
21h 141q1621b ; ;displays the cmd line parameters pointed by
dx
141q1621b ; mov ah,4ch ;exit
141q1621b ; int 21h
end main
|