문제

ATT syntax.

I'm trying to understand what the following piece of assembly code does:

movl  8(%ebp), %edx
movl  $0, %eax
testl %edx, %edx
je    .L7
.L10:
xorl  %edx, %eax
shrl  %edx
jne   .L10
.L7:
andl  $1, %eax

It's supposed to be the body of a function with one parameter: unsigned x. I know that this is a do-while loop but how can I completely convert it to C code?

도움이 되었습니까?

해결책

unsigned int function(unsigned int x)
{
  unsigned int a = 0;

  while (x != 0)
  {
    a = a ^ x;
    x = x >> 1;
  }

  a = a & 1;

  return a;
}

다른 팁

Easy:

void or unsigned some_function(unsigned x)
{
  unsigned edx = x;
  unsigned eax = 0;
  if (edx == 0)
    goto L7;
L10:
  eax ^= edx;
  if ((edx >>= 1) != 0)
    goto L10;
L7:
  eax &= 1;
  // ...
}

It is important to remember that eax is the return register in x86 (even though this snippet does not contain the ret instruction). Here, the function returns true if the first bit of eax is 1.

This algorithm xor all bits of the input and return the resulting bit.

It can be summarized in a short high-level sentence: it returns 1 if the numbers of 1 bits of the input is odd and 0 if it's even (you need to think a little about it to see it, I can explain more thoroughly if you need more details).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top