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 (A[i] ! ) i-i+j uses both a conditional bran

ID: 2248030 • Letter: 3

Question

3. (15 points) This C segment while (A[i] ! ) i-i+j uses both a conditional branch and an unconditional jump each time through the loop (see below) Variables i and k correspond to registers $s3 SA, and Ss5, and the base start a dress) ofthe integer array A n The result of te r gram a value x , 3 Code before optimization # Temporary register Sti-i*2 Loop: add, Ss3, Ss3 Stl, Stl, Stl St, St, Ss6 # Temporary register $t-P4 # Temporary register St-address of A[i] # Temporary register Sto-Ali] # goto Exit if Ali-k add add beq StO, Ss5, Exit add $s3, $s3, Ss4 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 (Alil! k) is true is 10, what is the number of instructions executed before and after optimization.

Explanation / Answer

Answer:-a) The optimized code for the c program as shown above can be written as-

lw $t1, $s6 #get the array base address in $t0

Loop1: lw $t2, 0($t1) #get the value from array

beq $t2, $s5, Exit1 #branch to label Exit1 if array value is equal to k

add $s3, $s3, $s4 # else set i=i+j

addi $t1, $t1, 1 #get the next address

j Loop1 #goto label Loop1

Exit1:

Answer:-b) Initially 75, now 53 instructions.

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