CS计算机代考程序代写 mips assembly Quiz4

Quiz4
Due No due date Points 10
Questions 1 Available until Feb 3 at 4:20pm Time Limit 60 Minutes
Instructions
You can take this quiz only once.
Once you start the quiz, you will need to complete it.
This quiz was locked Feb 3 at 4:20pm.
Attempt History
Attempt Time Score
LATEST Attempt 1 9 minutes 10 out of 10
Score for this quiz: 10 out of 10 Submitted Feb 3 at 3:57pm This attempt took 9 minutes.
Question 1
Not yet graded / 10 pts
Write MIPS assembly code for the following C code.
int sub1(int a, int b) {

int ans1;
ans1 = a – b;
return ans1; }
int sub2(int c, int d, int e) {
int ans2;
ans2 = sub1(c, d) – e;
return ans2; }
void main() {
int f = 1, g = 2, h = 3, ans3; ans3 = sub2(f, g, h); return;

}
register
variable
$a0
f
$a1
g
$a2
h
$a0
c
$a1
d
$a2
e
$a0
a
$a1
b
$s0
ans1
$s1
ans2
$s2
ans3
$v0
Returned value of sub1 procedure

$v0
Your Answer: .data
f: .word 1
g: .word 2
h: .word 3 ans1: .space 4 ans2: .space 4 ans3: .space 4
.globl main .text
main:
lw $a0, f
lw $a1, g
lw $a2, h
jal sub2
sw $s2, ans3 li $v0, 10
Returned value of sub2 procedure

syscall
sub2:
move $t0, $ra
jal sub1
sub $s1, $s0, $a2 sw $s1, ans2 move $s2, $s1 move $ra, $t0
jr $ra
sub1:
sub $s0, $a0, $a1 sw $s0, ans1
jr $ra
sub1:
sub $s0, $a0, $a1 #ans1 = $s0 = a-b
move be returned
$v0, $s0 #copy the value $s0 to $v0 to
jr $ra #return to the calling procedure (sub2)
sub2:
addi $sp $sp -4 #move the stack pointer down

sw $ra, 0($sp) #save the return address for sub2 procedure
jal sub1 #call sub1 procedure
sub
move be returned
lw $ra, 0($sp) sub2 back
jr (main)
$ra
addi $sp,
$sp, 4
$s1, $v0, $a2 #ans2 = $s1 = sub1() – e
$v0, $s1
#copy the value $s1 to $v0 to
#copy the return address for
#move the stack pointer up #return to the calling procedure
#f is initialized to 1 #g is initialized to 2 #h is initialized to 3
main:
li $a0, 1
li $a1, 2 li $a2, 3
addi
sw main
jal sub2 #call sub2 procedure
#copy the return address for
#move the stack pointer up #copy the returned value to
lw main back
$ra, 0($sp) addi $sp, $sp, 4
move $s2 = ans3
$s2, $v0
jr $ra #return
$sp, -4 #move the stack pointer down
$sp,
$ra, 0($sp) #save the return address for
addi $sp, $sp, 4 #move the stack pointer down

Quiz Score: 10 out of 10 This quiz score has been manually adjusted by +10.0 points.