I am writing this program in MIPS to calculate 2 to a power given by me and sum the sequential powers down to 0. For example, if I put 4 in $a0 like in the code below, I want it to calculate (4^2)+(3^2)+(2^2)+(1^2) which should come out to be 15. It should stop before it reaches zero. This is what I have written so far
main:
addi $a0, $zero, 4 #put k in $a0 in this case
addi $a1, $zero, 0 #put 0 in current sum
addi $v1, $v1, 0 #tally the total in $v1
for:
lw $10, $a0 #load k into reg $10
lw $11, $a1 #load sum into $11
addi $10, $10, -1 #subtracts 1 from k
li $9, 0 #sets i in for loop ($9) to 0
done:
li $v0, 10
syscall
I'm new to MIPS and could use a lot of help on finishing this, I know I want to use a for loop, but I don't know how to go through it while subtracting 1 from k and also calculating the sum. How would I bring 2 to a power of k, because I guess there is no power operation in mips. At this point in the course I can only use add, sub, and, or, slt, addi, j, beq, lw, sw, and sll. Can you not use a constant when using sub? Thank you for any help