如何从汇编例程调用C函数并使用nasm和gcc链接C和汇编文件

我想从汇编中调用至少1个C函数。 这是因为我从头开始做我自己的小操作系统(无中生有)。 我想从我的启动加载程序调用c函数的原因。 我可以理解集会,但写自己的程序很差。 因此,如果我可以将控制从assembly程序转移到c程序,我的工作就变得更容易了。

那么如何将程序集pgm和C程序文件链接成一个。 即使文件大小超过512字节,也可以。 我在mingw的帮助下在Windows 7上这样做。 我的c编译器是gcc ,汇编器是nasm

更容易向您展示一个例子,我刚才在互联网上发现了这个并将其保存为我的计算机上的源,不知道从哪里来

 ; printf1.asm print an integer from storage and from a register ; Assemble: nasm -f elf -l printf.lst printf1.asm ; Link: gcc -o printf1 printf1.o ; Run: printf1 ; Output: a=5, eax=7 ; Equivalent C code ; /* printf1.c print an int and an expression */ ; #include ; int main() ; { ; int a=5; ; printf("a=%d, eax=%d\n", a, a+2); ; return 0; ; } ; Declare some external functions ; extern printf ; the C function, to be called SECTION .data ; Data section, initialized variables a: dd 5 ; int a=5; fmt: db "a=%d, eax=%d", 10, 0 ; The printf format, "\n",'0' SECTION .text ; Code section. global main ; the standard gcc entry point main: ; the program label for the entry point push ebp ; set up stack frame mov ebp,esp mov eax, [a] ; put a from store into register add eax, 2 ; a+2 push eax ; value of a+2 push dword [a] ; value of variable a push dword fmt ; address of ctrl string call printf ; Call C function add esp, 12 ; pop stack 3 push times 4 bytes mov esp, ebp ; takedown stack frame pop ebp ; same as "leave" op mov eax,0 ; normal, no error, return value ret ; return