In: Computer Science
Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS) which works as a simple calculator. The program will get two integer numbers, and based on the requested operation, the result should be shown to the user. a. The program should print a meaningful phrase for each input, and the result. i. “Enter the first number” ii. “Enter the second number” iii. “Enter the operation type” iv. “The result is” b. The user should enter 0, 1, and 2 to tell the program the types of operation add, sub, and multiply, respectively. c. How many registers do you need to implement this program? d. What system calls do you need to write this program?
Please up vote ,comment if any query . Thanks for Question . Be safe .
Note : check attached image for output ,code compiled and tested in MARS MIPS simulator .
Also attached image for syscall we need and register we used inside program.
Program Plan :
Program :
.data
firstNumber : .asciiz "Enter the first number: "
secondNumber: .asciiz "Enter the second number:
"
operationType: .asciiz "Enter the operation
type(0=add,1=sub,2=multiply ): "
result : .asciiz "the result is :"
.text
main:
la $a0,firstNumber #$a0=address of
first number
li $v0,4 #syscall 4 to print
string
syscall
#read user input integer using
syscall 5
li $v0,5 #user input stored in
$v0
syscall
addi $t0,$v0,0 #move first number
in $t0=$v0+0
la $a0,secondNumber #$a0=address of
second number
li $v0,4 #syscall 4 to print
string
syscall
#read user input integer using
syscall 5
li $v0,5 #user input stored in
$v0
syscall
addi $t1,$v0,0 #move second number
in $t1=$v0+0
la $a0,operationType #$a0=address
of operation type
li $v0,4 #syscall 4
syscall
#read user input in $v0 using
syscall 5
#0 -> add
#1 -> subtract
#2 -> multiply
li $v0,5 #syscall 5
syscall
beq $v0,0,addition #if operation
type $v0==0 go to addition label
beq $v0,1,subtract #if operation
type $v0==1 go to subtract label
beq $v0,2,multiply #if operation
type $v0==2 go to multiply label
j exit #if invalid value jump to
exit label(operation type not 0,1 and 2)
addition: #addition label
add $t2,$t0,$t1 #t2=$t0+$t1 ,result=firstnumber +
secondnumber
j print #jump to print label
subtract: #subtract label
sub $t2,$t0,$t1 #t2=$t0-$t1 ,result=firstnumber -
secondnumber
j print #jump to print label
multiply: #multiply label
mul $t2,$t0,$t1 #t2=$t0*$t1 ,result=firstnumber *
secondnumber
j print #jump to print label
print: #print label
la $a0,result #$a0=address of result
li $v0,4 #syscall 4
syscall
addi $a0,$t2,0 #move result in $a0 =$t2
li $v0,1 #syscall 1
syscall
exit:#exit label
li $v0,10 #syscall 10 to terminate program
syscall
Output :
Register and syscall used in program :
Please up vote ,comment if any query .