0% found this document useful (0 votes)
84 views

Lab Report

The document contains the source code for several assembly language programs that perform different operations: 1) A program that multiplies two user inputs using MUL opcode. 2) A program that divides two user inputs using DIV opcode. 3) A program that determines if a single user input is odd or even by comparing it to odd numbers. 4) A program that compares a single user input to a specific character and displays a matched or unmatched message.

Uploaded by

Afrina Dipti
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Lab Report

The document contains the source code for several assembly language programs that perform different operations: 1) A program that multiplies two user inputs using MUL opcode. 2) A program that divides two user inputs using DIV opcode. 3) A program that determines if a single user input is odd or even by comparing it to odd numbers. 4) A program that compares a single user input to a specific character and displays a matched or unmatched message.

Uploaded by

Afrina Dipti
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Central Women’s University

Lab Report: 03

Course Title: Microprocessor and Micro Controller Sessional

Course Code: CSE-334

Submitted By:

Afrina Sultana

ID: 2018-1-17-016

Department of Computer Science and Engineering

Submitted To:

Ayesha Siddiqua

Lecturer

Department of CSE, CWU

Submission Date: 31-10-202


Introduction: In our previous lab we worked with new instructions and
OPCODES. In this lab we learnt how to do the multiplication, division, finding
odd and even numbers, comparison and finding how many 1’s are there in a binary
number.

Question: 09

Write a program of multiplication using assembly language.

Source Code:

.MODEL SMALL

.STACK 100H

.DATA

A DB 'ENTER FIRST INPUT: $'

B DB 10, 13,'ENTER SECOND INPUT: $'

C DB 10, 13,'MULTIPLICATION RESULT: $'

.CODE

MAIN PROC

MOV AX, @DATA

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:

MOV AH, 4CH

INT 21H

MAIN ENDP

END MAIN

3
Output:

Explanation: In this code the first three lines,

.MODEL SMALL

.STACK 100H

Work as header files.

Inside our data segment some messages will be printed for taking our inputs and
showing the final result like,

A DB 'ENTER FIRST INPUT: $'

B DB 10, 13,'ENTER SECOND INPUT: $'

4
C DB 10, 13,'MULTIPLICATION RESULT: $'

MAIN PROC is our main function here.

After that the data segment is initialized inside our code segment by,

MOV AX, @DATA

MOV DS, AX

To print our first message we write,

MOV AH, 9

INT 21H

Here, LEA stands for load effective address which loads the message of variable A
in DX.

We take our first input through MOV AH, 1.

MOV BL, AL: The input is copied to BL register.

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

We subtract 48 from the input value.

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.

MUL BL: BL = BL*AL. Our first input of BL and second input of AL is


multiplied together and stored in BL. Here MUL OPCODE does the operation for
multiplication.

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

Here, we copied AX to CX. We know that CX is a combination of both CL and


CH. That’s why 48 are added with both of them for the conversion.

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.

MOV AH, 2: Output function.

MOV DL, CH: Displays the higher value.

6
INT 21H: Interrupt function which interrupts the program and search for
immediate function to execute.

MOV DL, CL: Displays the lower value.

INT 21H: Interrupt function.

Lastly, to end our program we write,

EXIT:

MOV AH, 4CH

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

Write a program for division using assembly language.

Source Code:

.MODEL SMALL

.STACK 100H

.DATA

A DB 'ENTER FIRST INPUT: $'

7
B DB 10, 13,'ENTER SECOND INPUT: $'

C DB 10, 13,'DIVISION RESULT: $'

.CODE

MAIN PROC

MOV AX, @DATA

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:

MOV AH, 4CH

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,

MOV CL, BH: The dividend in BH is moved to CL.

MOV CH, 00: 00 is copied in CH.

MOV AX, CX: CX which is the combination of CH and CL is moved to AX.


Basically this derives that the dividend is moved to AX.

Now for the division we use the DIV OPCODE.

Our divisor is in BL, so 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.

MOV AH, 2: Output function.

MOV DL, AL: Displays the result of AL.

11
INT 21H: Interrupt function.

To end the program we write,

EXIT:

MOV AH, 4CH

INT 21H

MAIN ENDP

END MAIN

Discussion: While choosing our preferred dividend and divisor we have to be


careful about that. This program will not run for the results that are more than two
digits. There is no OPCODES for signed digits in division.

Question: 11

Write a program that finds odd and even number from user input using assembly
language.

Source Code:

.MODEL SMALL

.STACK 100H

.DATA

MSG1 DB ': EVEN$'


12
MSG2 DB ': ODD$'

.CODE

MAIN PROC

MOV AX, @DATA

MOV DS, AX

MOV AH, 1

INT 21H

CMP AL, '1'

JE ODD

CMP AL, '3'

JE ODD

CMP AL, '5'

JE ODD

CMP AL, '7'


13
JE ODD

CMP AL, '9'

JE ODD

EVEN:

LEA DX, MSG1

MOV AH, 9

INT 21H

JMP EXIT

ODD:

LEA DX, MSG2

MOV AH, 9

INT 21H

EXIT:

MOV AH, 4CH

INT 21H

14
MAIN ENDP

END MAIN

Output:

Explanation: .MODEL SMALL

.STACK 100H

Work as header files.

Inside our data segment we will declare some messages and then initialize them
inside our code segment as,

MOV AX, @DATA

15
MOV DS, AX

To take our input we write,

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,

CMP AL, '1': Compares the input with 1.

JE ODD: If the input is equal to 1 then it will jump to level ODD.

CMP AL, '3': Compares the input with 5.

JE ODD: If the input is equal to 5 then it will jump to level ODD.

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:

LEA DX, MSG1: Prints the first message.

MOV AH, 9: Output function for string.

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.

MOV AH, 9: Output function for string.

INT 21H: Interrupt function for interruption.

To end the program we write,

EXIT:

MOV AH, 4CH

INT 21H

MAIN ENDP

END MAIN

Discussion: We can execute this program in various ways. Such, as we can


separately compare the input with even numbers. We can also specify any numbers
inside our program and when we take any numbers from that specified numbers as
input, it will tell if that number is even or odd.

Question: 12

17
Write a program to compare a specific character with input using assembly
language.

Source Code:

.MODEL SMALL

.STACK 100H

.DATA

A DB ' MATCHED WITH $'

B DB ' NOT MATCHED$'

.CODE

MAIN PROC

MOV AX, @DATA

MOV DS, AX

MOV AH, 1

INT 21H

MOV BL, AL

CMP BL, 'Y'

18
JE L1

CMP BL, 'y'

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:

MOV AH, 4CH

INT 21H

MAIN ENDP

END MAIN

Output:

20
Explanation: .MODEL SMALL

21
.STACK 100H

These lines work as header files.

Then inside our data segment we will declare some messages as,

A DB ' MATCHED WITH $'

B DB ' NOT MATCHED$'

To let the code segment know about these messages we have to initialize them
inside our code segment like,

MOV AX, @DATA

MOV DS, AX

Now for user input we have to write,

MOV AH, 1: Input function.

INT 21H: Interrupt function which interrupts the program and search for
immediate function to execute.

MOV BL, AL: Input is taken through AL and moved in BL register.

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.

JE L1: If it matches with Y then it will jump to level L1.

22
Then again like this we can take as many characters as we want and compare them
with user input.

Our second comparison:

CMP BL, 'y'

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’.

JMP L2: Jumps to level L2.

In L2 level there are some instructions for printing the message.

L2:

MOV AH, 9: Output function to print string.

LEA DX, B: The message in variable B is loaded in DX register.

INT 21H: Interrupt function.

JMP EXIT: Then it will jump to EXIT and end the program.

The L1 level is then declared as,

L1:

MOV AH, 9: Output function to print strings.

LEA DX, A: The message in variable A is loaded in DX.

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.

MOV DL, BL: The user input in BL is moved in DL to display.

INT 21H: Interrupt function for the interruption.

To end our program we write,

EXIT:

MOV AH, 4CH

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 BX, 1111000011111000B

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:

MOV AH, 4CH

INT 21H

MAIN ENDP

END MAIN

26
Output:

Explanation: .MODEL SMALL

.STACK 100H

.CODE

These lines work as header files.

Then inside the main function our actual program starts.

First we have to declare a binary value. For that we write,

MOV BX, 1111000011111000B: This binary number is copied in BX.

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.

MOV AX, 0: AX is used to keep the count of 1 of binary number. We have


initialized AX=0.

Now to declare a loop we write,

TOP:

ROL BX, 1: Rotate Left OPCODE is used here.

JNC NEXT: Jump Not Carry means if there is no carry in the number then it will
jump to NEXT level.

INC AX: If there is carry then AX will be increased.

NEXT: Level NEXT.

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.

Then the printing part begins.

MOV AH, 2: Output function.

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.

INT 21H: Interrupt function for interruption.

To end our program we write,

28
EXIT:

MOV AH, 4CH

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

You might also like