Stack Review
CS 301 Lecture, Dr. Lawlor, 2005/10/10
First, a word of warning about the homework:
push compare
actually treats the label "compare" as a *memory reference*, and pushes
the first 4 bytes of the compare *subroutine* onto the stack! To
just push the pointer compare (instead of the first int of the machine code it points
to!), instead say:
push $compare
This should make your HW4_2 not crash anymore...
Inline Assembly: Values In (arguments) and Out (subroutine return value)
Here's an example (Executable Example in NetRun)
where we write a subroutine in assembly that takes two parameters as
input (x and y), add them together, and return the result. As
usual, the compiler just pushes the input parameters onto the stack, so
we extract them by loading relative to esp (if we had set up ebp, we'd
load relative to that). As usual again, we return our value in
register %eax.
extern "C" int do_stuff(int x,int y);
__asm__(
"do_stuff:\n"
" mov 4(%esp),%eax # parameter x\n"
" add 8(%esp),%eax # parameter y\n"
"# Note: return value in %eax\n"
" ret\n"
);
int foo(void) {
return do_stuff(3,4);
}
This should be all you need for HW4!