c程序调用nasm汇编函数

汇编部分代码如下:

#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的函数,



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值