Flat Assembler Tutorial: Write Your First Assembly Program Assembly language gives you total control over your computer hardware. While many assemblers exist, Flat Assembler (FASM) stands out because it is lightweight, fast, and features a powerful macro system.
This tutorial guides you through setting up FASM and writing your very first x86-64 assembly program for Windows: a classic “Hello, World!” message box. What is Flat Assembler (FASM)?
Flat Assembler is a fast, self-assembling compiler for x86 and x86-64 architectures. Unlike other assemblers, it does not require a separate linker. FASM takes your source code and directly outputs an executable binary file, making the development workflow incredibly streamlined. Step 1: Set Up Your Environment Before writing code, you need to download the assembler. Visit the official Flat Assembler website. Download the latest Windows binary package.
Extract the downloaded ZIP file to a folder on your computer (e.g., C:asm).
Inside that folder, you will find fasmw.exe. This is the official integrated development environment (IDE) for Windows. Step 2: The Source Code
Open fasmw.exe, create a new file, and paste the following code. Save the file as hello.asm.
format PE64 GUI 5.0 entry start include ‘win64w.inc’ section ‘.text’ code readable executable start: sub rsp, 8*5 ; Reserve shadow space and align stack mov rcx, 0 ; hWnd = NULL (no owner window) lea rdx, [_message] ; lpText = address of our message string lea r8, [_caption] ; lpCaption = address of our title string mov r9, 0 ; uType = MB_OK (simple OK button) call [MessageBoxW] ; Call the Windows API function mov rcx, 0 ; uExitCode = 0 call [ExitProcess] ; Exit the program cleanly section ‘.data’ data readable writeable _caption db ‘FASM Tutorial’, 0 _message db ‘Hello, World! You just wrote your first FASM program.’, 0 section ‘.idata’ import data readable writeable library kernel32, ‘KERNEL32.DLL’,user32, ‘USER32.DLL’ import kernel32, ExitProcess, ‘ExitProcess’ import user32, MessageBoxW, ‘MessageBoxW’ Use code with caution. Step 3: Understanding the Code
Let’s break down exactly what this program is doing section by section. The Headers and Setup
format PE64 GUI 5.0: This tells FASM to create a 64-bit Windows executable file (Portable Executable) with a Graphical User Interface.
entry start: Defines the entry point of the program. Execution will begin at the start: label.
include ‘win64w.inc’: Includes a standard FASM file containing predefined constants and structures for the 64-bit Windows API. The Code Section (.text) This is where the CPU instructions live.
sub rsp, 8*5: The 64-bit Windows calling convention requires the caller to reserve 32 bytes of “shadow space” on the stack before calling a function, plus an extra 8 bytes to keep the stack 16-byte aligned.
The Registers (rcx, rdx, r8, r9): Windows passes the first four arguments of a function through these registers. We load them with our window handle, message text address, caption text address, and button type.
call [MessageBoxW]: This triggers the Windows API to display the visual popup box.
call [ExitProcess]: Tells Windows that our program is finished and closes it safely. The Data Section (.data)
This section stores variables. We define our caption and message strings here. The db directive stands for “define byte,” and the , 0 at the end creates a null-terminator, which tells Windows where the text ends. The Import Section (.idata)
Because our program relies on Windows to display the message box and exit, we must map our code to the standard Windows system libraries (USER32.DLL and KERNEL32.DLL). This section links the function names used in our code to the actual functions inside Windows. Step 4: Compile and Run Compiling with FASM is instantaneous. In the FASM Windows IDE, look at the top menu bar.
Click on Run and then select Compile (or simply press Ctrl + F9).
FASM will generate a hello.exe file in the same directory as your source code. Go to Run and click Run (or press F9) to execute it.
A small graphical window will pop up on your screen displaying your “Hello, World!” message. Click “OK” to close the program safely. Conclusion
Congratulations! You have successfully stepped into the world of low-level programming. You set up Flat Assembler, configured a 64-bit Windows executable, managed CPU registers, and interacted directly with the Windows operating system API.
From here, you can explore math operations, memory management, or even write performance-critical logic for larger software projects. To help you take the next steps with FASM, tell me:
Are you interested in printing text to a command-line console instead of a pop-up window? AI responses may include mistakes. Learn more
Leave a Reply