New Posts  All Forums:Forum Nav:

# MARS MIPS Powers

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

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.

syscall #Print ans

any help on where this is going wrong would be appreciated
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:
``````
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:

syscall #Print ans
```
```

Edited by surfbumb - 7/13/12 at 12:12pm
 Black Silence (15 items)
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 8 Samsung S23A700D 120Hz
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder
SteelSeries QcK Mass Altec Lansing FX4021
 Black Silence (15 items)
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 8 Samsung S23A700D 120Hz
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder