In: Computer Science
need a code MIPS assembly language program to implement
algorithms of an 8-bit integer "positive integer" to calculate
the
square root for an-8 bit integer
using The Radix-2 SRT-Redundant and Non-Redundant Algorithm to approximate square root
Here is the resulting MIPS assembly code:
isqrt:
  # v0 - return / root
  # t0 - bit
  # t1 - num
  # t2,t3 - temps
  move  $v0, $zero        # initalize return
  move  $t1, $a0          # move a0 to t1
  addi  $t0, $zero, 1
  sll   $t0, $t0, 30      # shift to second-to-top bit
isqrt_bit:
  slt   $t2, $t1, $t0     # num < bit
  beq   $t2, $zero, isqrt_loop
  srl   $t0, $t0, 2       # bit >> 2
  j     isqrt_bit
isqrt_loop:
  beq   $t0, $zero, isqrt_return
  add   $t3, $v0, $t0     # t3 = return + bit
  slt   $t2, $t1, $t3
  beq   $t2, $zero, isqrt_else
  srl   $v0, $v0, 1       # return >> 1
  j     isqrt_loop_end
isqrt_else:
  sub   $t1, $t1, $t3     # num -= return + bit
  srl   $v0, $v0, 1       # return >> 1
  add   $v0, $v0, $t0     # return + bit
isqrt_loop_end:
  srl   $t0, $t0, 2       # bit >> 2
  j     isqrt_loop
isqrt_return:
  jr  $ra
You call it like any other MIPS procedure:
addi  $a0, $zero, 15
jal   isqrt # v0 = result
This procedure always returns $v0 =
floor(sqrt($a0)) for positive arguments.
Beware: the code enters an infinite loop for negative arguments. Sanitize your input before calling this procedure.