assembly code

I got this problem and  I need some help.  tips or comments are welcome.

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

1     movl     8(%ebp),  %esi
2     movl     12(%ebp),  %ebx
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      testl            %edx, %edx
12      jne              .L2
13      movl            %edi , %eax

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

1 int loop (int x , int n)
2 {
3        int result = ..................... ;
4        int mask;
5        for ( mask = .......... ; mask...............;mask=............)  {
6               result ^= ...............;
7        }
8        return result;
9        }

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?
lecosAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
The idea is to try to match each assembly instruction to its counterpart in the C code.

Start with (a), and figure out which registers are used for the 4 variables. Have a look at the first 4 lines for that :

>> 1     movl     8(%ebp),  %esi
>> 2     movl     12(%ebp),  %ebx
>> 3      movl      $-1,  %edi
>> 4       movl      $1,  %edx

Can you see what is what ?
0
lecosAuthor Commented:

** The idea is to try to match each assembly instruction to its counterpart in the C code.

Start with (a), and figure out which registers are used for the 4 variables. Have a look at the first 4 lines for that :

>> 1     movl     8(%ebp),  %esi                          // x is located in register %esi //
>> 2     movl     12(%ebp),  %ebx                  // n is located in register %ebx //
>> 3      movl      $-1,  %edi                              // im not sure what this means but im guessing that will hold the values for
>> 4       movl      $1,  %edx                                 " result" and "mask "//   not sure how the $1 or $-1 works here.

Can you see what is what ?  **





0
Infinity08Commented:
$1 and $-1 are literal integer values (resp. 1 and -1). The %edi register is initialized with the value -1, and %edx with 1.
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

lecosAuthor Commented:
ok, so  i think it would be safe to say that "result" is stored in %edx and "mask" in %edi? so if this is true i can answer the second question as well,  the intial values for mask  = -1 and result = 1. let me know if this is correct.


I'm not sure what the test condition for mask is. there's a testl operand but uses only %edx
0
Infinity08Commented:
>> ok, so  i think it would be safe to say that "result" is stored in %edx and "mask" in %edi?

Have a closer look. 'mask' is used to control the loop, so check in the assembler code which register is used to control the loop. 'result' is XOR'd with another value, so check in the assembler code where that happens. That should give you a push in the right direction :)


>> there's a testl operand but uses only %edx

The test instruction is basically a bitwise AND of the two operands, which sets the zero flag in case the result is 0. The jne instruction after that checks whether the zero flag is set or not (and jumps accordingly).
Note that the bitwise AND with the same value for both operands results in that same value again. So this is basically a test for 0.
0
lecosAuthor Commented:

Have a closer look. 'mask' is used to control the loop, so check in the assembler code which register is used to control the loop. 'result' is XOR'd with another value, so check in the assembler code where that happens. That should give you a push in the right direction :)

>>> "result" stored in %edi and "mask" stored in %edx. also,line 13 shows that the value returned is located at %edi which should be result. I looked at the code but i dont really understand how "mask" is being used to control the loop. please clarify.Dont really understand how the mask or result gets updated.



The test instruction is basically a bitwise AND of the two operands, which sets the zero flag in case the result is 0. The jne instruction after that checks whether the zero flag is set or not (and jumps accordingly).
Note that the bitwise AND with the same value for both operands results in that same value again. So this is basically a test for 0.

>>> JnE (jnz), i suppose that i will jump if the test gives 1 . i will go to return when i get a 0 .
0
Infinity08Commented:
>> I looked at the code but i dont really understand how "mask" is being used to control the loop.

In the C code, you have this :

>> 5        for ( mask = .......... ; mask...............;mask=............)  {

which means that 'mask' controls the duration of the loop. It's initialized, incremented, and there's a loop end condition on it.

If you know that %edx is used for 'mask', can you match each of these three with the corresponding assembler instructions ?


>> >>> JnE (jnz), i suppose that i will jump if the test gives 1 . i will go to return when i get a 0 .

The jump will be made whenever it has any value other than 0. As soon as the value becomes 0, the jump will not be made.
0
lecosAuthor Commented:
ok , this is what i have. I'm not sure how to translate this into a C code. please help me with that.


6          movl          %edx , % eax          //  value 1 stored in %eax //
7        andl             %esi , %eax            //  x & 1 //
8         xorl            %eax, %edi            // result =  (x & 1) ^ -1
9         movl           %ebx , % ecx
10      sall              %cl , %edx          //      mask = shift arith left  % cl         //
11      testl            %edx, %edx
12      jne              .L2


this is what i got,

1 int loop (int x , int n)
2 {
3        int result = ......-1............... ;
4        int mask;
5        for ( mask = .....1..... ; mask......x & 1.........;mask=....0........)  {
6               result ^= ......(x & 1 ).........;
7        }
8        return result;
9        }
0
Infinity08Commented:
>> 6          movl          %edx , % eax          //  value 1 stored in %eax //

This is only true for the first iteration. For subsequent iterations, a different value will be copied into eax.

>> 7        andl             %esi , %eax            //  x & 1 //

Same here. It's not always 1.

>> 8         xorl            %eax, %edi            // result =  (x & 1) ^ -1

Same here. Not always 1, and not always -1.

>> 5        for ( mask = .....1..... ; mask......x & 1.........;mask=....0........)  {

A for loop has three parts (in this order) :

(a) initialization. This happens once at the start of the loop.
(b) loop continue condition (as long as this condition is true, another iteration is done). This is checked at the beginning of each iteration. Note that in assembly code, this could be moved elsewhere, as long as the semantic result is the same.
(c) increment (or more generally, modification). Done at the end of each iteration.
0
lecosAuthor Commented:
>> 6          movl          %edx , % eax          //  value 1 stored in %eax //

This is only true for the first iteration. For subsequent iterations, a different value will be copied into eax.




>> 7        andl             %esi , %eax            //  x & 1 //

Same here. It's not always 1.

  @@ so i guess you can say : mask = x & mask


>> 8         xorl            %eax, %edi            // result =  (x & 1) ^ -1

Same here. Not always 1, and not always -1.

@@   result = result ^ (x & mask)


>> 5        for ( mask = .....1..... ; mask.<<8 != 0 .........;mask=...x ^ mask........)  {


please explain how the sall function works here, i think is being used to test the value of mask , whether is a 0 or 1.

also the result = result ^ (x & mask)

thanks for the quick reply , i appreciate your help.

0
Infinity08Commented:
>>   @@ so i guess you can say : mask = x & mask

What is eax used for ? Does it correspond to 'mask' ?


>> @@   result = result ^ (x & mask)

Looks good.


>> please explain how the sall function works here, i think is being used to test the value of mask , whether is a 0 or 1.

The sall instruction is an arithmetic shift left operation. The second operand is shifted left over an amount of bits indicated by the first operand.
It is not used for testing ... there's another instruction for that.
0
lecosAuthor Commented:
ok, i think i got it.


for ( mask = 1 ; mask= mask != 0 .;mask= mask << ( n  % 256)

result = result ^ (x & mask)

Let me know if this is correct. Infinity could you help me with the question I posted on stack frames. I've been reading but i would appreciate if you can make some comments on the code,  where to look in the code for the answer. thank you.
0
Infinity08Commented:
>> mask= mask != 0

make that just :

        mask != 0

and then it's fine :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Infinity08Commented:
>> Infinity could you help me with the question I posted on stack frames.

I'm monitoring it, but I see Superdave is helping you with that. If there's no progress, I'll have a go myself :)
0
lecosAuthor Commented:
>> mask= mask != 0

make that just :

        mask != 0

and then it's fine :)

yeah , it was a typo.  thanks for the help.  









0
ZanokaCommented:
What is %cl  doing here? I guess it has a number on %cl so that it makes %edx shift left by %cl. then, Where does %cl register come from?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Assembly

From novice to tech pro — start learning today.