Module template
A small guarded module skeleton for adding another chapter or reusable procedure.
A chapter module needs very little ceremony: a guard, the sections it uses, and one or more procedures with valid unwind information.
Guard the module
The ifndef guard prevents duplicate definitions if the file is included more than once. Give every real module a unique guard symbol.
ifndef NAME_asm ; header guard
NAME_asm = 0 ; header guard variable
Keep in mind: Rename both NAME and the guard before adding the module to main.asm.
Keep declarations in their sections
Constants belong in .const, writable storage in .data, and instructions in .code. Empty sections can be removed when a module does not use them.
; --- const section ---
.const
; --- data section ---
.data
; --- code section ---
.code
Keep in mind: A predictable file shape makes small modules easy to scan.
Start with a valid leaf procedure
This skeleton creates a frame-aware leaf procedure, returns zero through rax, and ends without touching the stack. Add saved registers or stack allocation only when the implementation needs them.
procedure proc frame
.endprolog
xor rax, rax ; return zero
ret ; return
procedure endp ; end proc
Keep in mind: Keep prologue directives adjacent to the instructions they describe.
Keep these rules
- Use one unique include guard per module.
- Separate constants, data, and code.
- Begin with the smallest correct procedure frame.