我正在使用 nasm编译以下程序集。然而,代码在 Windows 下的控制台中崩溃。
C:\>nasm -f win32 test.asm -o test.o
C:\>ld test.o -o test.exe
section .data
msg db 'Hello world!', 0AH
len equ $-msg
section .text
global _WinMain@16
_WinMain@16:
mov edx, len
mov ecx, msg
mov ebx, 1
mov eax, 4
int 80h
mov ebx, 0
mov eax, 1
int 80h
根据这个post . main
函数在 Windows 下不可用,必须用 WinMain
代替。
因此,如果您的入口点是_start
或main
,则应将其更改为_WinMain@16
并更改ret
在过程结束时 ret 16
:
我的工作示例:
section .text
global _WinMain@16
_WinMain@16:
mov eax, 0
ret 16
最佳答案
最大的问题是你试图在 Windows 上使用 Linux 中断! int 80 不适用于 Windows。
我们正在使用 Assembly,因此您的入口点可以是您想要的任何标签。 ld寻找的标准入口点是_start,如果你想使用另一个标签,你需要用-e选项告诉ld 所以如果你想让你的开始标签成为main,那么你需要
global main
ld -e main test.o -o test.exe
如果您要在 Windows 上使用 NASM,我建议您使用 GoLink 作为链接器。 这是一个简单的 Windows 控制台应用程序:
STD_OUTPUT_HANDLE equ -11
NULL equ 0
global GobleyGook
extern ExitProcess, GetStdHandle, WriteConsoleA
section .data
msg db "Hello World!", 13, 10, 0
msg.len equ $ - msg
section .bss
dummy resd 1
section .text
GobleyGook:
push STD_OUTPUT_HANDLE
call GetStdHandle
push NULL
push dummy
push msg.len
push msg
push eax
call WriteConsoleA
push NULL
call ExitProcess
生成文件:
hello: hello.obj
GoLink.exe /console /entry GobleyGook hello.obj kernel32.dll
hello.obj: hello.asm
nasm -f win32 hello.asm -o hello.obj
关于windows - Hello world 在 Windows 程序集中使用 nasm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12574924/