hello.asm 代码如下:(我也不知道对不对了,但是能跑起来...)
extern ExitProcess extern GetStdHandle extern WriteConsoleA extern AttachConsole STD_OUTPUT_HANDLE equ -11 INVALID_HANDLE_VALUE equ -1 ATTACH_PARENT_PROCESS equ -1 global Start section .data H_STDOUT dd 0 iCount dd 0 msg db "hello nams!", 0, 0dh,0ah msg_len equ $ - msg section .text Start: push STD_OUTPUT_HANDLE call GetStdHandle cmp eax, INVALID_HANDLE_VALUE jz finished mov [H_STDOUT], eax push ATTACH_PARENT_PROCESS call AttachConsole push 0 lea eax, [iCount] push eax push msg_len lea eax, [msg] push eax ;lea eax, [ H_STDOUT] ;push DWORD [ eax] push DWORD [H_STDOUT] call WriteConsoleA finished: xor eax, eax call ExitProcess
编译步骤如下:
nasm.exe -f win32 hello.asm -o hello.obj
GoLink.exe /console /entry:Start kernel32.dll user32.dll hello.obj
/console 控制台程序要加上这个标志,要不 GetStdHandle 都是返回NULL。
运行结果如下: