FASM之HelloWorld

叶遮沉阳發表於2024-05-28

1. 環境準備

下載彙編器fasmflat assembler 1.73.32 for Windows

配置環境變數:

  • fasm安裝路徑新增至pathD:\dev_tools\fasm\fasmw17332
  • 設定INCLUDE變數:D:\dev_tools\fasm\fasmw17332\INCLUDE

2. 編寫程式碼

建立hello.asm

format PE console
entry start

include 'win32a.inc'

;======================================
section '.data' data readable writeable
;======================================

hello_newline    db "Hello World!",10,0
hello_no_newline db "Hello World! (without a new line)",0

;=======================================
section '.code' code readable executable
;=======================================

start:

        ccall   [printf],hello_newline      ; Print 'Hello World!' and start a new line.
        ccall   [printf],hello_no_newline   ; Print 'Hello World!' without starting a new line.

        ccall   [getchar]                   ; I added this line to exit the application AFTER the user pressed any key.
        stdcall [ExitProcess],0             ; Exit the application

;====================================
section '.idata' import data readable
;====================================

library kernel,'kernel32.dll',\
        msvcrt,'msvcrt.dll'

import  kernel,\
        ExitProcess,'ExitProcess'

import  msvcrt,\
        printf,'printf',\
        getchar,'_fgetchar'

3. 程式碼執行

命令列視窗執行:

F:\code-work\code-test\fasm>fasm hello.asm
flat assembler  version 1.73.32  (1048576 kilobytes memory)
3 passes, 0.1 seconds, 2048 bytes.

F:\code-work\code-test\fasm>hello.exe
Hello World!
Hello World! (without a new line)

fasm hello.asm 編譯並連結生成可執行程式hello.exe

4. 參考

notepad++ 配置fasm彙編環境

FASM: Hello World (Windows/Console)

相關文章