Lab Programs 8086

37
8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE) Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore 1 ;pgm 1a ;binary search .model small .stack .data m1 db ’Search successful $’ m2 db ’search fails $’ list dw 0010h,0130h,0258h,1897h,3876h,6789h,0a765h,0ce65h n dw ($-list)/2 key dw ? .code mov ax,@data ; initialize DS register mov ds,ax mov si,0 mov di,n ; take the count dec di add di,di bak: cmp si,di ja loc3 mov bx,si add bx,di shr bx,1 and bx,0fffe h mov ax,list[bx] cmp ax,key je loc1 jb loc2 mov di,bx sub di,2 jmp bak loc2: mov si,bx add si,2 jmp bak loc3: lea dx,m2 jmp ahead loc1: lea dx,m1 shr bx,1 ahead: mov ah,9 int 21h mov ah,4ch int 21h END ; Prog 1a (Alternative prog)

description

aieee

Transcript of Lab Programs 8086

Page 1: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

1

;pgm 1a

;binary search

.model small .stack .data m1 db 'Search successful $' m2 db 'search fails $' list dw 0010h,0130h,0258h,1897h,3876h,6789h,0a765h,0ce65h n dw ($-list)/2 key dw ? .code mov ax,@data ; initialize DS register mov ds,ax mov si,0 mov di,n ; take the count dec di add di,di bak: cmp si,di ja loc3 mov bx,si add bx,di shr bx,1 and bx,0fffe h mov ax,list[bx] cmp ax,key je loc1 jb loc2 mov di,bx sub di,2 jmp bak loc2: mov si,bx add si,2 jmp bak loc3: lea dx,m2 jmp ahead loc1: lea dx,m1 shr bx,1 ahead: mov ah,9 int 21h mov ah,4ch int 21h END

; Prog 1a (Alternative prog)

id17705171 pdfMachine by Broadgun Software - a great PDF writer! - a great PDF creator! - http://www.pdfmachine.com http://www.broadgun.com

Page 2: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

2

.MODEL SMALL

.STACK

.data msg1 db 'search fails $' msg2 db 'search successful. position is:$' array dw 00ffh,01feh,23fdh,45fch,0abfbh,0bbfah,0cdf0h,0eeeeh length dw ($-array)/2 result db ? key dw 0eeeeh .code mov ax,@data ;initialize the data segment mov ds,ax mov bx, 1 mov dx, length mov cx, key again:cmp bx, dx ja fail mov ax, bx add ax, dx shr ax, 1 mov si, ax dec si add si, si cmp cx, array[si] jae big dec ax mov dx, ax jmp again big: je success inc ax mov bx, ax jmp again success:mov result, al lea dx,msg2 jmp display fail: lea dx, msg1 display: mov ah,9 int 21h exit: mov ah,4ch ;terminate the program normally INT 21H END

; 2a Reads a string from the keyboard and display it in the next line using

macros written in two different files. Use them in your source file include r.mac ; To include the macros

Page 3: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

3

include w.mac .model small .stack .data str db 50 dup(?) msg1 db 'Enter the string: $' msg2 db 10,13,'String is: $' .code mov ax,@data mov ds,ax mov dx, offset msg1 ;Display the message mov ah,09h int 21h mov si,0h ;Read the string using macro bak1: readch str[si] cmp str[si],13 je next inc si jmp bak1 next: lea dx, msg2 ;Display the message mov ah,09h int 21h mov dl,10 ; insert new line mov ah,2 int 21h mov dl,13 mov ah,2 int 21h mov si,0h bak2: writech str[si] ;Display the string using macro cmp str[si],13 je exit inc si jmp bak2 exit: mov ah, 4ch int 21h end

;pgm 3a

;bubble sort : ascending & descending

Algorithm

Page 4: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

4

Initialize the data in the memory.

Initialize the size of data.

Initialize the DS register.

Initialize BX with L.

Copy the contents of BX to CX register.

Load the first element into AL and use a pointing to the next element

Compare this element with the next element.

If AL is greater exchange the two elements in memory.

Increment the points and decrement CX register.

Has all the element compared in the array (i.e., Is CX=0?)

No, go to step 6.

Yes, decrement BX, if BX content is not zero go to step 5.

If BX is zero terminate the program. .MODEL SMALL .STACK .data arr db 1234h, 6548h, 4dach, 978dh, 38dch len dw $-a ; ;calculate the size of numbers .code mov ax,@data ;initialize the data segment mov ds,ax mov bx, len ;the number of data byte is initialized in bx register dec bx ;load bx with (n-1) nxtpass: mov cx, bx ;save the count in cx register mov si,0 ;initialize the pointer nxtcomp: mov al, arr[si] ;load the data in to al pointed by si inc si ;increment the point cmp al, arr[si] ;is the content of al less than that of si pointed data? jb next ;yes, go to next xchg al, arr[si] ;no, exchange two data in memory mov arr[si-1], al ;repeat till the end of the memory next: loop nxtcomp ;has all the comparison over in this pass? ;no, go to nxtcomp to continue dec bx ;has all the passes completed? jnz nxtpass ;no, go to nxtpass mov ah, 4ch ;terminate the program int 21h end (Bubble sort: alternative program) .model small .stack .data list db 3, 4, 2, 1, 5 n db $-list order equ 1

Page 5: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

5

.code mov ax,@data mov ds,ax mov bx,n dec bx nxtpass: mov cx,bx mov si,0 mxpcmp: mov al,list[si] inc si cmp al,list[si] if order eq 0 jb next else ja next endif xchg al,list[si] mov list[si-1],al next: loop nxtcmp dec bx jnz nxtpass mov ah,4ch int 21h end &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ;4a Program to display the ASCII value of the key pressed

.model small .stack .data msg db 'Enter a Key : $' .code mov ax,@data mov ds,ax mov ah,00h ; Select Video mode and clear screen using BIOS interrupt mov al,03h int 10h mov dx, offset msg ; Display message to accept a key from keyboard mov ah,09h int 21h mov ah,01h ; Read the key int 21h mov bl,al ; Save the ASCII value in BL register mov ah,02h ; Set the cursor using BIOS interrupt

Page 6: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

6

mov bh,00h mov dh,12 mov dl,40 int 10h and al,0f0h ; Mask the lower nibble of the ASCII value mov cl,04 shr al,cl call disp ; Display the digit using procedure mov al,bl ; Get the ASCII value saved in BL and al,0fh ; Mask the higher nibble of the ASCII value call disp ; Display the digit mov ah,4ch ; Terminate the program int 21h disp proc ; Procedure to convert the number to its ASCII cmp al,0ah ; and dsiplay the digit jb skip add al,07h skip: add al,30h mov dl,al mov ah,02h int 21h ret disp endp end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ;5a To reverse a given string and check whether it is a palindrome or not

.model small .stack .data str1 db 'nitin$' ; Given string len dw ($-str1) ; Length of the string taken from Location counter value ($) str2 db 50 dup(?) ; String to hold the reversed string msg1 db 'Palindrome$' ; Messages msg2 db 'Not Palindrome$' .code mov ax,@data mov ds,ax mov es,ax ;To use string instructions ES need to be initialized mov di,0h ;Index to Reverse string mov si,len ;Index to the last character of the string

Page 7: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

7

sub si,2 bak: mov al,str1[si] ;Reverse the string mov str2[di],al inc di dec si je next jmp bak next: mov al,str1[si] mov str2[di],al inc di mov str2[di],'$' ; Append $ to the end of the string cld ; Set autoincrement mode mov cx,len ; String length is set as the counter lea si,str1 lea di,str2 repe cmpsb ; Compare the two strings jz pal ; If they are equal go to display palindrome mov dx,offset msg2 ; If the strings are not equal display Not palindrome mov ah,09h int 21h jmp exit pal: mov dx,offset msg1 ; To display palindrome mov ah,09h int 21h exit: mov ah,4ch int 21h end (5A alternative program without using �String� instructions )

Data SEGMENT Msg1 db 10, 13, �Enter the string: � , �$� Msg2 db 0ah, 0dh, � It is a palindrome: $� Msg3 db 10, 13, � It is not a palindrome� , �$� Data ENDS Print MACRO msg ; macro definition to display ASCII string mov dx, OFFSET msg mov ah, 09h Int 21h ENDM ; end of macro definition Code SEGMENT ASSUME CS: Code, DS: Data Start: mov ax, Data ; initialize data segment register mov ds, ax

Page 8: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

8

mov si, 2000h ; pointer to memory to store input characters mov bx, 0000h ; bx hold the no of characters in the string Print Msg1 ; invoke macro to display the message UP: mov ah, 01h ; read string character by character int 21h ; and store in memory buffer pointed by si mov [si], al cmp al, 0dh jz NEXT inc bx jmp UP NEXT: dec bx lea di, [si] [bx] ; point di to the end of the string GO: mov al, ds:[si] ; compare first and last bytes of the string cmp al, ds:[di] ; if not equal, not a palindrome jnz L1 ; if equal, check other characters inc si dec di cmp si, di jae L2 jmp GO L1: Print Msg3 ; invoke macro to display �not palindrome� jmp STOP L2: Print Msg2 ; invoke macro to display � is a palindrome� STOP: mov ax, 4c00h ; terminate the program int 21h Code ENDS END Start

;6a To Read two strings and store them in locations STR1 and STR2 and to

; check whether they are equal or not equal and display appropriate

messages and length of the messages

; also display the length of the stored strings .model small .stack .data str1 db 257 dup(?) str2 db 257 dup(?) msg1 db 10,13,'Equal$' ; Messages msg2 db 10,13,'Not Equal$' msg3 db 10,13,'Enter string1 :$' msg4 db 10,13,'Enter string2 :$' msg5 db 10,13,'Length of string1 = $' msg6 db 10,13,'Length of string2 = $'

Page 9: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

9

.code mov ax,@data mov ds,ax mov es,ax mov dx,offset msg3 call disp mov str1,255 ;To read a string1 mov dx,offset str1 mov ah,0ah int 21h mov dx,offset msg4 call disp mov str2,255 ;To read string2 mov dx,offset str2 mov ah,0ah int 21h mov dx,offset msg5 ;To display the string1 length call disp mov dl,str1[1] add dl,30h mov ah,02h int 21h mov dx,offset msg6 ;To display the string2 length call disp mov dl,str2[1] add dl,30h mov ah,02h int 21h mov al,str1[1] ;Compare the string lengths cmp al,str2[1] jne noteq ;If they are not equal display 'not equal' mov ch,00h mov cl,str1[1] ;Cx contains the length of the string cld lea si,str1 lea di,str2 repe cmpsb ;Compare the strings jnz noteq ;If they are not equal display 'not equal' mov dx,offset msg1 ;If they are equal display 'Equal' call disp jmp exit noteq: mov dx,offset msg2 ;Display 'Not Equal' call disp

Page 10: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

10

exit: mov ah,4ch int 21h disp proc ;Display procedure mov ah,09h int 21h ret disp endp end &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ; 7a To read a name from the keyboard and display it at a specified location

; on the screen in front of the message What is your name?

readch macro c ; macro definition to read a character from keyboard mov ah,01h int 21h mov c,al endm writech macro c ; macro definition to write a character on screen mov dl,c mov ah,02h int 21h endm .model small .stack .data str db 20 dup(?) msg1 db 'Enter ur name: $' msg2 db 'What is Your name? $' .code mov ax, @data mov ds, ax mov ah,00h ; set video mode and clear screen mov al,03h int 10h LEA DX msg1 ;Display message call disp mov si,0h ;Read the string using macro bak1: readch str[si] cmp str[si],13 je next inc si jmp bak1 next: mov ah,02h ;Set the cursor

Page 11: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

11

mov bh,00h mov dh,12 mov dl,30 int 10h mov dx,offset msg2 ;Display the message call disp mov si,0h ;Display the string using macro bak2: writech str[si] cmp str[si],13 je exit inc si jmp bak2 exit: mov ah,4ch int 21h disp proc ;Procedure to display the message mov ah,09h int 21h ret disp endp end (7a - Alternative program) readstr macro loc mov ah,01h int 21h mov loc,al endm .model small .stack .data m1 db 'what is your name?: $' len db ($-m1) arr db 40h dup(?) .code mov ax,@data mov ds,ax mov si,00 L1: readstr arr[si] ; read your name character by character inc si cmp al,13 ; until ENTER key is pressed & store it in memory jnz L1 mov arr[si], '$' mov ah,00 ; set video mode & clear screen using BIOS function mov al,3 int 10h

Page 12: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

12

mov ah,2 ; set cursor position using BIOS function mov bh,0 mov dh,2 mov dl,20 int 10h lea dx,m1 ; display �what is your name� mov ah,09h int 21h mov si,0 lea dx,arr[si] ; display the name mov ah,09h int 21h mov ah,4ch int 21h end ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ;pgm 8a

;compute factorial of 'N' using recursive procedure

.model small .stack .data res dw ? n dw ? msg db 'invalid input: $' .code mov ax,@data mov ds,ax mov ax,n call fact ; call the procedure jmp last fact proc cmp ax,0 ; if no = 0 or 1, factorial is 1 jl error je exit cmp ax,1 je exit push ax dec ax call fact ; call 'fact' recursively pop ax mul res mov res,ax ret fact endp

Page 13: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

13

error: lea dx, msg ; display error message mov ah, 09h int 21h jmp last exit: mov res,1 ret last: mov ah,4ch int 21h end

(Factorial: Alternative Program) model small .stack .data num db ? result dw ? .code mov ax,@data ;initialize the ds register mov ds,ax mov ax,01 ;initialize the result as 01 if the number is 0 mov cx,num ;initialize the number cmp cx,0 ;check whether number is 0 je exit ;if yes, go to terminate the program mov bx,cx ;save the number in bx call fact ;call the factorial function exit: mov result,ax ;save the factorial in result mov ah,4ch ;terminate the program int 21h fact proc near ;function for factorial cmp bx,01 ;is bx content=01? jz next ;if yes, go to initialize ax push bx ;if no, save intermediate value on stack dec bx ;reduce bx value call fact pop bx ;pop the value stored and mul bx ;multiply with ax register ret ;return to the called program next: mov ax,01 ;initialize ax register 01 ret ;return to the called program fact endp ;end of the factorial function end &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

Page 14: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

14

;9A) COMPUTE nCr USING RECURSION PROCEDURE. ASSUME THAT 'n' AND

'r' ARE NON NEGATIVE INTEGER NUMBERS.

.model small

.stack .data n dw 5 r dw 4 ncr dw 1 msg db "the ncr is: :$" .code mov ax,@data mov ds,ax mov bx,n inc bx mov cx,r call ncp lea dx,msg mov ah,09h int 21h mov ax,ncr call ascii mov ah,4ch int 21h ncp proc near cmp cx,00h je l1 push cx dec cx call ncp mov ax,bx pop cx sub ax,cx mul ncr div cx mov ncr,ax l1: ret ncp endp ascii proc near mov bx,ax and ax,0f000h mov al,ah mov cl,4 shr al,cl call disp mov ax,bx and ax,0f00h mov al,ah shr al,cl

Page 15: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

15

call disp mov ax,bx and al,0f0h shr al,cl call disp mov ax,bx and al,0fh call disp ret ascii endp disp proc near cmp al,0ah jb skip add al,7 skip: add al,30h mov dl,al mov ah,02 int 21h ret end (9A � Alternative program for ncr )

;9a ;compute ncr .model small .stack .data n dw 6 r dw 3 ncr dw 1 dup(0) .code mov ax,@data mov ds,ax mov ax,n mov bx,r call ncppro mov ax,4ch int 21h ncrpro proc cmp bx,ax je res1 cmp bx,00 je res1 cmp bx,01 je resn dec ax cmp bx,ax je incncr push ax

Page 16: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

16

push bx call ncrpro pop bx pop ax dec bx push ax push bx call ncrpro pop bx pop ax ret res1: inc ncr ret resn: inc ncr incncr: add ncr,ax ret ncrpro endp end @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;10a To check whether a sub-string is present in main string or not.

assume cs:code, ds:data data segment msg db "Life is Beautiful" lms dw lms-msg str db "Life" lss dw lss-str msg1 db "substring found$" msg2 db "substring not found$" data ends code segment start: mov ax,data mov ds,ax mov es,ax mov dl,lms lea di,msg search: mov bx,di lea si,str mov cx,lss cld

Page 17: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

17

rep cmpsb jz success inc bx mov di,bx dec dl jnz search jmp failure success: lea dx,msg1 mov ah,09h int 21h jmp exit failure: lea dx,msg2 mov ah,09h int 21h exit: mov ah,4ch int 21h code ends end start

(10a � Alternative program)

.model small

.stack dis_msg macro p1 ; macro definition to display message mov ah,09h lea dx, p1 int 21h ; using dos function endm read macro p1 ; macro definition for reading string from keyboard mov ah,0ah lea dx,p1 int 21h endm .data cr equ 0dh lf equ 0ah m3 db cr,lf,'enter the main string:$' m4 db cr,lf,'enter the sub string:$' m1 db cr,lf,'the sub string is found:$' m2 db cr,lf,'the sub string is not found:$' z db 50h db 0h db 50h dup(?)

Page 18: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

18

y db 50h db 0h db 50h dup(?) .code mov ax,@data mov ds,ax dis_msg m3 read z dis_msg m4 read y mov cl,z+1 lea si,z add si,2h loop1: push si lea di,y add di,2h mov ch,y+1 mov bh,00h loop3: mov al,[si] cmp al,[di] jne loop2 inc si inc di inc bh cmp bh,y+1 je mes1 dec ch jnz loop3 loop2: pop si inc si dec cl cmp cl,00h jne loop1 dis_msg m2 jmp ter mes1: dis_msg m1 ter: mov ah,4ch int 21h end @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;11a

; Fibonacci series in Decimal

Page 19: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

19

.model samll

.stack

.data arr db 10 dup(?) num db 10 .code mov ax,@data mov ds,ax mov si,0000h mov al,00 mov bl,01 mov arr[si],al inc si mov arr[si],bl mov cl,num mov ah,00 repeat: add al,bl daa inc si mov arr[si],al dec cl xchg al,bl jnz repeat mov ah,4ch int 21h end (Alternative program to generate first n fibonacci numbers in Hex) .model small .stack .data arr db 20 dup(?) count db 0ah msg db "the fibonacci series : ",13,10,"$" .code mov ax,@data mov ds,ax mov si,00h mov ax,00h mov arr[si],al inc si mov bx,01h mov arr[si],bl inc si mov ch,count sub ch,02h back: add ax,bx mov arr[si],al inc si xchg ax,bx

Page 20: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

20

dec ch jnz back lea dx,msg mov ah,9 int 21h call disp mov ah,4ch int 21h disp proc near mov si,00h mov ch,count loop1: mov al,arr[si] mov ah,0 aam add ax,3030h mov dl,ah mov ah,2 push ax int 21h pop ax mov dl,al int 21h mov ah,2 mov dl, ' ' int 21h inc si dec ch jnz loop1 ret disp endp END &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

;12a -Assembly Language program to perform the following: Read the

current time from the system (format it as HH:MM:SS and display it on the

video screen

.model small

.stack

.code mov ah,2ch ;read system time using DOS function ; CH � hours, CL- minutes, DH - seconds mov al,ch ;transfer hours value from ch to al call disp ;display the contents of dl mov dl,':' ;display ':' between hours and minutes mov ah,2 int 21h mov al,cl ;transfer minutes value to dl

Page 21: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

21

call disp mov ah,4ch ;terminate the program normally int 21h disp proc near ;display the time by converting to ASCII aam add ax,3030h mov bx,ax mov dl,bh mov ah,2 int 21h mov dl,bl mov ah,2 int 21h ret ;return the main program disp endp end (Alternative Program for Real time clock)

.model small .stack .data msg db 'the current time is : $' .code mov ax,@data mov ds,ax ;initialise ds lea dx,msg ;display the message mov ah,09h int 21h mov ah,2ch ;dos function to get the current system time int 21h ; CH � hours, CL- minutes, DH - seconds mov al,ch aam ;bcd adjust after multiply mov bx,ax call display mov dl,':' ; display the char ':' mov ah,02h int 21h mov al,cl aam ;bcd adjust after multiply mov bx,ax call display

Page 22: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

22

mov ah,4ch ;dos function to terminate int 21h display: ;function to display a character on mov dl,bh ;the standard o/p device add dl,30h mov ah,02h int 21h mov dl,bl add dl,30h mov ah,02h int 21h ret end @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ; 13a. Program to simulate a Decimal Up-Counter to display 00-99

assume cs:code code segment start: mov cx,100d mov bl,00 next_digit: mov al,bl aam add ax,3030h mov dl,ah mov ah,2 push ax int 21h pop ax mov dl,al mov ah,2 int 21h mov dl,0dh mov ah,2 int 21h call delay inc bl loop next_digit mov ah,4ch int 21h delay proc mov si,02202h

Page 23: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

23

l1:mov di,0ffffh l2:dec di jnz l2 dec si jnz l1 ret delay endp code ends end start

(Alternative program Decimal up counter)

.model small

.stack

.data .code mov ax,@data mov ds,ax start: mov al,30h ;al contains first digit loop2: mov dl,al mov ah,02h int 21h push ax mov bl,030h ;bl contains second digit loop1: mov ah,02h mov dl,bl int 21h inc bl ;increment second digit call delay mov ah,03h ;get current cursor position int 10h mov ah,02h ;set cursor to next column mov dl,01h int 10h cmp bl,039h jle loop1 ;loop second digit(0-9) mov ah,02h ;set cursor position to previous column mov dl,00h int 10h pop ax inc al ;increment 1st digit cmp al,039h jle loop2 ;loop 1st digit(0-9)

Page 24: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

24

mov ah,4ch int 21h delay proc push cx push bx mov cx,0fffh n3: mov bx,0ffffh n4: dec bx jnz n4 loop n3 pop bx pop cx ret delay endp end start @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;14a

; Read cursor to co-ordinates and move the cursor

readkb macro m lea dx,m mov ax,09 int 21h mov ah,01h int 21h sub al,30h endm .model small .stack .data x db 1 y db 1 m1 db 'Enter x co-ordinate: $' m2 db 'Enter y co-ordinate: $' .code mov ax,@data mov ds,ax mov ah,0 mov al,3 int 10h readkb m1 mov x,al readkb m2

Page 25: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

25

mov y, al mov ah,2 mov bh,0 mov dh,x mov dl,y int 10h mov ah,01h int 10h mov ah,4ch int 21h end

(14a - Alternate Program )

.model small

.stack .data xmsg db 13,10,'enter value of x co-ordinates:','$' x db ? ymsg db 13,10,'enter value of y co-ordinates:','$' y db ? .code mov ax,@data mov ds,ax mov dx,offset xmsg ; to read bcd co=ordinates call read_bcd mov x,bh mov dx,offset ymsg call read_bcd mov y,bh mov ah,0 ;to clear the screen mov al,03 int 10h mov ah,02h ;to set cursor position mov dh,x mov dl,y mov bh,0 int 10h

Page 26: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

26

mov dl,'-' mov ah,06h int 21h mov ah,4ch int 21h read_bcd proc mov ah,09h int 21h mov ah,01h ;first digit int 21h mov bh,al mov ah,01h ;second digit int 21h mov bl,al mov cl,4h sub bh,30h ;to convert from ascii to bcd sub bl,30h shl bh,cl or bh,bl ret read_bcd endp end @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;15a - Program to create a file (input file) and to delete an

existing file.

assume cs:code, ds:data data segment fname2 db "krishna.txt" msg1 db "File created successfully$" fname1 db "lk.dat" msg2 db "File deleted successfully$" .code mov ax,@data mov ds,ax mov ah,3ch mov cx,00 lea dx,fname2 int 21h jc next

Page 27: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

27

disp msg1 next: mov ah,41h lea dx,fname1 int 21h jc finish disp msg2 finish: mov ah,4ch int 21h code ends end start

(Alternative Program)

;15a1) program to create a file (input file) .model small .stack .data fname db 'c:\masm\file.asm $',00h success db 'file is created successfully$' failure db 'error during file creation$' .code mov ax,@data mov ds,ax mov cx,20h lea dx,fname mov ah,3ch int 21h jc fail lea dx,success jmp exit fail: lea dx,failure exit: mov ah,09h int 21h mov ah,4ch int 21h end ;15a2 ;delete a file dispmsg macro m lea dx,m mov ah,09

Page 28: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

28

int 21h endm .model small .stack .data FN db 'c:\sce.txt $' m1 db 'Deletion successful $' m2 db 'Deletion Fails $' .code mov ax,@data mov ds,ax mov ah,41h mov cx,20h lea dx,FN int 21h jc error dispmsg m1 jmp stop error: dispmsg m2 stop: mov ah,4ch int 21h end @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Write an ALP to transfer a block of N words from one array to another considering (i) Non-

overlapping and (ii) Overlapping conditions

; (i) Non overlapping

mov si, 2000h ; first array mov bx, 3000h ; second array mov cx, N ; count (length of the array) UP: mov ax, WORDPTR [si] mov WORDPTR [bx], ax add bx, 02h add si, 02h dec cx jnz UP Mov ah, 4ch, Int 21h ; terminate the program ; (ii) Overlapping case

Page 29: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

29

mov cx, N ; length of the array mov dx, cx mov si, 3550h mov bx, 3555h cmp si, bx jc DOWN ; if source addr below dest addr, Bottom-to-Bottom transfer ; otherwise, Top-to-Top transfer UP1: mov ax, WORDPTR [si] mov WORDPTR [bx], ax add bx, 02h add si, 02h dec cx jnz UP1 jmp STOP DOWN: add bx, cx-1 add si, cx-1 UP2: mov ax, WORDPTR [si] mov WORDPTR [bx], ax sub bx, 02h sub si, 02h dec cx jnz UP2 STOP: mov ah, 4ch ; terminate the program int 21h @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Write an ALP for 2-digit BCD to ASCII conversion

Ex: 56 d = 35 36 (each digit must be converted) data SEGMENT NUM db 56 ; BCD number stored at memory NUM data ENDS code SEGMENT ASSUME cs:code, ds:data mov al, num ; take number into AL mov bl, al ; save number to bl and al, 0fh ; convert lower digit to ASCII add al, 30 mov NUM+1, al mov cl, 04 ; get unpacked higher digit rol bl, cl or bl, 30h mov NUM+2, al

mov ax, 4c00h ; terminate the program int 21h Code ENDS END

Page 30: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

30

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Develop and execute an assembly language program to find the LCM of two 16 bit unsigned

integers. Explanation: The LCM of two numbers is found by dividing the first number by the second number. If the remainder is zero, then the first number is the LCM. If there is a remainder, the first number is added to itself to get a new number. Again divide the new number by the second number. If there is no remainder, then the new number is the LCM. If there is a remainder, the new number is added to the first number and once again this number becomes the new number. The process is continued till the remainder becomes zero. Example: First No = 25 Second No = 15 Iteration Operation Reminder New Number 1 25 % 15 10 25+25=50 2 50 % 15 5 50+25=75 3 75 % 15 0 Therefore LCM is 75. Algorithm

Initialize word data in the data memory locations.

Fetch the 16-bit data into AX and BX from address location N and N +2.

Initialize DX to 0000H.

Save both AX and DX on top of the stack.

Divide the A--DX by contents of BX.

Is the remainder Zero?

Yes go to step 10.

No, restore the data from top of the stack.

Add the contents of N to AX_DX and go to step4.

The result is popped from the top of the stack (First Higher order 16-bits and these lower order 16-bits are popped).

Terminate the program. .model small ; both code & data segment <=64k each. .stack ;stack initialized .data n dw 000fh, 0005h ;initialize data memory locations for the operands lcm dw 2 dup(?) ;the calculated result is stored in lcm. .code ;the code part of the program starts here mov ax,@data ;initialize data segment mov ds,ax mov dx,0 ;clear dx register mov ax,n ;load the first number mov bx,n+2 ;load the second number back: push ax ;save both numbers on top of the stack push dx div bx ;divide first number by the second number

Page 31: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

31

cmp dx,0 ;is there is a number ? je exit ;no, terminate the program pop dx ;yes, pop the data stored pop ax add ax,n ;add the first number to the contents of ax jnc next ;if the result is greater then 16-bits inc dx ;increment dx ;register next: jmp back ;respect till the remainder is zero exit: pop lcm+2 ;pop the lcm value from the top of stack pop lcm mov ah, 4ch ;terminate the program int 21h end ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Write an assembly level program to find the GCD of two 16-bit unsigned

integers.

Explanation: GCD of two numbers are performed by dividing the greater number by smaller number till the remainder is zero. If it is zero, the divisor is the GCD. If not, the remainder and the devisor of the previous division are the new set of two numbers. The process is repeated by dividing greater of the two numbers by the smaller number till the remainder is zero. Example : First No.= 90 Second No.=120 Iteration Operation Remainder 1 120 % 90 30 2 90 % 30 0 Hence the GCD is 30. Algorithm

Initialize the data memory with data and the data segment register with appropriate address.

Load AX and BX registers with the operands (N1 and N2).

Are the two numbers N1 and N2 equal? If yes, go to step 10.

Is N1 greater than N2? if yes, go to step 6.

Exchange AX and BX register contents such that AX contains the bigger number.

Initialize DX register with 00H.

Perform divide operation (Contents of AX/contents BX).

If there is no remainder go to step 10.

Move the remainder into AX register and go to step 4.

Save the contents of BX as GCD.

Terminate the program normally. .model small ;both code & data segment <=64k each .stack ;stack initialized .data

Page 32: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

32

n1 dw 000fh ;initializing n1 with 000fh n2 dw 0005h ;initializing n2 as a data word & giving it the value 0005h gcd dw ? ;the result will be stored in gcd .code mov ax, @data ;initialize the data segment mov ds, ax ;initialize the ds register mov ax, n1 ;n1 is initialized mov bx, n2 ;n2 is initialized back:cmp ax, bx ;are they equal? je exit ;if yes, save the gcd jb intrg ;if no, is ax<bx? if yes, interchange the numbers divn: mov dx, 0 div bx ;check whether ax is divisible by bx cmp dx, 0 je exit ;if yes, save gcd mov ax, dx ;move the remainder as n1 data jmp back ;repeat the procedure intrg: xchg ax, bx ;load the higher number is ax and jmp divn ;lower number in dx and continue exit: mov gcd, bx ;save the gcd number mov ah, 4ch ;terminate the program int 21h end $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

Develop an ALP to sort a given set of 16 bit unsigned integers into

ascending order �using insertion sort� algorithm

Explanation: We arrange the given set of numbers 7800, 4800, 9200, 2311 in ascending order using insertion sort. We treat the first term 7800 as sorted. Now we insert the second term 4800 in the correct position, so that two elements are sorted. The sorted elements are 4800, 7800. Now we insert the third element 9200 in the corresponding position, so that three elements are sorted. The three-sorted elements are 4800, 7800, 9200. Finally, we insert the last term 2311, 4800, 7800, 9200. Algorithm

Initialize data in the data memory location.

Initialize to calculate the number of integers in location size 1.

Initialize DS register with data memory.

Initialize SI register with the count 2(to sort first two integers)

Check whether all the numbers up to the count indicated by CX register is sorted

No, sort them by comparing and inserting it in the right position.

Yes, include the next number and increment the count in CX register.

Have all the numbers include and sorted?

No, go to step 5.

Yes, terminate the program. .model small

Page 33: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

33

.stack

.data a dw 0004h, 0003h, 0001h, 0002h l dw ($-a)/2 .code mov ax,@data ;initialize the ax register mov ds, ax ;initialize ax value to ds register mov cx, 02h ;initialize l to cx register up: mov dx, cx dec cx mov si, dx add si, si mov ax, a[si] go: cmp a[si-2], ax jbe next mov di, a[si-2] mov a[si], di dec si dec si dec dx jnz go next: mov a[si], ax inc cx cmp cx, l jbe up mov ah,4ch ; terminate the program int 21h end &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

Password authentication program

title program to check password and give access .model small .data nam db 'KRISHNA$' pass db 50 db ? db 10 dup(?) msg1 db 10,13,"enter the password: $",10,13 msg2 db 10,13,"password valid : Congrats!!!! $",10,13 msg3 db 10,13,"password invalid, Sorry Try again ## $",10,13 print macro msg ; macro definition to print a string on screen lea dx,msg mov ah,09

Page 34: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

34

int 21h endm .code start: mov ax,@data mov ds,ax mov es,ax xor ax,ax print msg1 lea dx,pass mov ah,0ah ; read password from keyboard int 21h mov di,dx inc di mov cl, byte ptr[di] ; length of the string in cl register mov ch,00 inc di lea si, nam cld back: cmpsb jne xx loop back print msg2 jmp xxy xx: print msg3 xxy: mov ah,4ch int 21h end start @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

ALP to REVERSE A STRING using �STRING� instructions

.model small

.stack

.data count equ 05h x db �aruna�,�$� res db 5 dup (?),�$� .code mov ax,@data mov ds, ax mov es, ax lea si, x lea di, res mov cx, count add di, cx dec di back: cld

Page 35: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

35

lodsb std stosb loop back mov ah, 09h lea dx, res int 21h mov ah, 4ch int 21h end @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Read a 2-digit number from the Keyboard. Convert it to equivalent packed

HEX number.

Ex: When you enter say, 25 thro� keyboard, it will be taken as two ASCII values 32 and 35. From 32 and 35, you have to get back 25. Data SEGMENT msg db 10, 13, �Enter the number : �, 10, 13 , �$� Data ENDS Print MACRO str ; macro definition lea dx, str mov ah, 09h int 21h ENDM ; end of macro definition Code SEGMENT ASSUME CS: Code, DS: Data mov ax, Data ; initialize data segment register mov ds, ax Print msg mov ah, 01 ; read the first digit (character)of the number, say 2. int 21h call ASCTOHEX ; use a subroutine to convert the ASCII value ; so, get 02 from 32 mov cl, 04 rol al, cl ; make 02 into 20 by swapping the nibbles mov cl, al mov ah, 01h ; read the next digit of the number, say 5. int 21h call ASCTOHEX ; get 05 from 35 or al, cl ; get 25 by ORing 20 and 05

Page 36: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

36

jmp STOP ASCTOHEX: ; after pressing the key, its ASCII value will be in AL register sub al, 30h ; unpack the digit. i.e., subtract 30 from ASCII value cmp al, 09h ; if the digit is >9, then subtract 37 to get HEX digit jnae L1 sub al, 07h L1: ret STOP: mov ah, 4ch int 21h Code ENDS END

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Create an Assembly language source file containing two PUBLIC

Procedures called READKB and ECHO. READKB reads from keyboard using

INT 21H(Function 06)leaving the ASCII codes of key read in AL. procedure

ECHO displays the ASCII character in AL on the screen using INT 21H.

Develop an Assembly level language program which uses the READKB and

ECHO procedure once. This is the main program which calls the READKB

procedure and ECHO procedure.

Here there are three programs. Assembling and linking in the following manner can execute this program. Assemble all the three programs separately. Link the three programs together by using the command. Link<file1.obj>+<file2.obj>+<file3.obj> An .EXE file is generated by the first file name file1.exe. (This is the first program called <file1.asm>) .model small .stack .code extrn readkb:far ;these two procedures are not available in this file extrn echo:far mov cx,10 ;initialize the number of data character required. back:call readkb ;call the procedure to read the characters call echo ;call the procedure to display the character loop back ;call the above procedure 10 times mov ah,4ch ;terminate the program normally int 21h end Program to Display (ECHO) on the screen using INT 21H(Function 06H). (This is the second program called <file2.asm>) .model small

Page 37: Lab Programs 8086

8086 LAB PROGRAMS & Additional Programs (IV SEM CSE/ISE)

Compiled by: L. Krishnananda, Asst Professor, REVA ITM, Bangalore

37

.stack

.code public echo ;echo is available externally to all programs echo proc ;name of the procedure mov dl,al mov ah,06h ;function to display a character on crt screen int 21h ret ;return to the calling program echo endp end program to read the data from Keyboard using INT 21H(Function 06). (This is the third program called <file3.asm>) .model small .stack .code public readkb ;this procedure is made public readkb proc far mov ah,06h ;call interrupt to read the key pressed mov dl,0ffh int 21h jz readkb ;no, go to check again ret ;yes, return the main program readkb endp ;return the called program end