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