How to write hello world in assembler under Windows?

前端 未结 8 1077
情书的邮戳
情书的邮戳 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:34

    To get an .exe with NASM'compiler and Visual Studio's linker this code works fine:

    global WinMain
    extern ExitProcess  ; external functions in system libraries 
    extern MessageBoxA
    
    section .data 
    title:  db 'Win64', 0
    msg:    db 'Hello world!', 0
    
    section .text
    WinMain:
        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
        call ExitProcess
    
        hlt     ; never here
    

    If this code is saved on e.g. "test64.asm", then to compile:

    nasm -f win64 test64.asm
    

    Produces "test64.obj" Then to link from command prompt:

    path_to_link\link.exe test64.obj /subsystem:windows /entry:WinMain  /libpath:path_to_libs /nodefaultlib kernel32.lib user32.lib /largeaddressaware:no
    

    where path_to_link could be C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin or wherever is your link.exe program in your machine, path_to_libs could be C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x64 or wherever are your libraries (in this case both kernel32.lib and user32.lib are on the same place, otherwise use one option for each path you need) and the /largeaddressaware:no option is necessary to avoid linker's complain about addresses to long (for user32.lib in this case). Also, as it is done here, if Visual's linker is invoked from command prompt, it is necessary to setup the environment previously (run once vcvarsall.bat and/or see MS C++ 2010 and mspdb100.dll).

提交回复
热议问题