This tutorial follows the old tutorial in the FASM programming series, see Programming with FASM – first steps.
The FASM manual that follows this example can be found here.
This is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | format PE64 GUI include "win64ax.inc" .data Caption db 'Win64 assembly program',0 Message db 'Hello World!',0 .code start: xor r9d,r9d lea r8,[Caption] lea rdx,[Message] xor rcx,rcx call [MessageBox] mov ecx,eax invoke ExitProcess,0 .end start |
As you can see is need to set the format and included file: PE64 GUI and win64ax.inc.
The .data and .code area are the same steps as any programming with the call instructions (but it is not mandatory, but for the beginning, it is very good).
Now about the Windows MessageBox Windows function named MessageBox, this takes four arguments.
1 | MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType); |
We need to use the xor to fix and prepare the lea for load strings.
The x64 registers: RCX, RDX, R8, R9 are used for integer and pointer arguments in that order left to right.
Let’s parse some simple examples with xor versus registers in order to see the sized data:
1 2 | xor ecx,ecx ; is this: int index = 0 xor r9d,r9d ; is this: unsigned index = 0 |
The next step is the lea:
The FASM manual tells us:
The source operand must be a memory operand, and the destination operand must be a general register.
lea dx,[bx+si+1] ; load effective address to dx
Now I explain the basic rules as simple as possible, but the derivations are very complex and they become the rules of addressing.
Here are the basic rules:
The Intel assembler uses the opposite order (destination <- source) for operands.
Operands can be immediate (that is, constant expressions that evaluate to an inline value), register (a value in the processor number registers), or memory (a value stored in memory). An indirect operand contains the address of the actual operand value.
In FASM, when you write “[X]”, you are referring to the variable X.
If you write just X, you are referring to its address of X.
The lea is like the & in C programming.