99.0k views
1 vote
Write a MIPSzy subroutine, power, that accepts 1argument n and computes 2n. You can assume that nis larger than 0.The main program passes the parameterand receives the return value through the program stack.

1 Answer

6 votes

Answer:

mem : .word 3 2 3 5 2 1 0

.text

addi $t0, $zero, 5100

top:

lw $t1, 0($t0)

beq $t1, $zero, done

addi $sp, $sp, -4

sw $t1, 0($sp)

addi $t2, $zero,4

addi $sp, $sp, -4

sw $t2, 0($sp)

addi $sp, $sp, -4

jal power

lw $t3, 0($sp)

sw $t3, 0($t0)

addi $sp, $sp,12

li $v0,4

la $a0,prompt

syscall

li $v0,1

move $a0,$t3

syscall

j done

power:

li $t7,1 #store temp value

lw $s0,8($sp) #get value of x

lw $s1,4($sp) #get value of y

loop: #load until

mul $t7,$t7,$s0 #multiply by s0

sub $s1,$s1,1 #subract s1 by one

bgtz $s1,loop #loop if s1 greater than zero

sw $t7,0($sp) #save word to sp again

jr $ra #jump to register

done:

Step-by-step explanation:

User Panchu
by
4.2k points