More Assembly Line Programming

I could use a hint towards what is the accurate interpretation of the following assembly code to C.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//x at %ebp+8, n at %ebp+12

  movl      8(%ebp), %esi
  movl      12(%ebp), %ebx
  movl      $-1, %edi
  movl      $1, %edx
L2:
  movl      %edx, %eax
  andl      %esi, %eax	//andl combines two variables to put into memory e.g. z = x&y	
  xorl      %eax, %edi
  movl      %ebx, %ecx
  sall      %cl, %edx	//the shift operator	
  testl     %edx, %edx	//testl means !=	
  jne       .L2		//jne is a test condition for if not equal/not zero		
  movl      %edi, %eax


The comments are things I noted from reading about it. I interpreted these instructions as the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int a, *ap;
	a = 1;
	*ap = a;
	int total = *ap & a;
	int b = -1;
	b ^= 1;
	int *np;
	*np = n;
	a <<= a;
	if(a != a) {
		goto .L2;
	}
	b = a;
	return;


However, the proper result from converting it to C from assembly should be in this format:

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


Can someone help me understand how I should know when assembly is using a loop and what specifically in the assembly code indicates that?
Anytime you see a conditional jump back to a label in the code, you can be pretty sure it's some kind of loop.

In this case, line 13 is ANDing %edx to itself, which is just a way of testing if it's zero. If non-zero, then jump to .L2.
So the loop would look like:
1
2
3
4
// .L2
do 
{ /* instructions */
} while (a);

Last edited on
Topic archived. No new replies allowed.