0% found this document useful (0 votes)
52 views17 pages

COA Lab Task 3

COA Lab Task 3 for Computer Engineering Students

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)
52 views17 pages

COA Lab Task 3

COA Lab Task 3 for Computer Engineering Students

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/ 17

BRANCHING OPERATIONS

Fall 2023

COA LAB

Submitted by: NOOR UL HAQ

Registration no: 21pwcse2046

Section: A

Student Signature: ______________

Submitted to:

Dr. Bilal Habib

Oct 20, 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: ______________________
TASK 1
CODE
.text

.globl main

main:

li $v0, 4

la $a0, msg1

syscall

li $v0, 5 # takes first input

syscall

move $t0, $v0

li $v0, 4

la $a0, msg2

syscall

li $v0, 5 # takes operator

syscall

move $t1, $v0

li $v0, 4

la $a0, msg3

syscall
li $v0, 5 # takes second input

syscall

move $t2, $v0

li $t3, 1

li $t4, 2

li $t5, 3

li $t6, 4

beq $t3,$t1, adding

beq $t4,$t1, subtracting

beq $t5,$t1, multiplying

beq $t6,$t1, dividing

adding:

add $t7, $t0, $t2

li $v0, 1

move $a0, $t7

syscall

j end

subtracting:

sub $t7, $t0, $t2

li $v0, 1
move $a0, $t7

syscall

j end

multiplying:

mul $t7, $t0, $t2

li $v0, 1

move $a0, $t7

syscall

j end

dividing:

div $t7, $t0, $t2

li $v0, 1

move $a0, $t7

syscall

j end

end:

li $v0, 10

syscall

.data
msg1: .asciiz "Enter First Number: "

msg2: .asciiz "Press 1 To ADD. 2 To Subtract. 3 To Multiply. 4 To Divide."

msg3: .asciiz "Enter Second Number: "

CONSOLE:

CONCLUSION:
In summary, this MIPS assembly code implements a basic calculator program that provides users with
the capability to perform four fundamental arithmetic operations: addition, subtraction, multiplication,
and division. The program begins by prompting the user to enter two numbers and then a numeric code
representing the desired operation. Depending on the operator code entered, the program
appropriately calculates and displays the result.

This code demonstrates key concepts in assembly programming, including input/output operations,
conditional branching based on user input, and arithmetic instructions for performing calculations. It
serves as a practical example for those learning assembly language, showcasing how to create
interactive programs that execute mathematical operations based on user choices.

TASK 2
CODE:
.text

.globl main

main:

li $v0, 4

la $a0, msg1

syscall
li $v0, 5 # takes number input

syscall

move $t0, $v0

li $t1, 0x8 # t1=1000 in binary

and $t2,$t1,$t0

li $v0, 1

move $a0, $t2

syscall

end:

li $v0, 10

syscall

.data

msg1: .asciiz "Enter Number: "

CONSOLE:

CONCLUSION:
In summary, the MIPS assembly code serves as a straightforward program that engages the user to input
a number. It subsequently performs a bitwise AND operation with a fixed mask value (0x8) and displays
the result. This code demonstrates the use of bitwise operations and user input/output in the MIPS
assembly language. It can be valuable for those looking to grasp basic concepts of bitwise manipulation
in assembly programming.

TASK 3:
CODE:
.data

msg: .asciiz "Enter a number: "

.text

.globl main

main:

# Display prompt for user input

li $v0, 4

la $a0, msg

syscall

# Read an integer from the user

li $v0, 5

syscall

move $t0, $v0 # Store the user's input in $t0

# Check the 4th bit (0-based index) using a bitwise AND operation

li $t1, 1 # $t1 = 0001 in binary


sll $t1, $t1, 3 # Shift $t1 left by 3 positions to get 1000 (binary) for the 4th bit

and $t2, $t0, $t1 # Perform a bitwise AND between user input and $t1

# Toggle the 4th bit using a bitwise XOR operation

xor $t3, $t0, $t1

beqz $t2, bit_is_0 # If the 4th bit was 0, print "The 4th bit is 0."

beq $t2, $t1, bit_is_1 # If the 4th bit was 1, print "The 4th bit is 1."

bit_is_0:

li $v0, 4

la $a0, bit_0_msg

syscall

j exit

bit_is_1:

li $v0, 4

la $a0, bit_1_msg

syscall

j exit

exit:

li $v0, 10

syscall
.data

bit_0_msg: .asciiz "The 4th bit is 0\n"

bit_1_msg: .asciiz "The 4th bit is 1\n"

CONSOLE:

CONCLUSION:
In summary, this MIPS assembly code is a concise program that interacts with the user to input a
number and subsequently examines the 4th bit (counting from the right, 0-based) of the entered
number. It does so by performing bitwise AND and XOR operations to isolate and toggle the 4th bit.
Depending on the outcome, the code then displays the corresponding message indicating whether the
4th bit is set to 0 or 1.

This code effectively illustrates the use of bitwise operations for bit manipulation and provides an
insightful example of conditional branching based on the result of these operations in the MIPS
assembly language. It can be valuable for those seeking to understand the fundamentals of bitwise
operations and conditional programming in assembly.

TASK 4:
CODE:
.data

msg: .asciiz "Enter a number: "

even_msg: .asciiz "The number is even.\n"

odd_msg: .asciiz "The number is odd.\n"

.text

.globl main
main:

# Display prompt for user input

li $v0, 4

la $a0, msg

syscall

# Read an integer from the user

li $v0, 5

syscall

move $t0, $v0 # Store the user's input in $t0

# Check if the number is even or odd

andi $t1, $t0, 1 # Use a bitwise AND with 1 to check the lowest bit

beqz $t1, is_even # If the result is 0, the number is even

is_odd:

# Print the "odd" message

li $v0, 4

la $a0, odd_msg

syscall

j exit

is_even:
# Print the "even" message

li $v0, 4

la $a0, even_msg

syscall

exit:

li $v0, 10

syscall

CONSOLE:

CONCLUSION:
In summary, this MIPS assembly code is a straightforward program that interacts with the user to input a
number. It efficiently determines whether the number is even or odd by performing a bitwise AND
operation with 1, which checks the lowest bit (0 for even, 1 for odd). Depending on the result, the code
displays the appropriate message indicating whether the number is even or odd.

This code effectively demonstrates the use of bitwise operations for parity checking and conditional
branching based on the result. It serves as a practical example of conditional programming and bitwise
manipulation in the MIPS assembly language, making it a valuable resource for those learning these
essential concepts.

TASK 5:
Code:
.data

msg: .asciiz "Enter an even number: "

result_mul_msg: .asciiz "Shifting left by 1 position is equivalent to multiplication by 2.\n"


result_div_msg: .asciiz "Shifting right by 1 position is equivalent to division by 2.\n"

.text

.globl main

main:

# Display prompt for an even number

li $v0, 4

la $a0, msg

syscall

# Read an integer from the user

li $v0, 5

syscall

move $t0, $v0 # Store the user's input in $t0

# Check if the number is even

andi $t1, $t0, 1 # Use a bitwise AND with 1 to check the lowest bit

beqz $t1, even_number # If the result is 0, the number is even

odd_number:

# Print an error message and exit

li $v0, 4

la $a0, odd_msg
syscall

j exit

even_number:

# Print a message and demonstrate multiplication by 2 and division by 2

li $v0, 4

la $a0, result_mul_msg

syscall

sll $t2, $t0, 1 # Shift left by 1 position (multiply by 2)

li $v0, 1

move $a0, $t2

syscall

la $a0, newline

li $v0, 4

syscall

srl $t2, $t0, 1 # Shift right by 1 position (divide by 2)

li $v0, 1

move $a0, $t2

syscall

la $a0, newline

li $v0, 4
syscall

li $v0, 4

la $a0, result_div_msg

syscall

exit:

li $v0, 10

syscall

.data

odd_msg: .asciiz "Error: Please enter an even number.\n"

newline: .asciiz "\n"

CONSOLE:

CONCLUSION:
In summary, this MIPS assembly code serves as an instructive program that engages the user to input an
even number. It verifies the evenness of the input through bitwise manipulation by checking the lowest
bit (0 for even) and appropriately responds with an error message if the number is not even.

For an even number, the code provides a valuable demonstration of the equivalence between bitwise
left shifts (multiplication by 2) and bitwise right shifts (division by 2). It performs these operations on the
input number and displays the results to the user. This code effectively showcases bitwise operations,
conditional branching, and arithmetic concepts in the MIPS assembly language, making it a helpful
educational example for those learning assembly programming.

You might also like