3. (15 points) This C segment while (A[i] != k) uses both a conditional branch a
ID: 2248009 • Letter: 3
Question
3. (15 points) This C segment while (A[i] != 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 St1-74 # Temporary register St-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:-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.
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.