0% found this document useful (0 votes)
13 views46 pages

MPMC Lab Final

This laboratory manual outlines the Microprocessors, Microcontrollers, and Computer Networks Lab for the Electronics & Communication Engineering course at Rajiv Gandhi University of Knowledge Technologies for the academic year 2024-2025. It includes a list of experiments focusing on assembly language programming for tasks such as addition of numbers, matrix operations, and conversion from Non-BCD to BCD. Each experiment provides code examples, explanations, and aims to enhance students' understanding of microprocessor operations.

Uploaded by

Official001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views46 pages

MPMC Lab Final

This laboratory manual outlines the Microprocessors, Microcontrollers, and Computer Networks Lab for the Electronics & Communication Engineering course at Rajiv Gandhi University of Knowledge Technologies for the academic year 2024-2025. It includes a list of experiments focusing on assembly language programming for tasks such as addition of numbers, matrix operations, and conversion from Non-BCD to BCD. Each experiment provides code examples, explanations, and aims to enhance students' understanding of microprocessor operations.

Uploaded by

Official001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

MICROPROCESSORS, MICROCONTROLLERS AND

COMPUTER NETWORKS LAB

Laboratory Manual

Course Code: 20EC3182

Applicable for the batch of students admitted for the

Academic Year 2024-2025

DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING

RAJIV GANDHI UNIVERSITY OF KNOWLEDGE TECHNOLOGIES

SRIKAKULAM

RGUKT Srikakulam, Department of ECE


Rajiv Gandhi University of knowledge Technologies
Srikakulam

MP and MC Lab Record

Department of Electronics and Communication Engineering

Lab Record Submitted by

Name of the Student :

ID Number :

Department :

Class :

RGUKT Srikakulam, Department of ECE


List of Experiments

S.No. PROGRAM NAME


8086 Programming
1.
2.
3.
4.
5.
6.
7

8
9
10

MIPS 32 Programming
1.
2.
3.
4.
5.
6.
7

8
9
10

RGUKT Srikakulam, Department of ECE 3


8086 Programming

Experiment: 1

RGUKT Srikakulam, Department of ECE 4


Aim: Write an assembly language code to implement addition of two 64 bit numbers

Code:
ORG 100H ; Set the origin to 100H, the starting address for the program.
MOV CX, 02 ; Load CX with 2, setting up a loop to repeat twice.
MOV SI, 1000H ; Set SI to 1000H, which points to the starting address of the first 16-bit
data.
MOV DI, 2000H ; Set DI to 2000H, which points to the starting address for storing
results.

SR: MOV AX, [SI] ; Move the 16-bit value from the address pointed to by SI into AX.
INC SI ; Increment SI by 1 to point to the next byte (prepares for the next 16-
bit value).
INC SI ; Increment SI again to fully skip the current 16-bit value (total
increment is 2).
MOV BX, [SI] ; Move the next 16-bit value (pointed to by updated SI) into BX.
ADD AX, BX ; Add the values in AX and BX. The result is now in AX.
MOV [DI], AX ; Store the result from AX into the address pointed to by DI.
INC SI ; Increment SI to move to the next data pair for the next iteration.
INC SI ; Second increment to fully skip the current 16-bit value (move to next
16-bit value).
INC DI ; Increment DI by 1 to store the next result in the following 16-bit
address.
INC DI ; Second increment of DI (total increment is 2 for 16-bit spacing).
DEC CX ; Decrease CX by 1 to track the number of iterations left.
JNZ SR ; If CX is not zero, jump back to SR to repeat the process.

INT 21 ; Interrupt to return control to DOS (optional, depending on the


system).
RET ; Return from the program to the operating system.

Explanation

1. Initialize Registers:
o MOV CX, 02: Sets CX to 2, indicating that the loop will run twice.
o MOV SI, 1000H: Sets SI to the starting addresses 1000H to read the first pair of
16-bit data.
o MOV DI, 2000H: Sets DI to the starting addresses 2000H to store the result.

2. Loop Start (SR):

RGUKT Srikakulam, Department of ECE 5


o MOV AX, [SI]: Loads the 16-bit data at the memory address pointed to by SI
into AX.
o INC SI and INC SI: Increments SI by 2, moving to the next 16-bit data location.
o MOV BX, [SI]: Loads the next 16-bit data (pointed to by SI after incrementing)
into BX.
o ADD AX, BX: Adds the contents of BX to AX. The result is stored in AX.

3. Store the Result:


o MOV [DI], AX: Stores the result of the addition (now in AX) into the memory
location pointed to by DI.
o INC SI and INC SI: Moves SI to point to the next pair of 16-bit data for the next
iteration.
o INC DI and INC DI: Moves DI to the next 16-bit location for storing the next
result.

4. Loop Control:
o DEC CX: Decrements CX by 1, reducing the loop counter.
o JNZ SR: Checks if CX is not zero. If CX is non-zero, it jumps back to SR to repeat
the process.

5. Program Termination:
o INT 21: Ends the program and returns control to the DOS operating system.
o RET: Returns from the subroutine, marking the end of the code.

The code reads and adds pairs of 16-bit values from memory starting at 1000H, stores the
results at 2000H, and repeats this process twice. Each iteration handles the following:

 Reads two 16-bit values (one into AX and one into BX).
 Adds these values and stores the result at the address pointed to by DI.
 Moves to the next pair of values and stores the result in the next memory location.

This process continues for the two iterations specified by the initial value of CX = 2.

Results:
Input:

Output:

RGUKT Srikakulam, Department of ECE 6


Experiment 2:
AIM: Write an assembly language code to implement addition of two 2x2 matrix

Code:
ORG 100H ; Sets the origin to 100H for this program.
MOV SI, 1000H ; Load 1000H into SI (Source Index).
MOV DI, 1010H ; Load 1010H into DI (Destination Index).
MOV CL, 04H ; Load 4 into CL, used as a loop counter (indicating 4 iterations).
MOV BP, 1020H ; Load 1020H into BP, the starting address to store results.
L2: MOV AL, [SI] ; Load the byte from the memory address pointed to by SI into

AL.
ADD AL, [DI] ; Add the byte from the memory address pointed to by DI to
AL.
MOV [BP], AL ; Store the result from AL into the memory address pointed to
by BP.
INC SI ; Increment SI to point to the next byte in memory.
INC DI ; Increment DI to point to the next byte in memory.
MOV AL, 00H ; Set AL to 00H (though this value is overwritten in the next
loop iteration).
INC BP ; Increment BP to point to the next storage location.
LOOP L2 ; Decrement CL and jump to L2 if CL is not zero.

RET ; Return from the program (in this case, return control to the
OS).
Explanation:

1. Setting Up Pointers and Counter:


o MOV SI, 1000H: SI points to the memory location 1000H, where the first set of
data is located.
o MOV DI, 1010H: DI points to 1010H, where the second set of data is located.
o MOV CL, 04H: CL is loaded with 4, setting up a loop to repeat 4 times.
o MOV BP, 1020H: BP points to 1020H, the location where results will be stored.

2. Loop (L2):
o Load and Add Data:
 MOV AL, [SI]: Load the byte from the memory location pointed to by SI
(currently 1000H) into AL.
 ADD AL, [DI]: Add the byte from the memory location pointed to by DI
(currently 1010H) to AL. Now AL contains the sum of the two bytes.

RGUKT Srikakulam, Department of ECE 7


o Store Result:
 MOV [BP], AL: Store the resulting value in AL into the memory location
pointed to by BP (currently 1020H).
o Increment Pointers:
 INC SI: Increment SI to point to the next byte in the 1000H range.
 INC DI: Increment DI to point to the next byte in the 1010H range.
o Clear AL and Increment BP:
 MOV AL, 00H: Sets AL to 00H, though this is overwritten on each
iteration, so it has no real impact on the loop’s result.
 INC BP: Increment BP to store the next result in the following address
in 1020H range.
o Loop Control:
 LOOP L2: This instruction decrements CL by 1 and jumps to L2 if CL is
not zero. This loop runs four times as CL was initialized to 04H.

3. Exit the Program:


o RET: Returns control to the calling program or operating system.

Results:

Input:

Output:

RGUKT Srikakulam, Department of ECE 8


Experiment 3:
n
Aim: Write an assembly language code to implement Summation function ∑ x i
xi

Code:
ORG 100H ; Set the code origin at 100H (for COM file).
MOV SI, 1000H ; Load the starting address of the first 4 bytes into SI.
MOV DI, 1010H ; Load the starting address of the second 4 bytes into DI.
MOV BP, 1020H ; Load the starting address where the results will be stored into BP.
MOV AX, 0000H ; Clear AX register (will store the sum of bytes from the first set).
MOV BX, 0000H ; Clear BX register (will store the sum of bytes from the second set).
MOV CL, 04H ; Set the loop counter to 4 (for each byte in the sets).
L1: ADD AL, [SI] ; Add the byte at [SI] to AL (accumulating in AL).
INC SI ; Move to the next byte.
LOOP L1 ; Decrement CL and repeat if CL ≠ 0.
MOV CL, 04H ; Reset the loop counter to 4 for the second set.
L2: ADD BL, [DI] ; Add the byte at [DI] to BL (accumulating in BL).
INC DI ; Move to the next byte.
LOOP L2 ; Decrement CL and repeat if CL ≠ 0.
MOV [BP], AX ; Store the sum of the first set (from AX) at address 1020H.
ADD BP, 02H ; Move BP to the next address (1022H).
MOV [BP], DX ; Store the sum of the second set (from BX) at address 1022H.
HLT ; Halt the execution.

Explanation:

1. Setup:
- ORG 100H: Set the code origin at 100H.
- MOV SI, 1000H: Load starting address of the first 4 bytes into SI.
-MOV DI, 1010H: Load starting address of the second 4 bytes into DI.
- MOV BP, 1020H: Load starting address to store results into BP.
- MOV AX, 0000H and MOV BX, 0000H: Clear AX and BX to store sums.

RGUKT Srikakulam, Department of ECE 9


2. Summing First Set of Bytes (L1 Loop):
- MOV CL, 04H: Set loop counter to 4 for 4 bytes.
- L1: ADD AL, [SI]: Add the byte at [SI] to AL (low byte of AX).
- INC SI: Move to the next byte in the first set.
- LOOP L1: Repeat until 4 bytes are summed (CL reaches 0).
- Result: AX now holds the sum of the first 4 bytes from 1000H.

3. Summing Second Set of Bytes (L2 Loop):


- MOV CL, 04H: Reset loop counter to 4.
- L2: ADD BL, [DI]: Add the byte at [DI] to BL (low byte of BX).
- INC DI: Move to the next byte in the second set.
- LOOP L2: Repeat until 4 bytes are summed.
- Result: BX now holds the sum of the second 4 bytes from 1010H.

4. Storing Results:
- MOV [BP], AX: Store the sum of the first set (from AX) at 1020H.
- ADD BP, 02H: Move BP to 1022H.
- MOV [BP], DX: Store the sum of the second set (from BX) at 1022H.

5. End Program:
- HLT: Halt the program.

Note: MOV BX is an unnecessary line and should be removed.

Results:

Input:

Output:

RGUKT Srikakulam, Department of ECE 1


0
Experiment 4:
Aim: Write an assembly language code to implement to convert the Non-BCD to BCD

Code:

ORG 100H ; Set origin to 100H, the starting address for .COM files in DOS
MOV CX, 0003H ; Initialize CX with 3, which will act as a loop counter
MOV SI, 1000H ; Set SI to 1000H, used as a pointer to the memory location

L2: MOV AL, [SI] ; Move the byte at memory address pointed by SI into AL
CMP AL, 09 ; Compare AL with 09H
JZ L3 ; If AL is equal to 09H, jump to L3
JNZ L1 ; If AL is not equal to 09H, jump to L1

L3: INC SI ; Increment SI to point to the next memory location


DEC CL ; Decrement the lower byte of CX (CL) to reduce loop counter
JNZ L2 ; If CL is not zero, jump back to L2 to continue the loop
INT 3

L1: ADD AL, 06 ; Add 06 to the value in AL


JMP L3 ; unconditionally jump to L3

INT 3 ; Trigger software interrupt 3, used as a breakpoint for debugging


RET ; Return from the procedure or program

Explanation:
1. Set Starting Address:
- ORG 100H: Sets the code origin to 100H, which is the default starting address for .COM
files in DOS.

2. Initialize Registers:
- MOV CX, 0003H: Loads CX register with 0003H, which will act as a loop counter.

RGUKT Srikakulam, Department of ECE 1


1
- MOV SI, 1000H: Sets SI to 1000H, which will be used as a pointer to a memory location
starting at address 1000H.

3. Loop (L2 Label):


- MOV AL, [SI]: Loads a byte from the memory address pointed by SI into AL.
- CMP AL, 09: Compares the byte in AL with 09H.
- Conditional Jumps:
- JZ L3: If AL is equal to 09H, it jumps to L3.
- JNZ L1: If AL is not 09H, it jumps to L1.

4. Process Byte if Equal to 09H (L3 Label):


- INC SI: Increments SI to point to the next memory location.
- DEC CL: Decreases the lower byte of CX (CL) by 1 to update the loop counter.
- JNZ L2: If CL is not zero, it jumps back to L2 to continue the loop.

5. Process Byte if Not Equal to 09H (L1 Label):


- ADD AL, 06: Adds 06 to the value in AL.
- JMP L3: Unconditionally jumps back to L3.

6. End and Debugging:


- INT 3: Triggers interrupt 3, commonly used as a breakpoint for debugging.
- RET: Returns control from the procedure or program, ending the routine.

Results:

Input:

Output

RGUKT Srikakulam, Department of ECE 1


2
Experiment 5:
Aim:
i. Write an assembly language code to implement the series sum of n Numbers
ii. Write an assembly language code to implement the series sum of n2 Numbers
iii. Write an assembly language code to implement the series sum of n3 Numbers

Code:
I)
ORG 100H ; Set the code origin to address 100H. This is typical in COM programs
for DOS, where code starts at offset 100H in the segment.
MOV AX, 00H ; Initialize AX register to 0. AX will be used to accumulate the sum.
MOV BX, 01H ; Initialize BX register to 1. BX will be incremented and added to AX in
each iteration.
MOV CX, 100H ; Initialize CX register to 256 (in hexadecimal, 100H). CX will be used as
a counter to control the loop's iteration count.
L1: ADD AX, BX ; Add the value of BX to AX. AX accumulates the sum of
BX values in each iteration.
INC BX ; Increment BX by 1, so it adds the next higher number in each
loop iteration.
LOOP L1 ; Decrement CX by 1, and if CX is not zero, jump back to label
L1 to continue the loop, This loop will run 100 times as
specified by the initial value of CX.
INT 3 ; Trigger a breakpoint interrupt. This is typically used for debugging
to stop the program.
RET ; Return from the program. In DOS, this would exit to the operating
system if running as a .COM file.

Results:

Input:

Output

II)

RGUKT Srikakulam, Department of ECE 1


3
ORG 100H ; Set the origin to 100H (starting address for code in .COM files)
MOV CL, 03H ; Load 3 into CL. CL will act as the loop counter (run the loop 3 times).
MOV AL, 01H ; Initialize AL to 1. AL will hold the number to be squared.
MOV BL, 00H ; Initialize BL to 0. BL will temporarily hold values for incrementing AL.
MOV DL, 00H ; Initialize DL to 0. DL will be used to hold the value of AL during the
loop.
L1: MOV DL, AL ; Copy AL (the current number to square) into DL for temporary
storage.
MUL AL ; Multiply AL by itself (square it). The result is stored in AX.
ADD BX, AX ; Add the result (AX, the square of AL) to BX. BX accumulates the sum of

squares.
MOV AL, DL ; Reset AL with the value in DL (which is 0 initially, but will change as
we increment AL).
INC AL ; Increment AL by 1, preparing for the next number to square in the
next iteration.
LOOP L1 ; Decrement CL by 1 and repeat the loop if CL is not zero.
HLT ; Halt the program (end execution).

Explanation:
- ORG 100H: This directive sets the starting address for the program code at `100H`,
commonly used in `.COM` programs.
- MOV CL, 03H: Load 3 into CL. This sets up CL as a loop counter for the LOOP L1 instruction,

meaning the loop will run 3 times.


- MOV AL, 01H: Initialize AL with 1, representing the first number to be squared.
- MOV BL, 00H: Initialize BL to 0. Here, BL acts as a temporary placeholder for resetting AL
to 0 before incrementing it in each loop iteration.
- MOV DL, 00H: Initialize DL to 0. DL will store the value of AL temporarily within the loop.
- L1: MOV DL, AL: In each iteration of the loop, copy the current value of AL (the number we
want to square) into DL for storage.
- MUL AL: This instruction multiplies `AL` by itself, effectively squaring the value in `AL`. The

result (square of `AL`) is stored in `AX`.


- ADD BX, AX: Add the squared value in AX to BX. BX is used to accumulate the sum of
squares.
- MOV AL, BL: After adding the square to BX, reset AL to DL (which is 0 at this point) in
preparation to increment it by 1.
- INC AL: Increment AL by 1, setting up AL with the next number to square in the next
iteration of the loop.

RGUKT Srikakulam, Department of ECE 1


4
- LOOP L1: Decrement CL by 1 and check if it’s zero. If CL is not zero, jump back to the label
L1 and repeat the loop. Since CL starts at 3, the loop will iterate 3 times (for AL values 1, 2,
and 3).
- HLT: Halt the program, ending execution.

Results:

Input:

Output
III)
ORG 100H ; Set the origin to 100H (starting address for code in .COM files)
MOV CL, 03H ; Load 3 into CL. CL will act as the loop counter (run the loop 3 times).
MOV AL, 01H ; Initialize AL to 1. AL will hold the number to be squared.
MOV BL, 00H ; Initialize BL to 0. BL will temporarily hold values for incrementing AL.
MOV DL, 00H ; Initialize DL to 0. DL will be used to hold the value of AL during the
loop.
L1: MOV DL, AL ; Copy AL (the current number to square) into DL for temporary
storage.
MUL AL ; Multiply AL by itself (square it). The result is stored in AX.
MUL AL ; Multiply AL by itself (square it). The result is stored in AX.
ADD BX, AX ; Add the result (AX, the square of AL) to BX. BX accumulates the sum of

squares.
MOV AL, DL ; Reset AL with the value in DL (which is 0 initially, but will change as
we increment AL).
INC AL ; Increment AL by 1, preparing for the next number to square in the
next iteration.
LOOP L1 ; Decrement CL by 1 and repeat the loop if CL is not zero.
HLT ; Halt the program (end execution).

Results:

Input:

Output

RGUKT Srikakulam, Department of ECE 1


5
Experiment 6:
Aim: Write an assembly language code to implement to find minimum number in the given
array of matrix

Code:
ORG 100H ; Set the origin to 100H (starting address for code in .COM files)
MOV CL, 04H ; Load 4 into CL. CL will act as a loop counter, meaning the loop will run

4 times.
MOV SI, 1000H ; Initialize the source index (SI) to memory address 1000H. This points
to the start of the byte sequence.
MOV AL, [SI] ; Load the first byte at memory address [SI] (1000H) into AL. AL will
store the maximum value found.
DEC CX ; Decrement CX by 1, since we've already loaded the first byte into AL.
L1: INC SI ; Increment SI to point to the next byte in memory.
CMP AL, [SI] ; Compare the current maximum value in AL with the byte at
[SI].
JC L2 ; If AL is less than [SI], jump to L2 to update AL with the new
maximum.
MOV AL, [SI] ; (This line is only reached if AL is greater than or equal to [SI]).
Update AL with the new maximum value.
L2: LOOP L1 ; Decrement CX by 1 and repeat the loop if CX is not zero.
RET ; Return from the program (end execution).

Explanation:

 ORG 100H: This directive sets the starting address for the program at 100H, which is
commonly used in .COM programs.

RGUKT Srikakulam, Department of ECE 1


6
 MOV CL, 04H: Load 4 into CL. This is the loop counter, so the loop will run 4 times, which
means it expects a sequence of 4 bytes in memory starting at 1000H.
 MOV SI, 1000H: Initialize SI with 1000H. This points to the starting memory address of the
data to be scanned.
 MOV AL, [SI]: Load the byte at memory address [SI] (starting at 1000H) into AL. This sets AL
to the first byte, which initially assumes to be the maximum value.
 DEC CX: Decrement CX by 1 since the first byte is already loaded into AL and doesn’t need
to be rechecked.
 L1: INC SI: Increment SI to point to the next byte in memory.
 CMP AL, [SI]: Compare the current maximum value in AL with the byte at [SI]. This checks
if the new byte is larger than the value in AL.
 JC L2: If the value in AL is less than the value at [SI], the JC (Jump if Carry) instruction will
transfer control to L2, where AL is updated.
 MOV AL, [SI]: Update AL with the new maximum value if the previous comparison
showed that [SI] was larger than AL.
 L2: LOOP L1: Decrement CX by 1 and check if it’s zero. If CX is not zero, jump back to the
label L1 to continue scanning the next byte in memory. The loop will continue until all
bytes are checked (in this case, 4 bytes in total).
 RET: This instruction ends the program by returning control to the operating system.

Results:

Input:

Output

RGUKT Srikakulam, Department of ECE 1


7
Experiment 7:
Aim: Write an assembly language code to implement to find maximum number in the given
array of matrix

Code:
ORG 00H ; Set the origin to 00H (starting address for the code).
MOV CL, 04H ; Load 4 into CL. CL will act as a loop counter, indicating the number of
bytes to compare.
MOV SI, 1000H ; Initialize SI with the memory address 1000H. This is the starting point

for the byte sequence.


MOV AL, [SI] ; Load the first byte from memory address [SI] (1000H) into AL. AL will

hold the maximum value found.


L3: CMP AL, [SI+1] ; Compare the current maximum (in AL) with the next byte in
memory ([SI+1]).
JC L1 ; If AL is less than [SI+1], jump to L1 to update AL with the new
maximum.
INC SI ; Move to the next byte by incrementing SI.
DEC CL ; Decrement CL (the loop counter).
JZ EXIT ; If CL reaches zero, jump to EXIT and end the loop.
JMP L3 ; Jump back to L3 to continue comparing the next byte.
L1: MOV AL, [SI+1] ; If a larger value was found, update AL with the new maximum
(from [SI+1]).
DEC CL ; Decrement CL to continue the loop.

RGUKT Srikakulam, Department of ECE 1


8
INC SI ; Move SI to point to the next byte.
JNZ L3 ; Jump back to L3 if CL is not zero to continue the loop.
INT 3 ; Interrupt for debugging (pauses execution if in a debugging
environment).
EXIT: INT 3 ; Another debugging interrupt. This is the program exit point.
RET ; Return from the program, ending execution.

Explanation:

 ORG 00H: This sets the origin (starting point) for the code at address 00H. This
is typical for small programs that don’t rely on DOS segments.
 MOV CL, 04H: Loads 4 into CL. This value acts as a loop counter, so the loop will
iterate 4 times, scanning 4 bytes in memory.
 MOV SI, 1000H: Initializes SI to 1000H, which points to the start of the memory
range where the bytes are located.
 MOV AL, [SI]: Loads the first byte from the memory address [SI] (1000H) into
AL, initially assuming it to be the maximum value.
 L3: CMP AL, [SI+1]: Compares the current maximum value in AL with the next
byte in memory ([SI+1]).
 JC L1: If the byte at [SI+1] is greater than the current maximum (in AL), the JC
(Jump if Carry) instruction jumps to L1, where AL is updated with this new
maximum.
 INC SI: Increments SI to move to the next byte in memory for the next
comparison.
 DEC CL: Decrements CL (the loop counter) by 1.
 JZ EXIT: If CL reaches zero, it means all bytes have been checked, so JZ (Jump if
Zero) transfers control to the EXIT label to finish the program.
 JMP L3: Jumps back to L3 to continue comparing the next byte.
 L1: MOV AL, [SI+1]: If a new maximum was found, update AL with this new
value.
 DEC CL: Decrement the loop counter CL.
 INC SI: Increment SI to point to the next byte in memory.
 JNZ L3: Jump back to L3 if CL is not zero, meaning there are more bytes to
compare.
 INT 3: Triggers a software interrupt used for debugging, often pausing
execution in a debugger.
 EXIT: INT 3: Another debugging interrupt, indicating the end of the program.
 RET: Ends the program and returns control to the operating system.

RGUKT Srikakulam, Department of ECE 1


9
Results:

Input:

Output:

Experiment 8:
Aim: Write an assembly language code to implement sorting in ascending order

Code:
ORG 100H ; Set the origin to 100H (starting address for the code in .COM
files)
MOV CH, 05H ; Load 5 into CH. This is the outer loop counter, representing the
number of passes in the sort.
LOOP2: MOV CL, 05H ; Start of outer loop. Load 5 into CL. CL is the inner loop
counter, representing the number of comparisons in each
pass.
MOV SI, 2000H ; Initialize SI to memory address 2000H. This points to the
start of the data to be sorted.
LOOP1: MOV AL, [SI] ; Load the current byte at [SI] into AL.
MOV BL, [SI+1] ; Load the next byte (at [SI+1]) into BL.
CMP AL, BL ; Compare the current byte (AL) with the next byte (BL).
JC EXIT ; If AL is less than BL, jump to EXIT (no swap needed since
they're in order)
MOV DL, [SI+1] ; If AL is greater than BL, prepare to swap them by loading
BL into DL.
XCHG [SI], DL ; Exchange the byte at [SI] (AL) with DL (which holds
[SI+1]).
MOV [SI+1], DL ; Store DL back into [SI+1], completing the swap.

RGUKT Srikakulam, Department of ECE 2


0
EXIT: INC SI ; Move SI to the next pair of bytes in memory.
DEC CL ; Decrement the inner loop counter.
JNZ LOOP1 ; If CL is not zero, repeat the inner loop (continue
comparing the next pair).
DEC CH ; Decrement the outer loop counter (reducing the number
of remaining passes).
JNZ LOOP2 ; If CH is not zero, repeat the outer loop (another pass over
the data).
INT 3 ; Trigger an interrupt for debugging (pauses execution in a debugger).

Explanation:

 ORG 100H: Sets the origin (starting point) of the code at address 100H, which
is common for .COM programs.
 MOV CH, 05H: Loads 5 into CH, which serves as the outer loop counter. This
means the outer loop will perform 5 passes over the data to ensure all
elements are sorted.

Outer Loop (LOOP2):

 LOOP2: MOV CL, 05H: The outer loop starts here. Each time LOOP2 begins, it
reloads CL with 5, setting up the inner loop to perform 5 comparisons.
 MOV SI, 2000H: Initializes SI to point to the starting address 2000H in memory,
where the data sequence begins.

Inner Loop (LOOP1):

 MOV AL, [SI]: Loads the byte at the current SI address into AL, representing the
first of a pair of bytes to compare.
 MOV BL, [SI+1]: Loads the byte at SI+1 into BL, representing the second byte in
the pair to compare.
 CMP AL, BL: Compares AL (first byte) with BL (second byte).
 JC EXIT: If AL is less than BL, no swap is necessary because the bytes are in
order. JC (Jump if Carry) skips the swap and jumps to EXIT.
 MOV DL, [SI+1]: If AL is greater than BL, prepare to swap by loading the byte at
SI+1 into DL.
 XCHG [SI], DL: Exchanges the value in DL (which holds BL) with the byte at [SI],
swapping the values at [SI] and [SI+1].
 MOV [SI+1], DL: Writes the swapped value back to [SI+1], completing the
exchange of adjacent bytes.

RGUKT Srikakulam, Department of ECE 2


1
 EXIT: INC SI: Increments SI to move to the next pair of bytes.
 DEC CL: Decrements the inner loop counter (CL). This counter tracks the
number of comparisons to make within the current pass.
 JNZ LOOP1: If CL is not zero, jump back to LOOP1 to continue comparing the
next pair of bytes.

Outer Loop Control:

 DEC CH: Decrements the outer loop counter CH. This reduces the number of
remaining passes by 1.
 JNZ LOOP2: If CH is not zero, jump back to LOOP2 to start another pass through
the data.
 INT 3: Triggers a software interrupt for debugging, pausing the program if in a
debugging environment.

Results:

Input:

Output:

RGUKT Srikakulam, Department of ECE 2


2
Experiment 9:
Aim: Write an assembly language code to implement sorting in Descending order

Code:
ORG 100H ; Set the origin to 100H (starting address for the code in .COM
files)
MOV CH, 04H ; Load 4 into CH. This is the outer loop counter, indicating the
number of passes needed for sorting.
LOOP1: MOV CL, 04H ; Start of the outer loop. Load 4 into CL, which is the inner
loop counter.
MOV SI, 2000H ; Initialize SI to memory address 2000H. This points to the
start of the data sequence.
MOV AL, [SI] ; Load the first byte from memory (at address [SI]) into AL,
the first byte to compare.
LOOP2: MOV BL, [SI+1] ; Load the next byte (at address [SI+1]) into BL.
CMP AL, BL ; Compare the current byte in AL with the next byte in BL.
JNC LOOP3 ; If AL is less than or equal to BL, no swap is needed; jump
to LOOP3 to move to the next byte.
MOV DL, [SI+1] ; If AL is greater than BL, swap them. Load BL (the byte at
[SI+1]) into DL.
XCHG [SI], DL ; Exchange the value in DL with the byte at [SI], effectively
swapping AL and BL.
MOV [SI+1], DL ; Store DL back into [SI+1], completing the swap.
LOOP3: INC SI ; Move SI to the next byte in memory.

RGUKT Srikakulam, Department of ECE 2


3
DEC CL ; Decrement the inner loop counter (CL).
JNZ LOOP3 ; If CL is not zero, repeat the inner loop (go back to LOOP3).
DEC CH ; Decrement the outer loop counter (CH) after each pass.
JNZ LOOP1 ; If CH is not zero, repeat the outer loop (start another
sorting pass).
INT 3 ; Interrupt for debugging (pauses execution in a debugger).

Explanation:

 ORG 100H: This sets the starting address for the program to 100H, typical for
small assembly programs in .COM format.
 MOV CH, 04H: Loads 4 into CH, which is used as the outer loop counter,
representing the number of passes over the data to ensure it is sorted.

Outer Loop (LOOP1)

 LOOP1: MOV CL, 04H: Begins the outer loop, where CL is set to 4 each time.
This value serves as the inner loop counter, representing the number of
comparisons per pass.
 MOV SI, 2000H: Sets SI to point to 2000H, the start address of the byte
sequence in memory.
 MOV AL, [SI]: Loads the byte at [SI] into AL. This byte will be compared with
the next byte in the sequence.

Inner Loop (LOOP2)

 LOOP2: MOV BL, [SI+1]: Loads the byte at [SI+1] into BL for comparison with
AL.
 CMP AL, BL: Compares the byte in AL with the byte in BL.
 JNC LOOP3: If AL is less than or equal to BL, jump to LOOP3 to move to the next
byte pair without swapping.
 MOV DL, [SI+1]: If AL is greater than BL, load the byte in BL into DL to prepare
for a swap.

RGUKT Srikakulam, Department of ECE 2


4
 XCHG [SI], DL: Exchanges the value in DL with the byte at [SI]. This effectively
swaps the values in [SI] and [SI+1].
 MOV [SI+1], DL: Stores the swapped value in DL back to [SI+1], completing the
swap.

Increment and Decrement for Inner Loop

 LOOP3: INC SI: Increments SI to move to the next byte pair in memory.
 DEC CL: Decreases the inner loop counter CL.
 JNZ LOOP3: If CL is not zero, jump back to LOOP3 to compare the next pair of
bytes.

Decrement for Outer Loop

 DEC CH: Decrements the outer loop counter CH after completing an inner loop
pass.
 JNZ LOOP1: If CH is not zero, repeat the outer loop (another sorting pass over
the entire array).
 INT 3: Triggers an interrupt for debugging, pausing the program if run in a
debugger.

Results:

Input:

Output:

RGUKT Srikakulam, Department of ECE 2


5
Experiment 10:
Aim: Write an assembly language code to find factorial of given number

Code:
ORG 100H ; Set the origin to 100H (starting address for the code in .COM
files)
MOV AX, 01H ; Initialize AX to 1. AX will hold the running factorial result.
MOV CX, 05H ; Load 5 into CX. CX acts as the loop counter for the factorial
calculation.
MOV BX, 01H ; Initialize BX to 1. BX will hold the current multiplier in each loop

iteration.
L1: MUL BX ; Multiply AX by BX. The result (AX * BX) is stored in AX.
INC BX ; Increment BX by 1 for the next multiplication.
LOOP L1 ; Decrement CX by 1. If CX is not zero, repeat the loop (jump back
to L1).
HLT ; Halt the program (end of execution).

Explanation

 ORG 100H: Sets the starting address for the code at 100H, which is common
for .COM files in DOS.
 MOV AX, 01H: Initializes AX to 1. AX will hold the running result of the
factorial calculation.

RGUKT Srikakulam, Department of ECE 2


6
 MOV CX, 05H: Loads 5 into CX, which acts as the loop counter. This loop will
iterate 5 times, corresponding to the multipliers 1 through 5.
 MOV BX, 01H: Initializes BX to 1. BX serves as the current multiplier in each
iteration.

Loop (L1)

 L1: MUL BX: Multiplies AX by BX, storing the result back in AX. This
progressively builds up the factorial:
o In the first iteration, AX = 1 * 1 = 1.
o In the second iteration, AX = 1 * 2 = 2.
o In the third iteration, AX = 2 * 3 = 6.
o In the fourth iteration, AX = 6 * 4 = 24.
o In the fifth and final iteration, AX = 24 * 5 = 120.

 INC BX: Increments BX by 1 to use the next integer in the sequence for the
following multiplication.
 LOOP L1: Decrements CX by 1. If CX is not zero, LOOP jumps back to L1 to
repeat the loop. After 5 iterations, CX reaches zero, and the loop ends.
 HLT: Halts the program, ending execution.

Results:

Input:

Output:

RGUKT Srikakulam, Department of ECE 2


7
MIPS-32 Programming

RGUKT Srikakulam, Department of ECE 2


8
Experiment 1:

Aim: Write MIPS Program to add two numbers in memory and store the result in the next
location

Code:
.text
.globl main
main: la $t0,value
lw $t1,0($t0)
lw $t2,4($t0)
add $t3,$t1,$t2
sw $t3,8($t0)
.data
value: .word 50 30 0

Results:

Input:

RGUKT Srikakulam, Department of ECE 2


9
Output:

Experiment 2:

Aim:Write MIPS Program to add two constant numbers specified as immediate data and
store the result in a register

Code:
.text
.globl main
main: add $t1, $zero, 0x2A
add $t2, $zero, 0xD0
add $s3, $t1, $t2

Result:

Input:

Output:

RGUKT Srikakulam, Department of ECE 3


0
Experiment 3:

Aim:Write MIPS Program to add two constant numbers specified as immediate data and
store the result in a register (use system call to exit)

Code:
.text
.globl main
main: add $t1, $zero, 0x2A
add $t2, $zero, 0xD0
add $s3, $t1, $t2

li $v0, 10
Syscall

Result:

Input:

RGUKT Srikakulam, Department of ECE 3


1
Output

Experiment 4:

Aim:Write MIPS Program to read two numbers from the keyboard and print the sum

Code:
.data
str1: .asciiz "Enter the first number:"
str2: .asciiz "Enter the second number:"
str3: .asciiz "The Sum is = :"

.text
.globl main
main: li $v0,4
la $a0,str1 #print string
syscall

li $v0, 5 #read_integer
syscall
move $t0,$v0

RGUKT Srikakulam, Department of ECE 3


2
li $v0, 4 #print string
la $a0, str2
syscall

li $v0,5 #read number


syscall
move $t1, $v0

add $t1, $t0, $t1 #$t1=$t0+$t1

li $v0,4
la $a0,str3
syscall

li $v0,1
move $a0,$t1
syscall

li $v0,10
syscall

Result:

Input:

Output

RGUKT Srikakulam, Department of ECE 3


3
Experiment 5:

Aim:Write MIPS Program to calculate sum of 10 32-bi numbers stored in consecutive


memory locations

Code:
.data
num: .word 1 2 3 4 5 6 7 8 9 10
.text
.globl main
main: la $t0, num
li $t2,0 #to store sum of the value
li $t3,0 #count number
loop: lw $t1, 0($t0)
add $t2, $t2, $t1
addi $t3, $t3,1
addi $t0, $t0,4 # point to next
bne $t3,10,loop

li $v0,10

RGUKT Srikakulam, Department of ECE 3


4
syscall

Result:

Input:

Output

Experiment 6:

Aim:Write MIPS Program to check if a given number is a palindrome

Code:
.data
num: .word 0
msg: .asciiz "Enter the number"
msg1: .asciiz "Palindrome"
msg2: .asciiz "Not a Palindrome"

.text
.globl main
main: li $v0,4
la $a0, msg
syscall

li $v0,5
syscall
move $t0,$v0

RGUKT Srikakulam, Department of ECE 3


5
move $t3, $t0
li $t2,0

loop: mul $t2,$t2,10


rem $t1,$t0,10
div $t0,$t0,10
add $t2,$t2,$t1
bne $t0,$zero,loop

bne $t3,$t2,np
li $v0,4
la $a0,msg1
syscall
li $v0,10
syscall

np:
li $v0,4
la $a0,msg2
syscall

li $v0,10
syscall

Result:

Input:

Output

RGUKT Srikakulam, Department of ECE 3


6
Experiment 7:Write a mips program for Function call and return

Program:
.data
num1:word 14
num2:word 15
num3:word 0

.text
.global main
main:
lw $t0,num1
lw $t1,num2
jal sumfunc
sw $t1,sum
li $v0,10
syscall

Sumfunc:
add $t1, $t1, $t0

RGUKT Srikakulam, Department of ECE 3


7
jr $ra

Experiment 8:

Aim:Write MIPS Program to generate Fibonacci sequence

Code:
.data
prompt: .asciiz "Enter the number of Fibonacci numbers to generate: "
newline: .asciiz "\n"

.text
.globl main

main:
# Prompt user to input the number of Fibonacci terms
li $v0, 4 # Syscall: print string
la $a0, prompt # Address of prompt string
syscall

li $v0, 5 # Syscall: read integer


syscall

RGUKT Srikakulam, Department of ECE 3


8
move $t0, $v0 # Store input in $t0 (n)

# Initialize Fibonacci variables


li $t1, 0 # $t1 = 0 (F0)
li $t2, 1 # $t2 = 1 (F1)
li $t3, 0 # Counter = 0

fib_loop:
# Print the current Fibonacci number ($t1)
li $v0, 1 # Syscall: print integer
move $a0, $t1 # Load $t1 into $a0
syscall

# Print a newline
li $v0, 4
la $a0, newline
syscall

# Check if we have printed n numbers


addi $t3, $t3, 1 # Counter++
bge $t3, $t0, exit # If counter >= n, exit loop

# Calculate the next Fibonacci number


add $t4, $t1, $t2 # $t4 = $t1 + $t2
move $t1, $t2 # $t1 = $t2
move $t2, $t4 # $t2 = $t4
j fib_loop # Repeat loop

exit:
li $v0, 10 # Syscall: exit program
syscall

RGUKT Srikakulam, Department of ECE 3


9
Result:

Input:

Output

Experiment 9:

Aim:Write MIPS Program for bubble sort

Code:

.data
numbers: .word 8, 100, 0, 3, 7 # Array to be sorted
size: .word 5 # Size of the array
message: .asciiz "Sorted Array: " # Message to be printed
newline: .asciiz "\n"

.text
.globl main

main:
la $s7, numbers # Load address of the array into $s7
lw $s6, size # Load the size of the array into $s6

# Bubble Sort
li $s0, 0 # Outer loop counter

RGUKT Srikakulam, Department of ECE 4


0
outer_loop:
blt $s0, $s6, inner_start # If s0 < size, start inner loop
j print_array # Exit sort when done

inner_start:
li $s1, 0 # Inner loop counter
la $t0, numbers # Reset $t0 to the start of the array

inner_loop:
sub $t2, $s6, $s0 # Calculate the number of comparisons
sub $t2, $t2, 1 # t2 = size - outer loop counter - 1
bge $s1, $t2, outer_increment # If inner loop counter >= size - s0 - 1, end inner loop

lw $t3, 0($t0) # Load numbers[j] into $t3


lw $t4, 4($t0) # Load numbers[j+1] into $t4

# Compare and swap if necessary


ble $t3, $t4, skip_swap # If numbers[j] <= numbers[j+1], skip swap
sw $t4, 0($t0) # Swap numbers[j] and numbers[j+1]
sw $t3, 4($t0)

skip_swap:
addi $t0, $t0, 4 # Move to the next pair
addi $s1, $s1, 1 # Increment inner loop counter
j inner_loop # Repeat inner loop

outer_increment:
addi $s0, $s0, 1 # Increment outer loop counter
j outer_loop # Repeat outer loop

RGUKT Srikakulam, Department of ECE 4


1
# Print the sorted array
print_array:
la $s7, numbers # Reset $s7 to the start of the array
li $t3, 0 # Initialize print counter

# Print message
li $v0, 4 # Syscall: print string
la $a0, message
syscall

print_loop:
lw $t4, 0($s7) # Load current number into $t4
li $v0, 1 # Syscall: print integer
move $a0, $t4
syscall

li $v0, 4 # Syscall: print space


la $a0, newline
syscall

addi $s7, $s7, 4 # Move to the next number


addi $t3, $t3, 1 # Increment print counter
blt $t3, $s6, print_loop # Continue until all numbers are printed

# Exit program
li $v0, 10 # Syscall: exit
syscall

Result:

RGUKT Srikakulam, Department of ECE 4


2
Input:

Output

Experiment 10:

Aim: Write a MIPS 32 program to implement Different ALU operations between two
numbers based on the user given data?

Code:
.data
str1: .asciiz"enter the first number:"
str2: .asciiz"enter the second number:"
str3: .asciiz"which operation you want:\n1-sum of two numbers\n2-diference of two
numbers\n3-product of two numbers\n4-the AND operation\n5-the OR operation\
nenter the number :"
str4: .asciiz"\nthe sum of two numbers is:"
str5: .asciiz"\nthe difference of two numbers is:"
str6: .asciiz"\nthe product of two numbers is:"
str7: .asciiz"\nthe AND operation is:"
str8: .asciiz"\nthe OR operation is:"
str9: .asciiz"\nplease enter the correct number(1,2,3,4,5):"
.text
.globl main
main: li $v0,4
la $a0,str1
syscall
li $v0,5

RGUKT Srikakulam, Department of ECE 4


3
syscall
move $t0,$v0

li $v0,4
la $a0,str2
syscall
li $v0,5
syscall
move $t1,$v0

li $v0,4
la $a0,str3
syscall
first:
li $v0,5
syscall
move $t2,$v0

beq $t2,1,case1
beq $t2,2,case2
beq $t2,3,case3
beq $t2,4,case4
beq $t2,5,case5
j default_case

case1: add $t3,$t0,$t1


li $v0,4
la $a0,str4
syscall
li $v0,1
move $a0,$t3
syscall
j end

case2: sub $t4,$t0,$t1


li $v0,4

RGUKT Srikakulam, Department of ECE 4


4
la $a0,str5
syscall
li $v0,1
move $a0,$t4
syscall
j end

case3: mul $t5,$t0,$t1


li $v0,4
la $a0,str6
syscall
li $v0,1
move $a0,$t5
syscall
j end

case4: and $t6,$t0,$t1


li $v0,4
la $a0,str7
syscall
li $v0,1
move $a0,$t6
syscall
j end

case5: or $t7,$t0,$t1
li $v0,4
la $a0,str8
syscall
li $v0,1
move $a0,$t7
syscall
j end

default_case: li $v0,4
la $a0,str9

RGUKT Srikakulam, Department of ECE 4


5
syscall
j first

end:
li $v0,10
syscall

Result:
Input:
Output:

RGUKT Srikakulam, Department of ECE 4


6

You might also like