I have the following code (caller.c):
#include <stdio.h> extern int callee(int); int main(int argc, char *argv[]){ callee(4); return 1; } and (callee.s):
.globl callee callee: pop %eax add $4, %eax ret I compile with: gcc -m32 caller.c callee.s
and run:
./a.out
Segmentation fault (core dumped)
I am wondering what my mistake(s) is/are as I believed that main should now push a 32 bite number of the stack. I havent changed stack so that callee should now be able to pop that number from the same stack. Maybe I should add (add $4, %esp) before the pop (if the address of callee is in the "way"/actually been popped). I have tried that too with no success. callee should now get the number from the stack and add 4 to it. The eax-register should be where the return value from callee to caller should be kept (calling convention), but here I ignore the return value.
Could someone assist me?
related question: calling assembly function from c
calling convention: https://en.wikipedia.org/wiki/X86_calling_conventions
pop %eaxtakes the return address pushed by the CALL instruction and puts in in EAX. Remove thepop %eaxand simply access the first argument viamov 4(%esp), %eax. After execution is transferred to a function (in 32-bit code) the return address is at 0(ESP), first argument at 4(ESP), second argument at 8(ESP) etc.eax(however, check the specifics of your platform/compiler).