Lab Report
Lab Report
Lab Report: 03
Submitted By:
Afrina Sultana
ID: 2018-1-17-016
Submitted To:
Ayesha Siddiqua
Lecturer
Question: 09
Source Code:
.MODEL SMALL
.STACK 100H
.DATA
.CODE
MAIN PROC
MOV DS, AX
1
MOV AH, 9
LEA DX, A
INT 21H
MOV AH, 1
INT 21H
MOV BL, AL
SUB BL, 48
MOV AH, 9
LEA DX, B
INT 21H
MOV AH, 1
INT 21H
SUB AL, 48
MUL BL
AAM
2
MOV CX, AX
ADD CH, 48
ADD CL, 48
MOV AH, 9
LEA DX, C
INT 21H
MOV AH, 2
MOV DL, CH
INT 21H
MOV DL, CL
INT 21H
EXIT:
INT 21H
MAIN ENDP
END MAIN
3
Output:
.MODEL SMALL
.STACK 100H
Inside our data segment some messages will be printed for taking our inputs and
showing the final result like,
4
C DB 10, 13,'MULTIPLICATION RESULT: $'
After that the data segment is initialized inside our code segment by,
MOV DS, AX
MOV AH, 9
INT 21H
Here, LEA stands for load effective address which loads the message of variable A
in DX.
The inputs that we take in assembly language are basically in ASCII value. To
show them in decimal number we have to convert it to decimal. For this we write,
SUB BL, 48
To take second input we have to print the message of variable B like previous.
After that we take our second value.
5
For multiplication there are two OPCODES MUL and IMUL. For unsigned digits
we can use MUL OPCODE and for signed digits we can use the IMUL OPCODE.
Here I will use MUL OPCODE.
AAM: The result of the multiplication can contain two digits. For that we use the
AAM OPCODE which stands for ASCII Adjust for Multiplication.
We converted the ASCII values to decimal for taking our inputs. For that our
results will also be in decimal. But for the emulator to understand the value of the
result, we need to convert the result from decimal to ASCII value. For that we
write,
MOV CX, AX
ADD CH, 48
ADD CL, 48
We have to print our result one by one because there are two digits in the result.
For that we have to print the higher value first and then the lower value.
6
INT 21H: Interrupt function which interrupts the program and search for
immediate function to execute.
EXIT:
MAIN ENDP
END MAIN
Discussion: For signed digits we can use the IMUL OPCODE. This program will
not run for the results that are more than two digits.
Question: 10
Source Code:
.MODEL SMALL
.STACK 100H
.DATA
7
B DB 10, 13,'ENTER SECOND INPUT: $'
.CODE
MAIN PROC
MOV DS, AX
MOV AH, 9
LEA DX, A
INT 21H
MOV AH, 1
INT 21H
MOV BH, AL
SUB BH, 48
MOV AH, 9
LEA DX, B
8
INT 21H
MOV AH, 1
INT 21H
MOV BL, AL
SUB BL, 48
MOV AH, 9
LEA DX, C
INT 21H
MOV CL, BH
MOV CH, 00
MOV AX, CX
DIV BL
AAD
ADD AL, 48
MOV AH, 2
9
MOV DL, AL
INT 21H
EXIT:
INT 21H
MAIN ENDP
END MAIN
Output:
10
Explanation: First inside the data segment we will print some messages and
then initialize them inside our code segment like our previous program.
Then we’ll take our inputs through AL and then copy it in BH using MOV
instruction. As emulator is set for ASCII values we have to convert our inputs in
decimal. For that we have to subtract 48 from our inputs. Again we’ll take our
second input and move it in BL. Here I have considered BH as our dividend and
BL as our divisor. So, we have to move our dividend in AX register. We know that
CX register is a combination of both CH and CL. For that we write,
DIV BL
AAD: The result of the division can contain two digits. For that we use the AAD
OPCODE which stands for ASCII Adjust for Division.
Now the final result is stored in AL. To print the result we write,
ADD AL, 48: Adds 48 with the result to convert it from decimal to ASCII value.
11
INT 21H: Interrupt function.
EXIT:
INT 21H
MAIN ENDP
END MAIN
Question: 11
Write a program that finds odd and even number from user input using assembly
language.
Source Code:
.MODEL SMALL
.STACK 100H
.DATA
.CODE
MAIN PROC
MOV DS, AX
MOV AH, 1
INT 21H
JE ODD
JE ODD
JE ODD
JE ODD
EVEN:
MOV AH, 9
INT 21H
JMP EXIT
ODD:
MOV AH, 9
INT 21H
EXIT:
INT 21H
14
MAIN ENDP
END MAIN
Output:
.STACK 100H
Inside our data segment we will declare some messages and then initialize them
inside our code segment as,
15
MOV DS, AX
MOV AH, 1
INT 21H
Now to check whether the number is odd or even first we write the program to
compare the input with odd numbers. If it doesn’t matches with any of the odd
numbers then by default the number is even. We do the comparison as follows,
Like this the program will compare the input with the rest odd numbers and if that
is equal to that odd number then it will jump to the level ODD.
After the last comparison with 9, then level EVEN comes. If none of the
comparison is successful then by default then program will go to EVEN level and
print the result to EVEN which is,
EVEN:
INT 21H: Interrupt function which interrupts the program and search for
immediate function to execute.
16
JMP EXIT: After executing the result to even it will jump to exit.
And if any of the comparisons is successful, then the program jumps to level ODD
which is,
ODD:
LEA DX, MSG2: Prints the second message which says ODD.
EXIT:
INT 21H
MAIN ENDP
END MAIN
Question: 12
17
Write a program to compare a specific character with input using assembly
language.
Source Code:
.MODEL SMALL
.STACK 100H
.DATA
.CODE
MAIN PROC
MOV DS, AX
MOV AH, 1
INT 21H
MOV BL, AL
18
JE L1
JE L1
JMP L2
L2:
MOV AH, 9
LEA DX, B
INT 21H
JMP EXIT
L1:
MOV AH, 9
LEA DX, A
INT 21H
MOV AH, 2
MOV DL, BL
19
INT 21H
EXIT:
INT 21H
MAIN ENDP
END MAIN
Output:
20
Explanation: .MODEL SMALL
21
.STACK 100H
Then inside our data segment we will declare some messages as,
To let the code segment know about these messages we have to initialize them
inside our code segment like,
MOV DS, AX
INT 21H: Interrupt function which interrupts the program and search for
immediate function to execute.
Now to compare with user input we have to specify some characters according to
our preference.
CMP BL, 'Y': The CMP OPCODE compares the specified character Y with our
user input.
22
Then again like this we can take as many characters as we want and compare them
with user input.
JE L1
If user input doesn’t match with any of the specified characters then it will jump to
level L2 to print the message ‘NOT MATCHED’.
L2:
JMP EXIT: Then it will jump to EXIT and end the program.
L1:
INT 21H: Interrupt function which interrupts the program and search for
immediate function to execute.
23
MOV AH, 2: Output function to print the character.
EXIT:
INT 21H
MAIN ENDP
END MAIN
Discussion: This program can be executed to compare the user input with as
many characters as we want. For this we have to specify the characters inside our
program.
Question: 13
Write a program that finds number of 1 bit in a binary number of 16 bits using
assembly language.
Source Code:
.MODEL SMALL
24
.STACK 100H
.CODE
MAIN PROC
MOV CX, 16
MOV AX, 0
TOP:
ROL BX, 1
JNC NEXT
INC AX
NEXT:
LOOP TOP
MOV AH, 2
ADD AX, 48
MOV DX, AX
25
INT 21H
EXIT:
INT 21H
MAIN ENDP
END MAIN
26
Output:
.STACK 100H
.CODE
27
MOV CX, 16: Count Register is used for loop. Here 16 means that is loop will
execute for 16 times because we are using 16 bits binary number here.
TOP:
JNC NEXT: Jump Not Carry means if there is no carry in the number then it will
jump to NEXT level.
LOOP TOP: This loop will take the program to loop TOP. Then again the TOP
loop will continue the process of finding 1 and counting it.
ADD AX, 48: To display in ASCII form we have to add 48 with the result which is
in AX register.
MOV DX, AX: The result of AX will be moved to DX. DX is our display register
for 16 bits.
28
EXIT:
INT 21H
MAIN ENDP
END MAIN
Discussion: In this program we are working with 16 bits binary number. Because
of that we have to be careful while choosing our registers as they also have to be of
16 bits. This program can be executed for 8 bits, 32 bits and for 64 bits also.
Conclusion: In this lab I have learnt more OPCODES which will be required for
next problems. The problems that I solved in this lab were new for me and had new
instructions which I never used in my previous problems.
29