Systems Programming Lab Report
Systems Programming Lab Report
GROUP MEMBERS
Bodhisattwa Halder (Roll: 15)
Masud Rahaman Laskar (Roll: 18)
Soumyadeep Barman (Roll: 19)
Anuran Chakraborty (Roll: 20)
Arighna Saha (Roll: 21)
1|Page
INDEX
Assignment 1
1. Soumyadeep Barman
2. Soumyadeep Barman
3. Anuran Chakraborty
4. Anuran Chakraborty
5. Masud Rahaman Laskar
6. Masud Rahaman Laskar
7. Soumyadeep Barman
8. Anuran Chakraborty
9. Arighna Saha
10. Masud Rahaman Laskar
Assignment 2
1. Bodhisattwa Halder
2. Bodhisattwa Halder
3. Bodhisattwa Halder
4. Bodhisattwa Halder
5. Soumyadeep Barman
6. Arighna Saha
7. Arighna Saha
8. Anuran Chakraborty
9. Anuran Chakraborty
10. Anuran Chakraborty
Assignment 3
1. Anuran Chakraborty
2. Anuran Chakraborty
3. Anuran Chakraborty
2|Page
ASSIGNMENT 1
1. Write and test a MASM program to Display your name and program title on the output
screen.
The name and program title are first defined in the data section as strings. In the main
procedure the addresses of the strings are loaded in dx and printed out using interrupt 21h.
; Write and test a MASM program to Display your name and program title on the
output screen.
.model small
.stack 100h
.data
name1 db"Name: Anuran$"
programTitle db"Program title: Ques1$"
.code
mov ax,@data
mov ds,ax
;carriage return
mov AH, 02h
mov DL, 0DH
int 21H
;line feed
mov DL, 0AH
int 21H
;exit
mov ah,4Ch
int 21h
end
3|Page
2. Write and test a MASM program to convert a letter from uppercase to lowercase.
First a character is taken as input using 01h and int 21h. Then it is checked if it is lowercase. If
lowercase then it is converted to uppercase by subtracting 20h else no conversion is done.
.data
msg1 db 10,13,"Enter a character: $"
msg2 db 10,13,"Lowercase character is: $"
.code
main proc
mov ax,@data
mov ds,ax
;accept a character
mov ah,01h
int 21h
;check if al is uppercase
cmp al,'A'
jl display
cmp al,'Z'
jg display
add al,32
display:
;display prompt
4|Page
lea dx,msg2
mov ah,09h
int 21h
mov ah,4ch
int 21h
main endp
end main
First two hexadecimal numbers are taken as input. Hexadecimal input is taken by accepting each
character and converting to corresponding number. For every subsequent digit the register is left
shifted by 4 bits and the next digit is stored. The addition of the registers is performed and
printed out taking into account carry flag.
.model small
.stack 100h
.data
prompt1 db 13,10,"enter the 1st number: $"
prompt2 db 13,10,"enter the 2nd number: $"
prompt3 db 13,10,"the result of the addition is: $"
.code
main proc
mov ax,@data ;for moving data to
data segment
mov ds,ax
5|Page
mov ah,1 ;for taking input
int 21h
input1:
cmp al,0dh ;compare whether the
pressed key is 'enter' or not
je line1 ;if it is equal to
'enter' then stop taking first value
shift1:
shl bx, cl
or bl,al ;making 'or' will add
the current value with previous value
int 21h
jmp input1
line1:
lea dx, prompt2 ;show num2 prompt
mov ah, 9
int 21h
mov ah,1
int 21h
input2:
cmp al,0dh ;compare whether the
pressed key is 'enter' or not
je line2 ;if it is equal to
'enter' then stop taking first value
6|Page
cmp al,39h ;compare the input
whether it is letter or digit.39h is the ascii value of 9
jg letter2
shift2:
shl dx, cl
or dl,al ;making 'or' will add
the current value with previous value
int 21h
jmp input2
line2:
xor cx,cx
mov cx,dx
mov dh,16
sum:
add bx,cx ;add two number which are
stored in bx and cs register
jc pc1 ;if the register is
overflowed then print an extra 1
mov cl, 4
mov ch,bh
shr ch, cl
and ch,0fh
7|Page
cmp ch,10 ;convert decimal to
binary
add ch,'0'
cmp ch,':'
jl tag
add ch,7
tag:mov dl,ch
mov ah,2
int 21h
mov ch,bh
and ch,0fh
cmp ch,10
add ch,'0'
cmp ch,':'
jl tag1
add ch,7
tag1:mov dl,ch
mov ah,2
int 21h
mov ch,bl
shr ch, cl
and ch,0fh
cmp ch,10
add ch,'0'
cmp ch,':'
jl tag2
add ch,7
tag2:mov dl,ch
mov ah,2
int 21h
mov ch,bl
and ch,0fh
cmp ch,10
add ch,'0'
cmp ch,':'
jl tag3
add ch,7
tag3:mov dl,ch
mov ah,2
int 21h
jmp exit
8|Page
pc1: ;level for printing
overflowed 1
mov dl,'1'
mov ah,2
int 21h
jmp output
exit:
mov ah, 4ch ;return control to dos
int 21h
main endp
end main
4. Write and test a MASM program to find the second max and second min from an
array.
First the size of the array is taken as input then elements of the array are input. Next the
maximum and minimum elements of the array are found out by comparing the current element
with the last maximum and minimum element and accordingly update the max and min
variables. After the max and min are found out another loop is run to find out the second max
and second min by comparing with the current second max and second min and the already fount
out max and min. Then the two elements are printed out.
.model small
.stack 100h
.data
prompt_0 db 'enter the number of array elements :',0dh,0ah,'$'
prompt_1 db 'enter the array elements :',0dh,0ah,'$'
prompt_2 db 'the 2nd maximum is : $'
prompt_3 db 'the 2nd minimum is : $'
array dw 50 dup(0)
s dw ?
max dw ?
min dw ?
.code
main proc
9|Page
lea dx, prompt_0 ; load and display the string
prompt_0
mov ah, 9
int 21h
input1:
cmp al,0dh ;compare whether
the pressed key is 'enter' or not
je line1 ;if it is equal to
'enter' then stop taking first value
shl bx, 1
shl bx, 1
shl bx, 1
shl bx, 1
or bl,al ;making 'or' will
add the current value with previous value
int 21h
jmp input1
line1:
lea dx, prompt_1 ; load and display the string
prompt_1
mov ah, 9
int 21h
xor dx,dx
10 | P a g e
input2:
cmp al,0dh ;compare whether
the pressed key is 'enter' or not
je line2 ;if it is equal to
'enter' then stop taking first value
shl dx,1
shl dx,1
shl dx,1
shl dx,1
or dl,al ;making 'or' will
add the current value with previous value
int 21h
jmp input2
line2:
mov [si], dx ; set [si]=ax
add si, 2 ; set si=si+2
lea si,array
mov ax,bx
dec ax
xor bx,bx
xor cx,cx
mov bx,word ptr[si] ;store the maximum
mov cx,word ptr[si] ;store the 2nd
add si, 2
11 | P a g e
mov cx,bx
mov bx,word ptr[si]
max2:
cmp word ptr[si],cx
jl incre
cmp word ptr[si],bx
je incre
mov cx,word ptr[si]
incre:
add si, 2
dec ax
jnz arrayloop2
; display contents of cx
mov bx,cx
mov dh,bh
shr dh, 1
shr dh, 1
shr dh, 1
shr dh, 1
and dh,0fh
add dh,'0'
mov dl,dh
mov ah,2
int 21h
mov dh,bh
and dh,0fh
add dh,'0'
mov dl,dh
mov ah,2
int 21h
12 | P a g e
mov dh,bl
shr dh, 1
shr dh, 1
shr dh, 1
shr dh, 1
and dh,0fh
add dh,'0'
mov dl,dh
mov ah,2
int 21h
mov dh,bl
and dh,0fh
cmp dh,10
add dh,'0'
mov dl,dh
mov ah,2
int 21h
;=============================================================
====================
lea si,array
mov ax,s
dec ax
mov bx,max
min2:
cmp word ptr[si],cx
jg incre2
cmp word ptr[si],bx
je incre2
mov cx,word ptr[si]
13 | P a g e
incre2:
add si, 2
dec ax
jnz arrayloop3
; display contents of cx
mov bx,cx
mov dh,bh
shr dh, 1
shr dh, 1
shr dh, 1
shr dh, 1
and dh,0fh
add dh,'0'
mov dl,dh
mov ah,2
int 21h
mov dh,bh
and dh,0fh
add dh,'0'
mov dl,dh
mov ah,2
int 21h
mov dh,bl
shr dh, 1
shr dh, 1
shr dh, 1
shr dh, 1
and dh,0fh
add dh,'0'
mov dl,dh
mov ah,2
14 | P a g e
int 21h
mov dh,bl
and dh,0fh
cmp dh,10
add dh,'0'
mov dl,dh
mov ah,2
int 21h
exit:
mov ah, 4ch ;return control to
dos
int 21h
main endp
end main
In this program a task has been performed and a terminating message has been displayed when
the task is complete.
.model small
.stack 100h
.data
prompt1 db 13,10,"enter the 1st number: $"
prompt2 db 13,10,"enter the 2nd number: $"
promptyes db 13,10,"the second number is less than the first$"
promptno db 13,10,"the second number is not less than the first$"
promptter db 13,10,"Terminating!!!$"
.code
main proc
mov ax,@data ;for moving data to
data segment
mov ds,ax
15 | P a g e
lea dx, prompt1 ;show num1 prompt
mov ah, 9
int 21h
input1:
cmp al,0dh ;compare whether the
pressed key is 'enter' or not
je line1 ;if it is equal to
'enter' then stop taking first value
shift1:
shl bx, cl
or bl,al ;making 'or' will add
the current value with previous value
int 21h
jmp input1
line1:
lea dx, prompt2 ;show num2 prompt
mov ah, 9
int 21h
mov ah,1
int 21h
16 | P a g e
input2:
cmp al,0dh ;compare whether the
pressed key is 'enter' or not
je line2 ;if it is equal to
'enter' then stop taking first value
shift2:
shl dx, cl
or dl,al ;making 'or' will add
the current value with previous value
int 21h
jmp input2
line2:
xor cx,cx
mov cx,dx
mov dh,16
compare_nums:
cmp bx,cx ;add two number which are
stored in bx and cs register
jg pc1
pc1:
lea dx, promptyes ;show answer prompt
mov ah, 9
17 | P a g e
int 21h
exit:
lea dx, promptter ;show terminating prompt
mov ah, 9
int 21h
main endp
end main
6. Write and test a MASM program to Take a character from keyboard and print it.
First a character is taken as input from keyboard using 01h, 21h and then it is printed out using
02h, 21h.
; Write and test a MASM program to Take a character from keyboard and print
it.
.model small
.stack 100h
.data
msg1 db 10,13,"Enter a character: $"
msg2 db 10,13,"The character is: $"
.code
main proc
mov ax,@data
mov ds,ax
;accept a character
mov ah,01h
int 21h
18 | P a g e
;al has the character
;display prompt
lea dx,msg2
mov ah,09h
int 21h
mov ah,4ch
int 21h
main endp
end main
7. Write and test a MASM program to validate second numbers is less than the first.
Two numbers are taken as input using the previously described input procedure and the numbers
are compared using the cmp instruction and if the second number is greater than the first an
appropriate message is displayed using 09h, 21h.
; Write and test a MASM program to validate second numbers is less than the
first.
.model small
.stack 100h
.data
prompt1 db 13,10,"enter the 1st number: $"
prompt2 db 13,10,"enter the 2nd number: $"
promptyes db 13,10,"the second number is less than the first$"
promptno db 13,10,"the second number is not less than the first$"
.code
main proc
mov ax,@data ;for moving data to
data segment
mov ds,ax
19 | P a g e
lea dx, prompt1 ;show num1 prompt
mov ah, 9
int 21h
input1:
cmp al,0dh ;compare whether the
pressed key is 'enter' or not
je line1 ;if it is equal to
'enter' then stop taking first value
shift1:
shl bx, cl
or bl,al ;making 'or' will add
the current value with previous value
int 21h
jmp input1
line1:
lea dx, prompt2 ;show num2 prompt
mov ah, 9
int 21h
mov ah,1
int 21h
20 | P a g e
input2:
cmp al,0dh ;compare whether the
pressed key is 'enter' or not
je line2 ;if it is equal to
'enter' then stop taking first value
shift2:
shl dx, cl
or dl,al ;making 'or' will add
the current value with previous value
int 21h
jmp input2
line2:
xor cx,cx
mov cx,dx
mov dh,16
compare_nums:
cmp bx,cx ;add two number which are
stored in bx and cs register
jg pc1
pc1:
lea dx, promptyes ;show answer prompt
21 | P a g e
mov ah, 9
int 21h
exit:
mov ah, 4ch ;return control to dos
int 21h
main endp
end main
8. Write and test a MASM program to find maximum and minimum from an array.
First the size of the array is taken as input then elements of the array are input. Next the
maximum and minimum elements of the array are found out by comparing the current element
with the last maximum and minimum element and accordingly update the max and min
variables. Then max and min are printed out.
.model small
.stack 100h
.data
prompt_0 db 'enter the number of array elements :',0dh,0ah,'$'
prompt_1 db 'enter the array elements :',0dh,0ah,'$'
prompt_2 db 'the maximum is : $'
prompt_3 db 'the minimum is : $'
array dw 50 dup(0)
s dw ?
.code
main proc
input1:
22 | P a g e
cmp al,0dh ;compare whether
the pressed key is 'enter' or not
je line1 ;if it is equal to
'enter' then stop taking first value
shl bx, 1
shl bx, 1
shl bx, 1
shl bx, 1
or bl,al ;making 'or' will
add the current value with previous value
int 21h
jmp input1
line1:
lea dx, prompt_1 ; load and display the string
prompt_1
mov ah, 9
int 21h
xor dx,dx
input2:
cmp al,0dh ;compare whether
the pressed key is 'enter' or not
je line2 ;if it is equal to
'enter' then stop taking first value
23 | P a g e
shl dx,1
shl dx,1
shl dx,1
shl dx,1
or dl,al ;making 'or' will
add the current value with previous value
int 21h
jmp input2
line2:
mov [si], dx ; set [si]=ax
add si, 2 ; set si=si+2
lea si,array
mov ax,bx
dec ax
xor bx,bx
xor cx,cx
mov bx,word ptr[si] ;store the maximum
mov cx,word ptr[si] ;store the minimum
add si, 2
jmp incre
maximum:
mov bx,word ptr[si]
jmp incre
24 | P a g e
minimum:
mov cx,word ptr[si]
incre:
add si, 2
dec ax
jnz arrayloop2
; display contents of bx
output: ;level for printing
their sum
mov dh,bh
shr dh, 1
shr dh, 1
shr dh, 1
shr dh, 1
and dh,0fh
add dh,'0'
mov dl,dh
mov ah,2
int 21h
mov dh,bh
and dh,0fh
add dh,'0'
mov dl,dh
mov ah,2
int 21h
mov dh,bl
shr dh, 1
shr dh, 1
shr dh, 1
shr dh, 1
and dh,0fh
add dh,'0'
mov dl,dh
25 | P a g e
mov ah,2
int 21h
mov dh,bl
and dh,0fh
cmp dh,10
add dh,'0'
mov dl,dh
mov ah,2
int 21h
; display contents of cx
mov bx,cx
mov dh,bh
shr dh, 1
shr dh, 1
shr dh, 1
shr dh, 1
and dh,0fh
add dh,'0'
mov dl,dh
mov ah,2
int 21h
mov dh,bh
and dh,0fh
add dh,'0'
mov dl,dh
mov ah,2
int 21h
mov dh,bl
shr dh, 1
shr dh, 1
26 | P a g e
shr dh, 1
shr dh, 1
and dh,0fh
add dh,'0'
mov dl,dh
mov ah,2
int 21h
mov dh,bl
and dh,0fh
cmp dh,10
add dh,'0'
mov dl,dh
mov ah,2
int 21h
exit:
mov ah, 4ch ;return control to
dos
int 21h
main endp
end main
9. Write and test a MASM program to loop until the user decides to quit.
A loop is run infinitely and a message is displayed inside the loop and a character is taken as
input. If the character is q then the program terminates else the looping continues.
;Write and test a MASM program to loop until the user decides to quit
.model small
.stack 100h
.data
msg db 10,13,"Enter q to quit any other key to continue looping: $"
looping db 10,13,"loop$"
.code
main proc
mov ax,@data
mov ds,ax
27 | P a g e
label1:
;accept a character
mov ah,01h
int 21h
; check if character is q
cmp al,'q'
jne label1
;exit
mov ah,4Ch
int 21h
main endp
end main
10. Write and test a MASM program to print all the characters from A-Z.
A loop is run starting from ‘A’ and ending at ‘Z’. For every loop iteration the contents of bx are
displayed using 02h, 21h.
; Write and test a MASM program to Print all the characters from A-Z.
.model small
.stack 100h
.data
space db ' '
.code
main proc
mov ax,@data
mov ds,ax
28 | P a g e
mov bx,65
mov cx,0
label1:
;print the character
mov ah,02h
mov dl,bl
int 21h
;increment
inc bx
inc cx
cmp cx,26
jne label1
mov ah,4ch
int 21h
main endp
end main
ASSIGNMENT 2
N.B.: For the following assignments a macro table mtab.asm has been created and used.
mtab.asm
;macro to print new line
new_line macro
mov ah,02h
mov dl,0dh
int 21h
mov dl,0ah
int 21h
endm
29 | P a g e
;macro to print space
space macro
mov ah,02h
mov dl,' '
int 21h
endm
;macro to exit
exitp macro
mov ah,4ch
int 21h
endm
xor bx,bx
mov ah,01h
int 21h
;if \r
cmp al,0dh
je skip
input:
and ax,000fh
push ax
; bx=bx*10+ax
mov ax,10
mul bx
mov bx,ax
pop ax
30 | P a g e
add bx,ax
; take input
mov ah,01h
int 21h
cmp al,0dh
jne input
skip:
endm
; input : bx
; output : none
31 | P a g e
display: ; loop label
pop dx ; pop a value from stack to dx
or dl, 30h ; convert decimal to ascii code
int 21h ; print a character
loop display
endm
xor bx,bx
mov ah,01h
int 21h
cmp al,0dh
je skip
input:
xor ah,ah
sub ax,'0'
shl bx,1
or bx,ax
; take input
mov ah,01h
int 21h
cmp al,0dh
jne input
skip:
endm
mov ah,02h
32 | P a g e
mov cx,0
output:
mov dx,bx
and dx,01h
add dx,'0'
push dx
inc cx
shr bx,1
jnz output
mov cx,cx
display_loop:
pop dx
int 21h
loop display_loop
endm
xor bx,bx
mov ah,01h
int 21h
cmp al,0dh
je skip
input:
xor ah,ah
cmp ax,'A'
jge letter
sub ax,'0'
jmp shift
letter:
sub ax,55
shift:
shl bx,1
shl bx,1
33 | P a g e
shl bx,1
shl bx,1
or bx,ax
; take input
mov ah,01h
int 21h
cmp al,0dh
jne input
skip:
endm
mov ah,02h
mov cx,0
output:
mov dx,bx
and dx,0fh
cmp dx,10
jge letter
add dx,'0'
jmp line
letter:
add dx,55
line:
push dx
inc cx
shr bx,1
shr bx,1
shr bx,1
shr bx,1
jnz output
34 | P a g e
mov cx,cx
display_loop:
pop dx
int 21h
loop display_loop
endm
dec_input_with_neg macro
local @read,@error,@minus,@plus,@inpit,@end,@exit
jmp @read ; jump to label @read
35 | P a g e
and ax, 000fh ; convert ascii to decimal code
@exit:
endm
dec_output_with_neg macro
36 | P a g e
@repeat: ; loop label
xor dx, dx ; clear dx
div bx ; divide ax by bx
push dx ; push dx onto the stack
inc cx ; increment cx
or ax, ax ; take or of ax with ax
jne @repeat ; jump to label @repeat if zf=0
endm
pushall macro
push ax
push bx
push cx
push dx
endm
popall macro
pop dx
pop cx
pop bx
pop ax
endm
First two numbers are taken as input using the hex_input macro defined in mtab.asm given
above. Then addition is performed and the sum is output using hex_output macro.
include mtab.asm
.model small
.stack 100h
.data
iprompt1 db "Enter two numbers: $"
oprompt1 db "Their sum is: $"
37 | P a g e
oprompt2 db "Their difference is: $"
num1 dw ?
num2 dw ?
.code
main proc
mov ax,@data
mov ds,ax
;input prompt
printm iprompt1
hex_input
mov num1,bx
hex_input
mov num2,bx
;********************** SUM
**************************************
printm oprompt1
mov cx,num1
add bx,cx
jnc display
carry_disp:
;display carry
mov ah,02h
mov dl,'1'
int 21h
display:
hex_output
;********************** SUM **********************************
mov bx,num1
mov cx,num2
sub bx,cx
hex_output
38 | P a g e
;********************** DIFF *********************************
exitp
main endp
end main
A menu is displayed and a character is taken as input for choice if B is the character then binary
input is taken using the bin_input procedure. This is done by taking each character and shifting
by one bit and storing each digit. The input is continued until new line. Then the number is
output using the dec_output procedure. The dec_output procedure gives decimal output by taking
mod 10 of the register and dividing by 10. Every digit is pushed into the stack and printed out
ono by ono by popping.
;12. Write and test a program to Convert a Binary digit to Decimal and vice
versa.
.model small
.stack 100h
.data
prompt_0 DB 10,13,10,13,'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%$ '
prompt_1 DB 10,13,'Enter B for binary-decimal conversion, D for vice-
versa, any other key for quit : $'
prompt_2 DB 10,13,'Enter the Binary Number : $'
prompt_3 DB 10,13,'Enter the Decimal Number : $'
prompt_4 DB 10,13,'The converted Binary Number is : $'
prompt_5 DB 10,13,'The converted Decimal Number is : $'
ch_input macro
mov ah,1
int 21h
endm
.code
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 | P a g e
;;;;;;;;;;;;;;;;;Binary Input;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
bin_input proc
xor bx,bx
xor cx,cx
@binput:
mov ah,1
int 21h
cmp al,13
je @bend
sub ax,30h
shl bx,1
or bl,al
jmp @binput
@bend:
ret
bin_input endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;Binary Output;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
bin_output proc
mov ax, bx ; set ax=bx
xor cx, cx ; clear cx
mov bx, 2 ; set bx=2
40 | P a g e
int 21h ; print a character
loop @bdisplay ; jump to label @display
if cx!=0
ret
bin_output endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;Decimal Input;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
dec_input proc
mov ah,1
int 21h ; read a character
cmp al, 0dh ; compare al with cr
je @dend ; jump to label @end if
al=cr
41 | P a g e
ret ; return control to the calling
procedure
dec_input endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;decimal output;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
dec_output proc
mov ax, bx ; set ax=bx
xor cx, cx ; clear cx
mov bx, 10 ; set bx=10
ret
dec_output endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
main proc
mov ax,@data
mov ds,ax
@start:
display prompt_0
display prompt_1
ch_input
42 | P a g e
cmp al,'D'
je @dec2bin
cmp al,'B'
je @bin2dec
jmp @main_exit
@bin2dec:
display prompt_2
call bin_input
display prompt_5
call dec_output
jmp @start
@dec2bin:
display prompt_3
call dec_input
display prompt_4
call bin_output
jmp @start
@main_exit:
mov ah,4ch
int 21h
main endp
end main
3. Write and test a MASM program to perform subtraction of two 16 bit numbers.
First two numbers are taken as input using the hex_input macro defined in mtab.asm given
above. Then addition is performed and the difference is output using hex_output macro.
include mtab.asm
.model small
.stack 100h
.data
iprompt1 db "Enter two numbers: $"
oprompt1 db "Their sum is: $"
oprompt2 db "Their difference is: $"
num1 dw ?
num2 dw ?
.code
43 | P a g e
main proc
mov ax,@data
mov ds,ax
;input prompt
printm iprompt1
hex_input
mov num1,bx
hex_input
mov num2,bx
;********************** SUM
**************************************
printm oprompt1
mov cx,num1
add bx,cx
jnc display
carry_disp:
;display carry
mov ah,02h
mov dl,'1'
int 21h
display:
hex_output
;********************** SUM **********************************
mov bx,num1
mov cx,num2
sub bx,cx
hex_output
;********************** DIFF *********************************
exitp
44 | P a g e
main endp
end main
First two numbers are taken as input using the dec_input procedure defined in mtab.asm given
above. Then addition is performed and the product is output using dec_output procedure.
.model small
.stack 100h
.data
prompt_1 db "Enter two numbers: ",10,13,"$"
prompt_2 db "Their product is: $"
new_line db 10,13,"$"
num1 db ?
num2 db ?
.code
;*********************************
;**** decimal input **************
; input: none
; output: bx
dec_input proc
45 | P a g e
cmp al, '-' ; compare al with "-"
je @minus ; jump to label @minus if al="-"
46 | P a g e
ret ; return control to the calling
procedure
dec_input endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;
;************************************************
; decimal output
;************************************************
decimal_output proc
; this procedure will display a decimal number
; input : bx
; output : none
; uses : main
47 | P a g e
or dl, 30h ; convert decimal to ascii code
int 21h ; print a character
loop @display ; jump to label @display if cx!=0
main proc
mov ax,@data
mov ds,ax
xor bh,bh
;input prompt
display prompt_1
call dec_input
mov num1,bl
xor bh,bh
call dec_input
mov num2,bl
xor bh,bh
xor ah,ah
mov al,num1
mul bx
mov bx,ax
display new_line
display prompt_2
call decimal_output
mov ah,4ch
int 21h
main endp
end main
5. Write and test a MASM program to Convert Binary digit to Hex digit.
;Write and test a program to Convert a Binary digit to HexaDecimal and vice
versa
48 | P a g e
include mtab.asm
.model small
.stack 100h
.data
iprompt1 db "Enter binary number: $"
iprompt2 db "Enter hexadecimal number: $"
.code
main proc
mov ax,@data
mov ds,ax
;output
new_line
printm oprompt1
hex_output
;***********************************************************
;output
new_line
printm oprompt2
bin_output
;***********************************************************
49 | P a g e
exitp
main endp
end main
6. Write and test a MASM program to divide a 16 bit number by a 8 bit number.
First two numbers are taken as input using the hex_input macro defined in mtab.asm given
above. Then addition is performed and the quotient is output using hex_output macro.
.data
iprompt1 db 10,13,"Enter 16 bit number: $"
iprompt2 db 10,13,"Enter 8 bit number: $"
oprompt1 db 10,13,"Quotient is: $"
oprompt2 db 10,13,"Remainder is: $"
num1 dw ?
.code
main proc
mov ax,@data
mov ds,ax
;input
printm iprompt1
hex_input
mov num1,bx
printm iprompt2
hex_input
mov ax,num1
xor dx,dx
div bx
;output
mov bx,ax
mov num1,dx
50 | P a g e
printm oprompt1
pushall
hex_output
popall
mov bx,num1
printm oprompt2
pushall
hex_output
popall
exitp
main endp
end main
.model small
.stack 100h
.data
prompt db "The fibonacci series upto 10 terms is: $"
new_line db 10,13,"$"
space db " $"
f1 dw 1
f2 dw 1
f3 dw ?
51 | P a g e
push cx
push dx
endm
.code
;************************************************
; decimal output
;************************************************
decimal_output proc
; this procedure will display a decimal number
; input : bx
; output : none
; uses : main
52 | P a g e
mov ah, 2 ; set output function
main proc
mov ax,@data
mov ds,ax
mov bx,1
mov dx,1
display prompt
display new_line
pushall
call decimal_output
display space
popall
pushall
call decimal_output
display space
popall
mov bx,1
mov dx,1
mov cx,8
@loop:
mov f1,bx
mov f2,dx
add bx,dx
mov f3,bx ;f3=f1+f2
pushall
53 | P a g e
call decimal_output
display space
popall
loop @loop
mov ah,4ch
int 21h
main endp
end main
First the string is taken as input. Next the substring to be deleted is also taken as input. The
original string is completely searched and the place where the substring where the given
substring exists is found out and it is deleted. Finally the string is printed.
.model medium
.stack 100h
.data
prompt_1 db 10,13,'enter the string : $'
prompt_2 db 10,13,'enter the substring to be deleted : $'
prompt_3 db 10,13,'the new string is : $'
newline db 10,13,'$'
;input string
buffersize_1 db 51 ; 50 char + return
inputlength_1 db 0 ; number of read
characters
string db 51 dup(0) ; actual buffer
end_1 db '$'
index1 db 0 ;index for
looping
;input substring
buffersize_2 db 21 ; 20 char + return
inputlength_2 db 0 ; number of read
characters
substring db 21 dup(0) ; actual buffer
54 | P a g e
index2 db 0 ;index for
looping
55 | P a g e
mov dl,[ si ]
mov [ di ],dl ;copy from input string
to output string
inc al
mov index3,al ;increment the index
endm
.code
main proc
mov ax,@data
mov ds,ax
display prompt_1
get_string buffersize_1 ; input the
string
display prompt_2
get_string buffersize_2 ; input
the substring
@loop1:
56 | P a g e
mov di,offset substring ; load our
pointer to the beginning of the structure
mov index2,0
string_copy
compare
jne @label1
mov bl,inputlength_2
xor bh,bh
dec bx
@loop2:
inc si
dec cl
inc index2
string_copy
compare
jne @label1
dec bl
jne @loop2
@label1:
inc si
loop @loop1
@print:
string_copy ; add '$' after
the output string
display prompt_3
display newstring ; display the
output string
mov ah,4ch
int 21h
57 | P a g e
main endp
end main
The file name for the file to be created is taken as a string input. The file is then created using
3ch, 21h interrupt. Similarly for the file to be deleted the file name is taken as input and deleted
using 41h, 21h interrupt.
.model small
.stack 100h
.data
msg1 db 10,13,'enter file name to be created $'
msg2 db 10,13,'file is created$'
msg3 db 10,13,'enter file name to be deleted $'
msg4 db 10,13,'file is deleted$'
msg5 db 10,13,'deletion error$'
fnc db 50 dup(?)
fnd db 50 dup(?)
.code
pushall macro
push ax
push bx
push cx
push dx
endm
popall macro
pop dx
pop cx
pop bx
pop ax
endm
58 | P a g e
readlp:
mov ah,01h
int 21h
cmp al,13
je exit
mov [di],al
inc di
jmp readlp
exit:
endm
main proc
mov ax,@data
mov ds,ax
print msg1
pushall
readstr fnc
popall
crte:
mov cx,0
mov dx,offset fnc
mov ah,3ch
int 21h
print msg2
print msg3
pushall
readstr fnd
popall
dlte:
lea dx,fnd
mov ah,41h
int 21h
jc nfound
print msg4
jmp exit
nfound:
print msg5
exit:
mov ah,4ch
59 | P a g e
int 21h
main endp
end main
First an array size is taken as input and then all elements of the array are input using the
dec_input macro defined in mtab.asm. Then the whole array is scanned for the element and if
found the index is displayed else not found is displayed.
.model small
.stack 100h
.data
prompts db 10,13,"Enter size of array: $"
prompte db 10,13,"Enter elements of array: $"
promptsr db 10,13,"Enter element to search: $"
promptfound db 10,13,"element found at: $"
promptnotfound db 10,13,"element not found $"
arr dw 50 dup(?)
s dw ?
.code
main proc
mov ax,@data
mov ds,ax
;accept size
dec_input
; bx has the size
printm prompte
mov s,bx
lea si,arr
mov cx,bx
60 | P a g e
@array_input:
pushall
dec_input
mov word ptr[si],bx
popall
inc si
inc si
loop @array_input
@linear_search:
cmp bx,word ptr[si]
je @found
inc si
inc si
loop @linear_search
@found:
printm promptfound
mov bx,s
sub bx,cx
inc bx
dec_output
new_line
@exit:
exitp
main endp
end main
61 | P a g e
ASSIGNMENT 3
1. Write and test a MASM program to Implement Binary search. Show the steps. Each
step will be succeeded by “Enter” key.
The size of the array is taken as input, next the elements of the array are also taken as input. The
array is thereafter sorted by the sort procedure described in the next question. Then the element
to be searched is taken as input. Then the element is searched using the binary search algorithm,
comparing the middle element with the element to be searched and accordingly adjusting the
limits of the portion of the array to be searched. If the element is found it is displayed else “not
found” is displayed.
.model small
.stack 100h
.data
prompts db 10,13,"Enter size of array: $"
prompte db 10,13,"Enter elements of array: $"
promptsr db 10,13,"Enter element to search: $"
promptfound db 10,13,"element found at: $"
promptnotfound db 10,13,"element not found $"
wrong_key db 10,13,"Invalid key entered: $"
62 | P a g e
arr dw 50 dup(?)
s dw ?
strt dw ?
stop dw ?
min_idx dw ?
temp dw ?
.code
main proc
mov ax,@data
mov ds,ax
;accept size
dec_input
; bx has the size
printm prompte
mov s,bx
lea si,arr
mov cx,bx
@array_input:
pushall
dec_input
mov word ptr[si],bx
popall
inc si
inc si
loop @array_input
call sort
; enter element to search
printm promptsr
dec_input
63 | P a g e
mov cx,s
dec cx
mov strt,00h
mov stop,cx
;*************
push bx
push cx
mov bx,cx
call deci_output
pop cx
pop bx
space
push bx
push cx
mov bx,word ptr[si]
call deci_output
pop cx
pop bx
new_line
;*************
call ent
jg @greater
;if less
@lesser:
dec cx
mov stop,cx
jmp @compare
64 | P a g e
@greater:
inc cx
mov strt,cx
@compare:
mov cx,stop
cmp cx,strt
jge @binary_search
;************************************************
@found:
printm promptfound
mov bx,cx
inc bx
dec_output
new_line
@exit:
exitp
main endp
deci_output proc
dec_output
ret
deci_output endp
ent proc
;prompt for enter
;********** pressing enter will show next step esc will exit
************
@error_enter:
mov ah,01h
int 21h
65 | P a g e
je @exit2
cmp al,0dh
je @compare2
printm wrong_key
jmp @error_enter
;*************************************************************
***********
@compare2:
ret
@exit2:
exitp
ent endp
sort proc
;************** sorting ****************************
lea si,arr
mov cx,s
dec cx
@outer_loop:
mov dx,cx ; dx is the
inner loop counter
mov di,si
inc di
inc di
mov min_idx,si
push si
@inner_loop:
mov si,min_idx
mov bx,word ptr[si]
cmp word ptr[di],bx
jge @incr
; else set min_idx the elements
mov min_idx,di
@incr:
inc di
inc di
dec dx
jnz @inner_loop
;swap
pop si
mov di,min_idx
66 | P a g e
mov bx,word ptr[di]
xchg word ptr[si],bx
mov word ptr[di],bx
inc si
inc si
push si
push cx
; here keyboard input inserted
@next_iter:
pop cx
pop si
loop @outer_loop
ret
;***************************************************
sort endp
end main
2. Write and test a MASM program to Implement Selection Sort. Show the steps. Each
step will be succeeded by “Enter” key. The Program will terminate when the “Esc” key
is pressed.
The size of the array is taken as input, next the elements of the array are also taken as input. The
program sorts the array using selection sort algorithm. At each step the minimum element is
found and swapped with the current element. At every step the array is printed out and if the
input is enter then the next iteration is performed else if it is exit, the program terminates.
;Write and test a MASM program to Implement Selection Sort. Show the steps.
;Each step will be succeeded by “Enter” key. The Program will terminate when
the “Esc” key is pressed.
include mtab.asm
67 | P a g e
inc si
inc si
mov cx,temp
loop @array_print
endm
.model small
.stack 100h
.data
prompts db 10,13,"Enter size of array: $"
prompte db 10,13,"Enter elements of array: $"
promptsr db 10,13,"The sorted array is: $"
wrong_key db 10,13,"Invalid key entered: $"
arr dw 50 dup(?)
s dw ?
temp dw ?
min_idx dw ?
.code
main proc
mov ax,@data
mov ds,ax
;accept size
dec_input
; bx has the size
printm prompte
mov s,bx
lea si,arr
mov cx,bx
68 | P a g e
inc si
loop @array_input
;***************************************************
;swap
pop si
mov di,min_idx
mov bx,word ptr[di]
xchg word ptr[si],bx
mov word ptr[di],bx
inc si
inc si
push si
push cx
array_output arr
; here keyboard input inserted
;******* pressing enter will show next step esc will exit ********
69 | P a g e
@error_enter:
mov ah,01h
int 21h
loop @outer_loop
;***************************************************
;******* array output ***************************
printm promptsr
array_output arr
;***************************************************
@exit:
exitp
main endp
end main
3. Write and test a MASM program to wait for left mouse clicks and display a text string
at the exact clicked spot in the client area.
The mouse position is found out by the 03h, 33h interrupt. the cursor is then moved to the
specific position and the string is printed on mouse click.
include mtab.asm
.model large
.stack 100
.data
prompt1 db 'press left mouse button$'
prompt2 db 'hello$'
70 | P a g e
.code
main proc
mov ax,@data
mov ds,ax
mov ah,00h
mov al,13h
int 10h
xor cx,cx
xor dx,dx
mov ah,00h
int 33h
left_clk:
xor bx,bx
mov ax,3
int 33h
cmp bx,1
jne left_clk
;mov dl,dh
;mov dh,ch
;mov bh,0
;mov ah,2
;int 10h
;mov dx,offset prompt2
;mov ah,09h
;int 21h
pushall
dec_output cx
popall
mov ah,02h
mov dl,20h
int 21h
pushall
dec_output dx
popall
mov ah,4ch
int 21h
main endp
end main
71 | P a g e
72 | P a g e