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

Consider the following assembly code long loop(long x, int n) x in %rdi, n in %e

ID: 3890665 • Letter: C

Question

Consider the following assembly code long loop(long x, int n) x in %rdi, n in %esi 1 loop: movl movl movl %esi, %ecx $1,%edx $0,%eax 4 2 L3 : movq %rdi, %r8 orq sa 10 11 L2: testq %rax , %rax jne rep; ret 13 L3 The preceding code was generated by compiling C code that had the following overall form 1 long loop(long x, long n) long result long mask; for (mask mask ; mask result I return result; Your task is to fill in the missing parts of the C code to get a program equivalent to the generated assembly code. Recall that the result of the function is returned in register %rax. You will find it helpful to examine the assembly code before, during, and after the loop to form a consistent mapping between the registers and the program variables A. Which registers hold program values x, n, result, and mask? B. What are the initial values of result and mask? C. What is the test condition for mask? D. How does mask get updated? E. How does result get updated? F. Fill in all the missing parts of the C code

Explanation / Answer

A. The registers holding the values x, n, result and mask are:

The register % rdi holds x.

The register % ecx holds n.

The register % edx holds result.

The register % eax holds msk.

B. The initial values of result and mask are 0 and 1 respectively. (i.e result = 0 and mask =1).

C. The test condition for mask is:

for (mask=1; mask!=0; mask<<n)

D. The value in the mask is left shifted by the number present in n.

E. The result get updated by performing AND (x, mask).

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