8086 Assembly Language Programs |
;1 Write an alp to sort in ascending order using bubble sort algorithm
; a given set of byte sized unsigned numbers in memory.The sorted
; elements should replace the original unsorted elements in
memory.
name bubblesort
page 60,80
title ascending order using bubble sort
.model small
.stack 64
.data
a db 34h,78h,56h,47h
si_ze dw $-a ;si_ze=no of elements
.code
bubsort:
mov ax,@data
mov ds,ax
mov bx,si_ze
dec bx ;bx=no of
passes needed to complete sorting(n-1)
outlup:
mov cx,bx ;cx=no of comparisions to
be performed in a pass
mov si,0
inlup:
mov al,a[si]
inc si
cmp al,a[si]
jb go_on
xchg al,a[si]
mov a[si-1],al
go_on:
loop inlup ;dec cx,until cx=0
dec bx
jnz outlup
int
3 ;breakpoint interrupt
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.
name selectionsort
page 60,80
title descending order using selection sort
.model small
.stack 64
.data
a db 44h,11h,22h,66h
si_ze dw $-a ;si_ze=4
nc dw
?
;total no of comparisions made
.code
selsort:
mov ax,@data
mov ds,ax
mov dx,si_ze ;in selsort for
n elements to sort we need n-1 passes
dec
dx ;dx=3 no of passes required
mov nc,0
outlup:
mov cx,dx
;cx=no of comparisions to be performed in a pass
mov si,0
mov ah,a[si]
mov bx,si
inlup:
inc si
inc nc
cmp ah,a[si]
jb go_on
mov ah,a[si] ;ah=smallest
element in the vector
mov bx,si
;bx=position of the smallest element
go_on:
loop inlup
;untill cx=0
xchg ah,a[si] ;xchg the last
element pointed by si,with the
mov a[bx],ah ;smallest
element pointed by bx
dec dx
jnz outlup
int
3 ;breakpoint interrupt
align 16
end selsort
;3. Write an 8086 alp to sort in ascending order using
Insertion Sort
; algorithm,a given set of 16 bit unsigned numbers in memory.The
sorted
; elements should replace the original unsorted elements in
memory.
name insertionsort
page 60,80
title ascending order using insertion sort algorithm
.model small
.stack 64
.data
a dw 78h,34h,12h,56h
si_ze dw ($-a)/2 ;si_ze=4(no of elements)
.code
insort:
mov ax,@data
mov ds,ax
mov
cx,2 ;cx=2,insert the second element in the
proper position
outlup:
mov dx,cx
dec dx
;dx=cx-1,max no of comparisions needed to insert element
mov si,dx
add si,si
mov ax,a[si]
inlup:
cmp a[si-2],ax
jbe inlupexit
mov di,a[si-2]
mov a[si],di
dec si
dec si
dec dx
jnz inlup
inlupexit:
mov a[si],ax
inc cx ;inc
cx to insert the next element in proper position
cmp cx,si_ze
jbe outlup
exit:
int 3
;breakpoint interrupt
align 16
end insort
;4. Add/Sub of multiword
name addsub
page 60,80
title 8086 alp for multi word addition/subtraction
.model small
.stack 64
.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(?)
.code
addsub:
mov ax,@data
mov ds,ax
mov
cx,8 ;cx is used as loop counter
mov
bx,7 ;bx contains the offset address of byte in
n1 & n2
clc
addagn:
mov al,n1[bx]
adc al,n2[bx]
mov s[bx],al ;store sum in
s[]
dec bx
loop addagn
mov cx,8
mov bx,7
clc
subagn:
mov al,n1[bx]
sbb al,n2[bx]
mov d[bx],al ;store
difference in d[]
dec bx
loop subagn
int 3
align 16
end addsub
;5. Write an 8086 alp to get the screen width(no of cols)
using BIOS
; interrupt,and calculate the no of rows from the appropriate word
; location in BIOS data area,and clear the screen using BIOS
interrupt.
name clearscreen1
page 60,80
title clear screen using bios interrupt
.model small
.stack 64
.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'
.code
display proc
push ax
push bx
push cx
push dx
lea dx,msg1
;displays msg1
mov ah,09h
int 21h
mov al,rows ;al=no of
rows[25]
mov cl,10h
mov ah,00h
div
cl ;25/10 | al=quotient[2] |
ah=remainder[5]
mov bl,al
;al=2
mov dl,hexcode[bx] ;dl=8 bit ascii code of char
to be displayed[2]
push ax
mov ah,02h
;display char to std o/p dev
int 21h
pop ax
mov bl,ah
;ah=5
mov dl,hexcode[bx] ;dl=8 bit ascii code of char
to be displayed[5]
mov ah,02h
;display char to std o/p dev
int 21h
lea dx,msg2 ;displays
msg2
mov ah,09h
int 21h
mov al,cols ;al=no of
cols[80]
mov cl,10h
mov ah,00h
mov bh,00h
div
cl ;80/10 | al=quotient[8] |
ah=remainder[0]
mov bl,al
;al=8
mov dl,hexcode[bx] ;dl=8 bit ascii code
of char to be displayed[8]
push ax
mov ah,02h
;display char to std o/p dev
int 21h
pop ax
mov bl,ah
;ah=0
mov dl,hexcode[bx] ;dl=8 bit ascii code
of char to be displayed[0]
mov ah,02h
;display char to std o/p dev
int 21h
pop dx
pop cx
pop bx
pop ax
ret
display endp ;end
display procedure
main:
mov ax,@data
mov ds,ax
mov ah,0fh
int 10h
mov cols,ah ;ah=no of
char cols on screen
mov cl,ah
mov ch,0
push ds
lds si,bytes
mov ax,[si] ;ax=total
no of bytes on video page(1b ascii+1b AB)
pop ds
shr
ax,1 ;divides ax by 2 to get total no of chars
on page
div cl
mov rows,al
call display
lea dx,msg3
mov ah,09h ;displays msg3
int 21h
mov ah,01h ;i/p char from std i/p dev &
echo to std o/p dev
int 21h
mov dh,0 ;initialize row coordinate to 0
again:
mov bh,0 ;bh=page 0
mov dl,0 ;initialize column coordinate to 0
mov ah,02h ;set cursor position to (dl,dh)
int 10h
mov bl,0 ;colour(set foregnd & bckgnd of
char with same colour)
mov al,'x' ;char to be displayed
mov ah,09h ;write char at cursor position
int 10h
inc dh ;inc row coordinate by one
position
cmp dh,rows
jb again
mov ah,4ch ;exit
int 21h
end main
;6. GCD of 4 unsigned 16 bit numbers
name gcd
page 60,80
title program to find gcd of 4 unsigned 16 bits numbers
.model small
.stack 64
.data
values dw 0090,0120,4bh,0019h
gcd dw ?
.code
hcf proc
again:
cmp ax,bx
je exit
jb bigbx
divaxbx:
mov dx,0
div bx
cmp dx,0
je exit
mov ax,dx
jmp again
bigbx:
xchg ax,bx
jmp divaxbx
exit:
mov gcd,bx
ret
hcf endp
;Main program
gcd4:
mov ax,@data
mov ds,ax
mov ax,values
mov bx,values+2
call hcf
mov ax,gcd
mov bx,values+4
call hcf
mov ax,gcd
mov bx,values+6
call hcf
int 3
align 16
end gcd4
;7. LCM of 2 16 bit unsigned numbers
name lcm
page 60,80
title program to find lcm of 2 16 bit unsigned numbers
.model small
.stack 64
.data
values dw 0025,0015
lcm dw 2 dup(?)
.code
l_c_m:
mov ax,@data
mov ds,ax
mov dx,0
mov ax,values ;dx_ax=25
mov bx,values+2 ;bx=15
again:
push ax
push dx
div bx
cmp
dx,0 ;remainder of the division is stored in dx
je exit
pop dx
pop ax
add ax,values
jnc noincdx
inc dx
noincdx:
jmp again
exit:
pop lcm+2
pop lcm
int 3
align 16
end l_c_m
;8. Write an 8086 alp to search for a given 8 bit value using
linear search
; in an array of 8 bit numbers.Message should be displayed on crt
; indicating whether the search was a failure or a success.If it
is a
; success case,the position of the element in the array is to be
displayed
name linearsearch
page 60,80
title linear search program
.model small
.stack 64
.data
array db 55h,33h,44h,66h,22h
len dw $-array ;length=5
scrkey equ 33h
asc1 equ (scrkey/10h)+'0' ;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,'$'
.code
lin:
mov ax,@data
mov ds,ax
mov es,ax
cld ;direction flag D=0
mov
di,0 ;di=0,autoincrement di
mov al,scrkey ;al=35
mov cx,len
repne scasb
;scasb==>[al]-[[di]]
jz success
lea dx,failmsg
jmp display
success:
mov bx,di
;bx=position of the scrkey found in array
add bl,'0'
;convert this position into ascii for display purpose
mov result,bl
lea dx,sucmsg
display:
mov ah,09h
int 21h
mov ah,4ch
int 21h
align 16
end lin
;9. Write an 8086 alp to search for a given 16 bit value using
binary search
; in an array of 16 bit numbers,which are in ascending
order.Message
; should be displayed on CRT indicating whether the search was a
failure
; or a success.If it is a success case,the position of the element
in the
; array is to be displayed.
name binarysearch
page 60,80
title binary search program to search a 16 bit value
.model small
.stack 64
.data
cr equ 13
lf equ 10
array dw 1122h,2345h,3344h,4455h,5566h
len dw ($-array)/2 ;length=5
scrkey equ 2345h
asc1 equ (scrkey/1000h)+'0' ;asc1='2'
asc2 equ (scrkey/100h) mod 10h + '0' ;asc2='3'
asc3 equ (scrkey/10h) mod 10h " '0' ;asc3='4'
asc4 equ (scrkey mod 10h) + '0' ;asc4='5'
sucmsg db 'Given Element '
db ' Found at position:'
result db ?,cr,lf,'$'
failmsg db 'Given Element '
db ' Not found',cr,lf,'$'
.code
binscr:
mov ax,@data
mov ds,ax
mov bx,1
mov dx,len ;dx=5
mov cx,scrkey ;cx=2345
again:
cmp bx,dx
ja failure
mov ax,bx
add ax,dx
shr ax,1
mov si,ax
dec si
add si,si
cmp cx,array[si]
jae biger
dec ax
mov dx,ax
jmp again
biger:
je success
inc ax
mov bx,ax
jmp again
success:
add al,'0'
mov result,al
lea dx,sucmsg
jmp display
failure:
lea dx,failmsg
display:
mov ah,09h
int 21h
quit:
mov ah,4ch
int 21h
align 16
end binscr
;10. Using BIOS routine,write an 8086 alp to find memory size
of the PC you
; are using.Using appropriate message,the display should indicate
memory
; size in Kilo bytes using 4 hex digits.Also check the result with
the
; appropriate word in BIOS data area using debug/codeview.
name memorysize
page 60,80
title program to find memory size using int 12h
.model small
.stack 64
.data
msg db 'Memory size in Kilo bytes='
ascres db 4 dup(?),'Hex',0ch,0ah,'$'
res dw ?
hexcode db '0123456789abcdef'
.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 ax,@data
mov ds,ax
int 12h
mov res,ax
mov al,byte ptr res
call hex_asc
mov ascres+2,dh
mov ascres+3,dl
mov al,byte ptr res+1
call hex_asc
mov ascres,dh
mov ascres+1,dl
mov dx,offset msg
mov ah,09h
int 21h
mov ah,4ch
int 21h
align 16
end main
All the above Programs were executed on Microsoft's Macro Assembler Version 5.0
© Copyrights Madhu Sudan Rao G.K