Lab Programs 8086
Lab Programs 8086
;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
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
.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
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
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
mov ah,09h
int 21h
mov dl,10
mov ah,2
int 21h
mov dl,13
mov ah,2
int 21h
mov si,0h
bak2: writech str[si]
cmp str[si],13
je exit
inc si
jmp bak2
;pgm 3a
;bubble sort : ascending & descending
Algorithm
.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
.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
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
int 21h
mov bl,al
mov ah,02h
mov bh,00h
mov dh,12
mov dl,40
int 10h
and al,0f0h
mov cl,04
shr al,cl
call disp
mov al,bl
and al,0fh
call disp
mov ah,4ch
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$'
len dw ($-str1)
str2 db 50 dup(?)
; Given string
; Length of the string taken from Location counter value ($)
; String to hold the reversed string
msg1 db 'Palindrome$'
; Messages
msg2 db 'Not Palindrome$'
.code
mov ax,@data
mov ds,ax
mov es,ax
mov di,0h
mov si,len
sub si,2
bak: mov al,str1[si]
mov str2[di],al
inc di
dec si
je next
jmp bak
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
UP:
NEXT:
GO:
L1:
Print Msg3
jmp STOP
Print Msg2
mov ax, 4c00h
int 21h
L2:
STOP:
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 = $'
.code
mov ax,@data
mov ds,ax
mov es,ax
mov dx,offset msg3
call disp
mov str1,255
mov dx,offset str1
mov ah,0ah
int 21h
mov al,str1[1]
cmp al,str2[1]
jne noteq
mov ch,00h
mov cl,str1[1]
cld
lea si,str1
lea di,str2
repe cmpsb
jnz noteq
;Display procedure
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
mov ah,01h
int 21h
mov c,al
endm
writech macro c
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
mov al,03h
int 10h
LEA DX msg1
call disp
;Display message
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
mov bh,00h
mov dh,12
mov dl,30
int 10h
mov dx,offset msg2
call disp
mov si,0h
bak2: writech str[si]
cmp str[si],13
je exit
inc si
jmp bak2
L1:
mov si,00
readstr arr[si]
inc si
cmp al,13
jnz L1
mov arr[si], '$'
mov ah,00
mov al,3
int 10h
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
jmp last
fact proc
cmp ax,0
jl error
je exit
cmp ax,1
je exit
push ax
dec ax
call fact
; if no = 0 or 1, factorial is 1
pop ax
mul res
mov res,ax
ret
fact endp
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
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;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
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
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
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
mov ah,09h
lea dx, p1
int 21h
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(?)
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
.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
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
mov al,ch
call disp
mov dl,':'
mov ah,2
int 21h
mov al,cl
mov ah,2ch
int 21h
mov al,ch
aam
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
mov ah,4ch
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
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
loop2: mov dl,al
mov ah,02h
int 21h
push ax
cmp bl,039h
jle loop1
;loop second digit(0-9)
mov ah,02h
mov dl,00h
int 10h
pop ax
inc al
;increment 1st digit
cmp al,039h
jle loop2
;loop 1st digit(0-9)
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
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
call read_bcd
mov x,bh
mov dx,offset ymsg
call read_bcd
mov y,bh
mov ah,0
mov al,03
int 10h
mov ah,02h
mov dh,x
mov dl,y
mov bh,0
int 10h
mov dl,'-'
mov ah,06h
int 21h
mov ah,4ch
int 21h
read_bcd proc
mov ah,09h
int 21h
mov ah,01h
int 21h
mov bh,al
;first digit
mov ah,01h
int 21h
mov bl,al
mov cl,4h
;second digit
sub bh,30h
;to convert from ascii to bcd
sub bl,30h
shl bh,cl
or bh,bl
ret
read_bcd endp
end
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
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
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) Nonoverlapping and (ii) Overlapping conditions
; (i) Non overlapping
UP:
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:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
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
1
25 % 15
10
2
50 % 15
5
3
75 % 15
0
Therefore LCM is 75.
Algorithm
New Number
25+25=50
50+25=75
.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
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
Iteration
1
2
Hence the GCD is 30.
Algorithm
Second No.=120
Operation
120 % 90
90 % 30
Remainder
30
0
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
.stack
.data
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
.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
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
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
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
jmp STOP
ASCTOHEX:
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
.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