How to write hello world in assembler under Windows?

前端 未结 8 917
情书的邮戳
情书的邮戳 2020-11-22 12:01

I wanted to write something basic in assembly under Windows, I\'m using NASM, but I can\'t get anything working.

How to write and compile hello world without the he

8条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 12:38

    This example shows how to go directly to the Windows API and not link in the C Standard Library.

        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', 10
    message_end:
    

    To compile, you'll need NASM and LINK.EXE (from Visual studio Standard Edition)

       nasm -fwin32 hello.asm
       link /subsystem:console /nodefaultlib /entry:main hello.obj 
    

提交回复
热议问题