Coa Lab 2
Coa Lab 2
Fall 2023
COA LAB
Section: A
Submitted to:
Oct 14 , 2023
CODE:
.text
.globl main
main:
syscall
syscall
li $t1, 10
# divide number
mfhi $t2
syscall
syscall
.data
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
li $t1, 0
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
syscall
.data
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
li $v0, 4
la $a0, msg2
syscall
li $v0, 5
syscall
equal:
li $v0, 4
la $a0, msg3
syscall
j end
nequal:
li $v0, 4
la $a0, msg4
syscall
syscall
.data
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;
Cin>>age;
Else
CODE:
.text
.globl main
main:
li $v0, 4
la $a0, msg1
syscall
li $v0, 5
syscall
li $t1, 18
egreater:
li $v0, 4
la $a0, msg2
syscall
j end
less:
li $v0, 4
la $a0, msg3
syscall
syscall
.data
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)).
Int limit;
Int sum;
Cout<<"Enter a number"<<endl;
Cin>>limit;
sum += i;
CODE:
.text
.globl main
main:
li $v0, 4
la $a0, msg1
syscall
li $v0, 5
syscall
li $t2, 0
adding:
j adding
display:
li $v0, 4
la $a0, msg2
syscall
li $v0, 1
syscall
end:
li $v0, 10
syscall
.data
msg1: .asciiz "Enter the limit to calculate the sum from 0 to that number : "
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.