Can you write a Windows API Hello World In Assembly in 2019?
Short answer?
Yes. Long answer?
BUT Why would you do this to yourself?
HAHA
Because its cool..
Actually In the wee days of Windows 3.1 we wrote mostly in C.. And occasionally dipped in to ASM as needed for performance.
Computers were slower back then, so we had to really push things sometimes..
Like not just for fun.. Our computers were much slower.. :)
So lets see if we can do some real bare-metal, old-school, hair-pulling, Windows API madness.
π οΈ Tools Youβll Need
Before we get into the weeds, letβs make sure you have the right tools. You can’t just yell at the computer in assembly and expect it to listen.
- MASM (Microsoft Macro Assembler) β Comes with Visual Studio, or you can grab it separately.
- GoLink β A free linker that plays well with assembly.
- GoASM β If you prefer it over MASM.
- Windows SDK β Because weβre working with Windows API.
- A solid cup of coffee β Trust me, youβll need it.
βοΈ The Assembly Code (MASM Style)
Here it isβthe moment you’ve been waiting for. Our lovely Windows GUI Hello World in assembly:
|
|
π What This Code Does
- Registers a window class β Because Windows likes bureaucracy.
- Creates the main window β Where all the magic happens.
- Creates a label, text field, and button β The UI elements.
- Handles button clicks β Reads text from the input field and pops up a
MessageBox
. - Runs the Windows message loop β Because Windows loves messages.
π Breaking Down the Assembly Code
If you didnβt run away the first time you saw the assembly code, you probably noticed itβs doing a few key things:
- Setting up the application β Registering a window class and creating a main window.
- Creating UI elements β Label, text field, and button.
- Handling Windows messages β Because Windows apps are event-driven.
- Processing button clicks β Fetching input text and displaying a message box.
ποΈ Step 1: Setting Up the Window Class
|
|
GetModuleHandle
fetches the instance handle of the application.mov hInstance, eax
stores it for later use.
|
|
- Registers a window class with Windows, telling it what kind of window we want.
πΌοΈ Step 2: Creating the Window and UI Elements
|
|
- This creates the main window and stores the handle in
hWnd
.
We repeat this process for:
- Label:
"Name:"
- Text Field:
ES_AUTOHSCROLL
(allows text input) - Button:
"Say Hello"
π Step 3: The Windows Message Loop
|
|
GetMessage
retrieves messages from the Windows event queue.TranslateMessage
andDispatchMessage
send them to the correct window procedure.
π±οΈ Step 4: Handling Button Clicks
|
|
- If the button is clicked (
wParam == 1
), we:- Get text from the input field
- Display it in a message box
π’ Assembly Instructions Used- What they do
Instruction | Meaning |
---|---|
mov | Moves data from one register/memory location to another |
cmp | Compares two values |
jne | Jumps to a label if values are not equal |
je | Jumps to a label if values are equal |
invoke | Calls a Windows API function |
jmp | Jumps unconditionally to a label |
ret | Returns from a function/procedure |
π The Same Program in C (WinAPI)
Now, letβs rewrite this in C to see how much easier our lives could have been.
|
|
βοΈ C vs Assembly
Feature | Assembly | C |
---|---|---|
Readability | Nightmare fuel | Much easier to follow |
Lines of Code | A ton | Half as much |
Memory Control | Absolute control | Higher-level, but less hassle |
Performance | Slightly faster | Still fast enough |
Ease of Debugging | Painful | Debugging tools help a lot |
Portability | Not portable | C is far more portable |
π Verdict
- Use Assembly if you want total control over performance, memory, and execution.
- Use C if you want readable, maintainable, and scalable code.