如何在Windows下用汇编程序编写Hello World?

如何在Windows下用汇编程序编写Hello World?

我想在Windows下编写一些基本的汇编语言,我正在使用NASM,但是我不能让任何东西工作。

如何在Windows上不借助C函数编写并编译Hello World?



翻阅古今
浏览 768回答 3
3回答

慕后森

此示例演示如何直接转到WindowsAPI,而不是在C标准库中链接。    global _main    extern  _GetStdHandle@4     extern  _WriteFile@20     extern  _ExitProcess@4     section .text _main:     ; DWORD  bytes;         mov     ebp, esp    sub     esp, 4     ; hStdOut = GetstdHandle( STD_OUTPUT_HANDLE)     push    -11     call    _GetStdHandle@4     mov     ebx, eax         ; WriteFile( hstdOut, message, length(message), &bytes, 0);     push    0     lea     eax, [ebp-4]     push    eax     push    (message_end - message)     push    message     push    ebx     call    _WriteFile@20     ; ExitProcess(0)     push    0     call    _ExitProcess@4     ; never here     hlt message:     db      'Hello, World', 10message_end:要编译,您需要NASM和LINK.EXE(来自VisualStudioStandardEdition)   nasm -fwin32 hello.asm    link /subsystem:console /nodefaultlib /entry:main hello.obj

人到中年有点甜

这些是使用WindowsAPI调用的Win 32和Win 64示例。他们是为MASM而不是NASM,但看看他们。您可以在这,这个文章。;---ASM Hello World Win32 MessageBox.386.model flat, stdcall include kernel32.inc includelib kernel32.lib include user32.inc includelib user32.lib.data title db 'Win32', 0msg db 'Hello World', 0.codeMain:push 0            ; uType = MB_OK push offset title ; LPCSTR lpCaption push offset msg   ; LPCSTR lpText push 0            ; hWnd = HWND_DESKTOP call MessageBoxApush eax          ; uExitCode = MessageBox(...)call ExitProcessEnd Main;---ASM Hello World Win64 MessageBoxextrn MessageBoxA: PROC extrn ExitProcess: PROC.data title db 'Win64', 0msg db 'Hello World!', 0.code main proc  sub rsp, 28h     mov rcx, 0       ; hWnd = HWND_DESKTOP   lea rdx, msg     ; LPCSTR lpText   lea r8,  title   ; LPCSTR lpCaption   mov r9d, 0       ; uType = MB_OK   call MessageBoxA   add rsp, 28h     mov ecx, eax     ; uExitCode = MessageBox(...)   call ExitProcessmain endpEnd若要使用MASM组装和链接这些文件,请将其用于32位可执行文件:ml.exe [filename] /link /subsystem:windows  /defaultlib:kernel32.lib /defaultlib:user32.lib /entry:Main或者对于64位可执行文件:ml64.exe [filename] /link /subsystem:windows  /defaultlib:kernel32.lib /defaultlib:user32.lib /entry:main
打开App,查看更多内容
随时随地看视频慕课网APP