Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please help!! This (now familiar) MIPS/SPIM program includes a subroutine called

ID: 3801876 • Letter: P

Question

please help!!

This (now familiar) MIPS/SPIM program includes a subroutine called myadd that performs x=(y+z);. In the space below, replace the myadd subroutine with one named ispow2 that will make x=ispow2(y);, which will return 1 if y is a power of 2 and 0 otherwise. The algorithm for this is on the MAGIC algorithms page; basically, here you are implementing (&(x-1))==0. You should test your routine using SPIM before you submit it. #### # # Addition routine: # x = y + z # .text .globl my add myadd: la $t0, y #t0 = y lw $t0, 0($t0) la $t1, z #t1 = z lw $t1, 0($t1) addu $t2, $t0, $t1 #t2 = y + z la $t0, x #x = 12 sw $t2, 0($t0) jr $ra #return

Explanation / Answer

####
#
# My Is Power-of 2 routine:
#
# x = ispow2(y)
#

.text
.globl ispow2

ispow2:
   li   $t0, 1       # t0 = 1
   la   $t1, y       # t1 = y
   lw   $t1, 0($t1)
   sub   $t2, $t1, $t0   # t2 = t1 - 1
   and   $t3, $t1, $t2   # t3 = t1 & t2
   beq   $t3, $0, fi
   add   $t0, $0, $0
fi:   la   $t1, x
   sw   $t0, 0($t1)   # x = t0
   jr   $ra       # return