0

I have the following AT&T assembly code:

  movl 12(%ebp),%eax
  cmpl %eax,8(%ebp)
  jle L7
  movl 8(%ebp),%eax
L7:
  leave

I'm supposed to "transpose"(??) it to C code. I actually just have to fill in the blanks in this skeleton C code:

int g(int x, int y) {
    if (x ______ y)
        return ______;
    else
        return ______;
}

From what I unerstand, the assembly is going to return whatever is in %eax when done.

So this is how I understand what's happening:

The cmpl %eax,8(%ebp) line is comparing x (8(%ebp)) with y (%eax). If x is <= to y, we jump to L7: and return whatever is in %eax at that time, which is y. Otherwise, we proceed to the next line in the assembly code and movl x (8(%ebp)) to %eax, and return whatever is in %eax, which would be x at that point.

In the end, this is what I think is happening:

int g(int x, int y) {
    if (x <= y)
        return y;
    else
        return x;
}

Am I correct in saying that the assembly returns whatever is in %eax when the assembly code is finished running?

0
1

Am I correct in saying that the assembly returns whatever is in %eax when the assembly code is finished running?

Assembly doesn't "return" anything.

In assembly, it is left to the programmer to decide how to pass values between the caller and callee routines. In order to avoid things getting messy, calling conventions have been devised.

Your question therefore becomes "what calling convention was used to compile the unknown C code to the known assembly?"

When compiling for x86 architectures, C compilers usually use the cdecl convention. Working on the assumption that was the convention used here, then integer results would indeed always be returned in register EAX.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.