1

I’m having a hard time figuring out how to square $8. I need to do this without using multiply or division.

addi $8, $0, 1 #seed value
addi $9, $0, 0x2010 #starting memory address
addi $12, $0, 16 #so it can loop 16 times
Loop:
addu $8, $8, $8 #$8 = 8 + 8 #i need to square this value
sw $8, ($9) #the value of $8 gets stored into memory
addi $12, $12, ,-1
addi $9, $9, 4 #increments the address by 4
beq $12, $0, out
beq $12, $12, Loop
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Rami
  • 11
  • 1
  • `8` is a power of 2. Left shift it by 3 if you want to be cheeky. If you want your function to work fast in general, O(bits) = O(log N) to calculate `N*N`, instead of O(N) for repeated addition, you can do the usual shift / AND-test / add loop [Multiplication using Logical shifts in Mips assembly](//stackoverflow.com/q/18812319) – Peter Cordes Feb 04 '19 at 04:10
  • _"8 is a power of 2. Left shift it by 3 if you want to be cheeky."_ I'm pretty sure OP's talking about squaring whatever value is in register `$8`, not about squaring the number 8. – Michael Feb 04 '19 at 09:34

0 Answers0