please wirte the mips code
main() {
x = 2*foo(4*y+7);
x = x+(4*y+7);
}
int foo (int n) {
int junk[10] = {0,1,2,3,4,5,6,7,8,9};
junk[4] += 2; if (n<1)
return 1;
else
return (foo(n-1) + junk[4] + n);
}
Greetings!!
.data
x: .word 0
y: .word 0
junk: .word 0,1,2,3,4,5,6,7,8,9
.text
#MAIN STARTS
main:
li $t0,y
sll $a0,$t0,2 #4y
addi $a0,$a0,7 #4y+7
jal foo #FUNCTION CALL
li $v0,10
syscall
#MAIN END
#FUNCTION DEFINITION
foo:
la $t0,junk #address of junk
li $t1,4
li $t2,2
mul $t1,$t1,4 #calculate ofset
add $t0,$t0,$t1 #calclate index
bgt $a0,1,else #if less than 1
sw $t2,0($t0) #store
li $v0,1 #return value 1
jr $ra #return
else:
lw $t3,0($t0) #junk[4]
move $t4,$a0 #n
addi $a0,$a0,-1 #n-1
add $a0,$t3,$t4 #n-1 + junk[4] + n
jal foo #foo()
jr $ra #return to main
Hope this helps
Get Answers For Free
Most questions answered within 1 hours.