Passing arguments from C to 64bit linux Assembly

烂漫一生 提交于 2019-12-08 09:39:03

问题


I'm trying to write my first assembly function to be called from a C program. Here is the .c part:

#include <stdio.h>

extern int sum(int a, int b);

int main() {

printf("2+3 = %d\n", sum(2,3));

    return 0;
}

And the assembly part:

.text

.global sum

sum:

push %rbp
mov %rsp, %rbp

mov 8(%rbp), %rax
add 12(%rbp), %rax

pop %rbp

ret

According to some tutorials, arguments should be stored in %ebp (in 32bit version) register. However, when I compile the code above, I'm getting this result:

2+3 = 4195607

Could someone explain me what am I doing wrong?

Oh, and here is the Makefile:

test: test.o testc.o
    gcc -o test test.o testc.o
test.o: f1.s
    gcc -o test.o -c f1.s
testc.o: f2.c
    gcc -c -o test.o f2.c

Thank you in advance for any tips!

Regards, Filip

来源:https://stackoverflow.com/questions/22917922/passing-arguments-from-c-to-64bit-linux-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!