Overclock.net banner

MARS MIPS Powers

9.8K views 2 replies 2 participants last post by  Will93  
#1 ·
ive created a simple loop that should calculate "number 1" to the power of "number 2"
for some reason it will only do the loop 3 times despite being told to go round "number 2" times
number 1 and 2 are user input numbers, but are set with a default

addi $s6,$zero, 100 # default Value for num 1
addi $s7,$zero, 200 # default Value for num 2

mul $t1, $s6, $s6
add $t0, $zero, 1

loop3:
addi $t0, $t0, 1 # generating numbers in register $t0
mul $t2, $t1, $s6 # compute the sum
add $t3, $zero, $t2 # move it top reg t3
blt $t0, $s7, loop3 # if t0 less than s7 then go to loop.

addi $v0, $zero, 1
add $a0, $zero, $t3
syscall #Print ans

any help on where this is going wrong would be appreciated
 
#2 ·
I changed your code around a little bit. This handles when the power is a 0 or 1...and everything beyond 1. I did not implement negative powers.

Code:

Code:
 addi $s6,$zero, 100 # default Value for num 1
 addi $s7,$zero, 200 # default Value for num 2

 beq $s7, $zero, L1 #if $s7 and 0 are equal jump to L1

 add $t2, $zero, $s6  #store value of s6 into t2
 addi $t0, $zero, 1  #initialize t0 register to 1

 beq $s7, $t0, L2 #if $s7 and 1 are equal jump to L2

 loop3:

 addi $t0, $t0, 1 # increment value stored in t0 register
 mul $t2, $t2, $s6 # multiply values stored in t2 and s6 register, placed result in t2 register
 blt $t0, $s7, loop3 # if t0 less than s7 then go to loop.

 j L3

 L1: 
 addi $t2, $zero, 1 #store 1 into t2...number to power of 0

 j L3

 L2:
 add $t2, $zero, $s6 #store s6 into t2...number to power of 1

 L3:

 addi $v0, $zero, 1
 add $a0, $zero, $t2
 syscall #Print ans