Need help understanding this practice problem in my reading. Can someone please explain to me what it is doing at every step.

Consider the following assembly code:

x at %ebp+8, n at %ebp+12

1   movl    8(%ebp), %esi   //I assume this is x being stored in register
2   movl    12(%ebp), %ebx  //Also I think this is n being stored in register
3   movl    $-1, %edi
4   movl    $1, $edx
5   .L2:
6   movl    %edx, %eax
7   andl    %esi, %eax
8   xorl    %eax, %edi
9   movl    %ebx, %ecx
10  sall    %cl, %edx
11  test1   %edx, %edx
12  jne     .L2
13  movl    %edi, %eax

The preceding code was generated by compiling C code that had the following overall form:

int loop(int x, int n)
    int result = ________;
    int mask;

    for(mask = ________; mask _________; mask = _________)
        result ^= _________;

    return result;

It's fill in the blanks (obviously). Thanks

Re: Need help understanding this assembly code 80 80

Please provide the full C code, but to start.

  1. Line 1 - push x and y onto stack
  2. Line 3 - move x into the esi register
  3. Line 4 - move y into the ebx register
  4. Lines 5 and 6 are the initialization of the result and mask variables. %edx is mask and %edi is result.
  5. Line 7 is the loop invariant.

You should be able to figure out the rest - please don't ask us to solve your homework problems for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.