COA Lab Task 3
COA Lab Task 3
Fall 2023
COA LAB
Section: A
Submitted to:
.globl main
main:
li $v0, 4
la $a0, msg1
syscall
syscall
li $v0, 4
la $a0, msg2
syscall
syscall
li $v0, 4
la $a0, msg3
syscall
li $v0, 5 # takes second input
syscall
li $t3, 1
li $t4, 2
li $t5, 3
li $t6, 4
adding:
li $v0, 1
syscall
j end
subtracting:
li $v0, 1
move $a0, $t7
syscall
j end
multiplying:
li $v0, 1
syscall
j end
dividing:
li $v0, 1
syscall
j end
end:
li $v0, 10
syscall
.data
msg1: .asciiz "Enter First 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
and $t2,$t1,$t0
li $v0, 1
syscall
end:
li $v0, 10
syscall
.data
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
.text
.globl main
main:
li $v0, 4
la $a0, msg
syscall
li $v0, 5
syscall
# Check the 4th bit (0-based index) using a bitwise AND operation
and $t2, $t0, $t1 # Perform a bitwise AND between user input and $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
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
.text
.globl main
main:
li $v0, 4
la $a0, msg
syscall
li $v0, 5
syscall
andi $t1, $t0, 1 # Use a bitwise AND with 1 to check the lowest bit
is_odd:
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
.text
.globl main
main:
li $v0, 4
la $a0, msg
syscall
li $v0, 5
syscall
andi $t1, $t0, 1 # Use a bitwise AND with 1 to check the lowest bit
odd_number:
li $v0, 4
la $a0, odd_msg
syscall
j exit
even_number:
li $v0, 4
la $a0, result_mul_msg
syscall
li $v0, 1
syscall
la $a0, newline
li $v0, 4
syscall
li $v0, 1
syscall
la $a0, newline
li $v0, 4
syscall
li $v0, 4
la $a0, result_div_msg
syscall
exit:
li $v0, 10
syscall
.data
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.