Problem 2: Arithmetic expressions [1.5 points] Write a program to evaluate the f
ID: 3742256 • Letter: P
Question
Problem 2: Arithmetic expressions [1.5 points] Write a program to evaluate the following function in u and v: 3u7uv- v2+1 Here, the variables u and v, are user inputs, and the program should receive them from the user at run-time. Then it should print the outcome computed as per given arithmetic expression. [To understand how to take the user input or write on console, please refer to the sample MIPS example programs). You are required to create and use two subroutines for this p Pay attention to the registers used for passing arguments to subroutine, and also the registers used for returning the output from a subroutine 1) int Square (a): Return a2. 2) int Multiply (a, b): Return a* b.Explanation / Answer
Screenshot
--------------------------------------------------------------
Program
#Data declaration section
.data
#User input
u: .asciiz "Enter the value of u: "
v: .asciiz "Enter the value of v: "
#result print
result: .asciiz "3u^2+7uv-v^2+1= "
#Main method
.text
#User prompt for u value
la $a0,u
li $v0,4
syscall
#Read user input
li $v0,5
syscall
#store in s0 register
move $s0,$v0
#User prompt for v value
la $a0,v
li $v0,4
syscall
#Read v value
li $v0,5
syscall
#store in s1 register
move $s1,$v0
#move u value into a0 register for function call
move $a0,$s0
#call square function to find u^2
jal Square
#Find 3*u^2 and store in s2 register
mul $s2,$v0,3
#Move v value in a1 register for function call
move $a1,$s1
#call function to find u*v
jal Multiply
#find 7*u*v
mul $t0,$v0,7
#find 3*u^2+7*u*v
add $s2,$s2,$t0
#put v value in a0 register for square function call
move $a0,$s1
#Call square function
jal Square
#find 3*u^2+7*u*v-v^2
sub $s2,$s2,$v0
#find 3*u^2+7*u*v-v^2+1
addi $s2,$s2,1
#Result display string
la $a0,result
li $v0,4
syscall
#Display result
move $a0,$s2
li $v0,1
syscall
#End of the program
li $v0,10
syscall
#Square function
Square:
mul $v0,$a0,$a0
jr $ra
#multiplication of uv
Multiply:
mul $v0,$a0,$a1
jr $ra
------------------------------------------
Output
Enter the value of u: 2
Enter the value of v: 3
3u^2+7uv-v^2+1= 46
-- program is finished running --
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.