This c segment While (A[i]= k) i = i + j uses both a conditional branch and an u
ID: 3884978 • Letter: T
Question
This c segment
While (A[i]= k) i = i + j uses both a conditional branch and an unconditional jump each time through the loop (see below). Variables j j, and k correspond to registers Ss3, Ss4, and Ss5, and the base (start address) of the integer array A in $s6. The result of the program is the final value of the index i in Ss3. Code before optimization Loop: add $t1, $s3, $s3 # Temporary register $t1 = j*2 add $t1, $t1, $t1 # Temporary register $t1 = j*4 add $t1, $t1, $s6 # Temporary register $t1 = address of A[i] lw $t0, 0($t1) # Temporary register $t0 = A[i] beq $t0, $s5, Exit # goto Exit if A[i] ! = k add $s3, $s3, $s4 #i = i + j j Loop # goto Loop Exit: a. Rewrite the assembly code so that it executes at most one branch or jump each time through the loop (i.e., no more than one jump/branch executed per iteration).Explanation / Answer
ANSWER
A)
CODE
add $t1,$s3, $s3 # Temp reg $t1 = 2 * i
add $t1,$t1, $t1 # Temp reg $t1 = 4 * i
add $t1,$t1, $s6 # $t1 = address of save[i]
lw $t0,0($t1) # Temp reg $t0 = save[i]
bne $t0,$s5, Exit # go to Exit if save[i] k
Loop: add$s3, $s3, $s4 # i = i + j
add $t1,$s3, $s3 # Temp reg $t1 = 2 * i
add $t1,$t1, $t1 # Temp reg $t1 = 4 * i
add $t1,$t1, $s6 # $t1 = address of save[i]
lw $t0,0($t1) # Temp reg $t0 = save[i]
beq $t0,$s5, Loop # go to Loop if save[i] = k
Exit:
The numberof instructions executed by this new form of the
loop is 5+ 10 x 6 = 65.
THANK YOU
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.