Skip to main content
edited title
Link
velkoon
  • 103
  • 2

AT&T - Does assembly code return whatever is in %eax if not otherwise specifiedby default?

Source Link
velkoon
  • 103
  • 2

AT&T - Does assembly code return whatever is in %eax if not otherwise specified?

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?