程式語言 - Netwide Assembler (NASM) - Win32 API - Hello, world!



參考資訊:
https://masm32.com/board/index.php
https://www.nasm.us/xdoc/2.13rc9/html/nasmdoc0.html

經典的Hello, world!程式框架總是能夠讓人細心品味一款程式語言的美好,司徒就使用一個簡單的Message對話盒來展現Hello, world!框架

main.asm

    [bits 32]
    global _start

    extern _MessageBoxA@16
    extern _ExitProcess@4

    %define MessageBox  _MessageBoxA@16
    %define ExitProcess _ExitProcess@4

    section .drectve info
_entry    db "/entry:start "
_gdi32    db "/defaultlib:c:\\masm32\\lib\\gdi32.lib "
_user32   db "/defaultlib:c:\\masm32\\lib\\user32.lib "
_kernel32 db "/defaultlib:c:\\masm32\\lib\\kernel32.lib "

    segment .data
szCaption db "main",0
szContent db "Hello, world!",0

    segment .text
_start:
    push 0
    push szCaption
    push szContent
    push 0
    call MessageBox

    push 0
    call ExitProcess

Line 7~8:為了方便名稱使用,重新定義名稱
Line 10~14:傳遞給Linker的指令
Line 16:初始化的資料區段
Line 17~18:定義字串
Line 20:程式區段
Line 21:程式進入點
Line 22~26:顯示Message對話盒
Line 28~29:結束Process並且釋放相關資源

Makefile

export WINEPREFIX=/home/user/.wine_x86

TARGET=main
MYWINE=box86 wine
NASM32=/home/user/.wine_x86/drive_c/nasm
MASM32=/home/user/.wine_x86/drive_c/masm32

all:
	${MYWINE} ${NASM32}/bin/nasm.exe -fwin32 ${TARGET}.asm
	${MYWINE} ${MASM32}/bin/link.exe /SUBSYSTEM:WINDOWS /MERGE:.rdata=.text ${TARGET}.obj

run:
	${MYWINE} ${TARGET}.exe

clean:
	rm -rf ${TARGET}.exe ${TARGET}.obj

編譯、執行

$ make
$ make run