0% found this document useful (0 votes)
12 views16 pages

Coa Lab 2

Computer Organization and Architecture Lab Task for Engineering Studnets

Uploaded by

Ihsan ul Haq
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)
12 views16 pages

Coa Lab 2

Computer Organization and Architecture Lab Task for Engineering Studnets

Uploaded by

Ihsan ul Haq
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/ 16

BRANCHING OPERATIONS

Fall 2023

COA LAB

Submitted by: NOOR UL HAQ

Registration no: 21pwcse2046

Section: A

Student Signature: ______________

Submitted to:

Dr. Bilal Habib

Oct 14 , 2023

Department of Computer Systems Engineering

University of Engineering and Technology, Peshawar


ASSESSMENT RUBRICS COA LABS

LAB REPORT ASSESSMENT


Marks
Criteria Excellent Average Nill
Obtained
1. Objectives of Lab All objectives of Objectives of Objectives of
lab are properly lab are lab are not
covered partially shown
[Marks 10] covered [Marks 0]
[Marks 5]
2. MIPS All the instructions Some The
instructions with
Comments and are well written instructions are instructions are
proper with comments missing are not properly
indentations.
explaining the poorly written
code and properly commented [Marks 0]
indented code
[Marks 20] [Marks 10]
3. Simulation run The code is The code is The code is
without error
and warnings running in the running but written but not
simulator without with some running due to
any error and warnings or errors
warnings errors. [Marks 0]
[Marks 10] [Marks 5]
4. Procedure All the instructions Some steps are steps are totally
are written with missing missing
proper procedure [Marks 10] [Marks 0]
[Marks 20]
5. CONSOLE Proper CONSOLE Some of the No or wrong
of the code written CONSOLEs CONSOLE
in assembly are missing [Marks 0]
[Marks 20] [Marks 10]
6. Conclusion Conclusion about Conclusion Conclusion
the lab is shown about the lab is about the lab is
and written partially shown not
[Marks 20] [Marks 10] shown[Marks0
]
7. Cheating Any kind of
cheating will
lead to 0 Marks
Total Marks Obtained:__________
Instructor Signature: ______________________
QUESTION NO 1
Enter a number 5432 from user and then display the last digit in the console. (hint: use mfhi ).

CODE:

.text

.globl main

main:

# Prompt for the first number

li $v0, 4 # syscall: print_str

la $a0, msg1 # load address of the message

syscall

# Read the first number

li $v0, 5 # syscall: read_int

syscall

move $t0, $v0 # store the first number in $t0

li $t1, 10

# divide number

div $t0, $t1

mfhi $t2

# Display the result


li $v0, 1 # syscall: print_int

move $a0, $t2 # load the result into $a0

syscall

# Exit the program

li $v0, 10 # syscall: exit

syscall

.data

msg1: .asciiz "Enter number: "

CONSOLE:

CONCLUSION:
In conclusion, the provided code is a MIPS assembly program that prompts the user to enter a number,
reads the input, extracts the last digit from it, and then displays that last digit on the console. The
program accomplishes this by dividing the input number by 10 and using the "mfhi" instruction to obtain
the remainder, which is the last digit. The program is well-structured and achieves its intended
functionality effectively. It demonstrates the use of various MIPS assembly instructions, such as "li," "la,"
"move," "div," and "syscall," to handle input and output.

QUESTION NO 2
Check whether a number input by user is negative or equal to zero or greater then zero using branching
(Use bgt or ble).

CODE:

.text

.globl main
main:

li $v0, 4

la $a0, msg1

syscall

li $v0, 5

syscall

move $t0, $v0

li $t1, 0

beq $t0,$t1, equal

bgt $t0,$t1, greater

ble $t0,$t1, less

equal:

li $v0, 4

la $a0, msg2

syscall

j end

greater:

li $v0, 4

la $a0, msg3

syscall
j end

less:

li $v0, 4

la $a0, msg4

syscall

end: # Exit the program

li $v0, 10 # syscall: exit

syscall

.data

msg1: .asciiz "Enter any Number: "

msg2: .asciiz "Number equal to 0"

msg3: .asciiz "Number greater than 0"

msg4: .asciiz "Number less than 0"

CONSOLE:

CONCLUSION:

The code is well-structured and effectively determines whether the input number is equal to zero,
greater than zero, or less than zero. It makes use of the "beq," "bgt," and "ble" branching instructions to
control program flow based on the user's input. The appropriate message is displayed to the user
accordingly. The use of labels and branching instructions demonstrates the logic of conditional
statements in MIPS assembly language. This code is a good example of basic control flow and
conditional branching in assembly language programming.

QUESTION NO 3
Check using branch whether the number input by user are equal or not (Use beq).

CODE:

.text

.globl main

main:

li $v0, 4

la $a0, msg1

syscall

li $v0, 5

syscall

move $t0, $v0

li $v0, 4

la $a0, msg2

syscall

li $v0, 5

syscall

move $t1, $v0


beq $t0,$t1, equal

bne $t0,$t1, nequal

equal:

li $v0, 4

la $a0, msg3

syscall

j end

nequal:

li $v0, 4

la $a0, msg4

syscall

end: # Exit the program

li $v0, 10 # syscall: exit

syscall

.data

msg1: .asciiz "Enter First Number: "

msg2: .asciiz "Enter Second Number: "

msg3: .asciiz "Both Number are equal"

msg4: .asciiz "Both Number are not equal"


CONCLUSION:

The code is well-structured and effectively checks whether the two input numbers are equal or not. It
demonstrates the use of the "beq" and "bne" branching instructions to control program flow based on
the user's input. The code also displays the appropriate message to the user, indicating the equality or
inequality of the input numbers. This example showcases how branching instructions can be used to
implement conditional statements in MIPS assembly language. It serves as a good illustration of basic
conditional branching in assembly language programming.

CONSOLE:

QUESTION NO 4
Write the assembly of the below C++ code.

Int age;

Cout<<"enter your age"<<endl;

Cin>>age;

If(age > 18)

Cout<<"you can apply for CNIC"<<endl;

Else

Cout<<"you cannot apply for CNIC"<<endl;


}

CODE:

.text

.globl main

main:

li $v0, 4

la $a0, msg1

syscall

li $v0, 5

syscall

move $t0, $v0

li $t1, 18

bge $t0,$t1, egreater

blt $t0,$t1, less

egreater:

li $v0, 4

la $a0, msg2

syscall

j end
less:

li $v0, 4

la $a0, msg3

syscall

end: # Exit the program

li $v0, 10 # syscall: exit

syscall

.data

msg1: .asciiz "Enter Your Age: "

msg2: .asciiz "you can apply for CNIC"

msg3: .asciiz "you cannot apply for CNIC"

CONSOLE:

CONCLUSION:

In conclusion, the provided MIPS assembly code replicates the functionality of a simple C++ program
that assesses a user's age for CNIC eligibility. The code takes an age input, compares it to 18, and based
on the result, displays a corresponding message. It effectively employs conditional branching
instructions, such as "bge" and "blt," to control program flow and uses system calls for input and output
operations. The program provides a clear example of how to handle user input, perform conditional
checks, and generate appropriate output messages in the context of assembly language programming.

QUESTION NO 5
Write a program which take a limit from user and compute the sum of numbers from 0 to the limit (Use
bqe, add, addi, and J (jump)).

Below is the C++ language code:

Int limit;

Int sum;

Cout<<"Enter a number"<<endl;

Cin>>limit;

for (int i = 1; i <= limit; ++i) {

sum += i;

Cout<<"sum of numbers from 1 to <<limit<<"is"<<sum<<endl;

CODE:

.text

.globl main

main:

# Print the message to enter a limit

li $v0, 4
la $a0, msg1

syscall

# Read an integer input

li $v0, 5

syscall

move $t0, $v0

# Initialize loop control variables

li $t1, 0 # Start the loop from 0

li $t2, 0

adding:

# Check if t1 (current loop counter) is greater than t0 (user input)

bgt $t1, $t0, display

# Add t1 to t2 (accumulate sum)

add $t2, $t2, $t1

# Increment t1 (loop counter)

addi $t1, $t1, 1

# Jump back to the "adding" label

j adding
display:

# Print the message for displaying the sum

li $v0, 4

la $a0, msg2

syscall

# Print the sum (t2)

li $v0, 1

move $a0, $t2

syscall

end:

# Exit the program

li $v0, 10

syscall

.data

msg1: .asciiz "Enter the limit to calculate the sum from 0 to that number : "

msg2: .asciiz "\nThe Sum is : "

CONSOLE:

CONCLUSION:
In conclusion, the provided MIPS assembly code mirrors the behavior of a C++ program that calculates
the sum of numbers from 1 to a user-specified limit. The code prompts the user to input a limit, then
iterates from 1 to that limit, accumulating the sum of these numbers. It effectively uses loop control
variables, conditional branching with "bgt," addition instructions with "add" and "addi," and output
operations with system calls to display the final sum. This assembly code demonstrates the practical
application of loops and arithmetic operations in the context of assembly language programming and
effectively provides the desired functionality.

You might also like