汇编部分代码如下:
#myfunc.asm nasm -f elf -g -F stabs
global myfunc
myfunc:
extern printf
push dword [myint] ; one of my integer variables
push dword mystring ; pointer into my data segment
call printf
add esp,byte 8 ; `byte' saves space
; then those data items...*/
segment _DATA
myint dd 1234
mystring db 'This number -> %d <- should be 1234',10,0
global myfunc定义了一个函数,以供外部调用
extern printf引入了外部函数,相当于c中的声明,(我看到的别人的代码都是extern _printf,因为printf的原型就是_printf,但我写_printf,链接的时候会报找不到_printf)
add esp,byte 8是将栈指针地址加8,因为上两行有两个 push dword,所以加8之后正好指向myfunc返回的地址
c程序代码如下:
#main.c gcc -g -c main.c gcc -o main main.o myfunc.o
#include <stdio.h>
int _myfunc() __attribute__((cdecl));(去掉_myfunc前的下划线也可以,不知为什么)
int main(void)
{
myfunc();
return 0;
}
int _myfunc() __attribute__((cdecl))声明了一个返回值为int,无参的名为myfunc的函数,