在C语言中调用nasm子过程.docVIP

  • 20
  • 0
  • 约1.38千字
  • 约 4页
  • 2018-02-15 发布于河南
  • 举报
在C语言中调用nasm子过程

在 C语言中调用nasm过程,在nasm中调用C运行时库 作者:Allen C调用nasm过程 GCC的函数原型声明:int f(int) __attribute__((cdecl)); 可以是cdecl也可以是stdcall,stdcall约定在函数返回前必需将参数出栈,因此不能使用在参数不定的函数调用上,例如scanf和printf_s。 VC/BCB的函数原型声明:int __cdecl f(int); C语言代码: // gcc –c filename.c #include stdio.h int fact(int) __attribute__((cdecl)); int main(void) { int n,sum; printf(Sum integers up to: ); scanf(%d,n); sum = fact(n); //,sum); printf(Sum is %d\n,sum); return 0; } Nasm自过程代码: ; nasm –f coff filename.asm segment .text global _fact _fact: enter 0,0 mov eax,[ebp+8] ;eax = n cmp eax,1 jbe term_cond ;if (n = 1) dec eax ;n -= 1 push eax call _fact ;eax = fact(n-1) pop ecx mul dword [ebp+8] ;edx:eax = eax * [ebp + 8] jmp short end_fact term_cond: mov eax,1 end_fact: leave ret 将生成的.o文件用GCC连接:gcc –o filename cfile.o asmfile.o Nasm调用C运行时库函数 %ifdef ELF_TYPE %define _scanf scanf %define _printf printf %define _getchar getchar %define _putchar putchar %define _fputs puts %endif extern _scanf, _printf, _getchar, _putchar, _fputs segment .data format db %d,0 segment .text global _asm_main _asm_main: enter 0,0 pusha lea eax,[ebp-16] push eax push dword format call _scanf add esp,8 popa leave ret 注:在C中声明的函数在nasm代码中要加单下划线调用,例如scanf预定于为_scanf

文档评论(0)

1亿VIP精品文档

相关文档