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

6. You know the following assembly code snippet is from a for loop in C. Determi

ID: 3871424 • Letter: 6

Question

6. You know the following assembly code snippet is from a for loop in C. Determine the
for loop behavior by determining a-what does the loop counter get initialized to? b-what
is the conditional end check of the loop? c-how is the loop counter modified? d-what
happens in the body of the loop? Recall the jg instruction jumps if the result is greater
than 0. Assume there is a variable called data stored at -24(%rbp) that is initialized to a
value before the code snippet. (20 points, 5 points each for a-d described above)

movl $0, -32(%rbp)
movl $16, -28(%rbp)
jmp .L2
.L3:
movq -24(%rbp), %rdx
movl -28(%rbp), %eax
movq %rdx, %rbx
movl %eax, %ecx
sarq %cl, %rbx
movq %rbx, %rax
andl $1, %eax
xorl %eax, -32(%rbp)
addl $1, -28(%rbp)
.L2:
cmpl $47, -28(%rbp)
jle .L

Explanation / Answer

A) Loop counter is stored in -28(%rbp) and is initialized to 16 as shown in an instruction movl $16, -28(%rbp)

B) Conditional check of the loop is an instruction cmpl $47, -28(%rbp)

check whether counter is less than 47 or not

C) loop counter modification is shown in an instruction addl $1, -28(%rbp)

loop counter is incremented by 1

D) Some data is initially stored at -24(%rbp). Then arithmetic right shift is applied on that data and then it is stored in %rax register. Then the anding with $1 is done. And correseponding result is xored with -32(%rbp) is done and result is stored at -32(%rbp) . Then loop counter is incremented and loop gets executed again based on the conditional check.