Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

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 ccalling assembly function from c

calling convention: https://en.wikipedia.org/wiki/X86_calling_conventions

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

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

Source Link
stian
  • 2k
  • 5
  • 29
  • 49

how to get argument integer in callee?

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