這是對我的工作:
ASM文件:
; file: blahasm.asm
; assemble with nasm (v 2.10, Mar 12 2012): nasm -f elf32 blahasm.asm -o blahasm.o
bits 32
global _blah
_blah:
mov eax, 42
ret
C文件:
// file: blahc.c
// compile with MinGW x86 (gcc v 4.6.2): gcc -Wall -O2 blahc.c blahasm.o -o blah.exe
#include <stdio.h>
extern int blah(void);
int main(void)
{
printf("blah():%d\n", blah());
return 0;
}
輸出:
blah():42
我很驚訝,唯一可用的格式是elf32
,並且它在MinGW所在的gcc的Windows端口中受支持。
UPDATE:
我已經創建了NASM和MinGW的唯一裝配程序。
ASM文件:
; file: nsm.asm
; assemble with NASM (v 2.10, Mar 12 2012): nasm -f elf32 nsm.asm -o nsm.o
; compile (link) with MinGW x86 (gcc v 4.6.2): gcc -Wall -O2 nsm.o -o nsm.exe
bits 32
extern ___main
extern _printf
global _main
section .rdata
textstr:
db "Hello World!", 10, 0
section .text.startup
_main:
call ___main
push textstr
call _printf
add esp, 4
ret
輸出:
Hello World!
我正在從 「MinGW的殼」(MSYS)gcc和我不需要指定任何其他命令行參數使程序與標準庫成功鏈接。而且我不需要對MinGW和MSYS安裝做任何特殊的處理,我想我使用了所有的默認設置參數。
那麼你用什麼命令來構建它?給我們提供錯誤信息只是問題的一半。 –
不要讓他們強迫你,萊利!站穩!但如果你必須,看看這是否相關http://stackoverflow.com/questions/3848357/gcc-createprocess-no-such-file-or-directory –
順便說一句,MinGW似乎把自己的東西放入'main ()',特別是'call ___ main'。我建議不要在程序集中實現'main()',因爲這個調用或者其他任何東西在缺失時可能會破壞事情。 –