程式語言 - MASM32 - Hello, world!



參考資訊:
https://www.masm32.com/

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

main.asm

    .386
    .model flat,stdcall
    option casemap:none

include c:\masm32\include\windows.inc
include c:\masm32\include\kernel32.inc
include c:\masm32\include\user32.inc

includelib c:\masm32\lib\user32.lib
includelib c:\masm32\lib\kernel32.lib

    .data
caption db "main",0
content db "Hello, world!",0

    .code
start:
    invoke MessageBox, NULL, offset content, offset caption, MB_OK
    invoke ExitProcess, 0
end start

Line 1:CPU指令
Line 2:記憶體類型是flat,calling convention使用stdcall
Line 3:程式區分大小寫
Line 5~7:Header檔案
Line 9~10:Library檔案
Line 12:初始化的資料區段
Line 13~14:初始化的Global變數,型態是byte(char)且最後一個byte是0
Line 16:程式區段
Line 17:程式進入點
Line 18:顯示Message對話盒,invoke是一個偽指令,最終會展開成call + push的綜合指令
Line 19:結束Process並且釋放相關資源
Line 20:檔案結尾,end指令後的程式將不會被編譯

Makefile

export WINEPREFIX=/home/user/.wine_x86

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

all:
	${MYWINE} ${MASM32}/bin/ml.exe /c /coff /nologo ${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