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

3. (15 points) This C segment while (Ai]!-k) uses both a conditional branch and

ID: 3749461 • Letter: 3

Question

3. (15 points) This C segment while (Ai]!-k) uses both a conditional branch and an unconditional jump each time through the loop (see below). Variables i, j, and k correspond to registers Ss3, Ss4, and Ss5, and the base (start address) of the integer array A in Ss6. The result of the program is the final value of the index i in Ss3 Code before optimization Loop: add St1, ss3, Ss3 add St1, St1, St1 add St, St1, Ss6 lw beq St0, Ss5, Exit add s3, Ss3, Ss4 # Temporary register $t1-1*2 # Temporary register Sti 74 # Temporary register St1 address of A[i] # Temporary register $t0 = Ali] # goto Exit if A [i] = k St0, 0(St1) Loop # goto Loop Exit: a. (13 points) 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). b. (2 points) If the number of times when (A i] '-k) is true is 10, what is the number of instructions executed before and after optimization.

Explanation / Answer

Answer :

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote