Macros

single-line

multiline

Useful standard macros

STRUC

%include

Conclusion

It is a fifth part ofand here we will look at macros. It will not be blog post about x86_64, mainly it will be about nasm assembler and it's preprocessor. If you're interesting in it read next.NASM supports two form of macro:All single-line macro must start fromdirective. It form is following:Nasm macro behaves and looks very similar as in C. For example, we can create following single-line macro:and than use it in code:Multiline macro starts withnasm directive and end with. It general form is following:For example:And we can use it:For example let's look atmacro:Let's try to go through it macro and understand how it works: At first line we defined PRINT macro with one parameter. Than we push all general registers (withinstruction) and flag register with (withinstruction). After this we jump tolabel. Pay attention that all labels which defined in macro must start with. Now we move tomacro with 2 parameter. Let's look onimplementation. You can remember that we usesystem call in all previous posts for printing string to stdout. It looks like this:In ourmacro we define first two instruction for putting 1 to rax (write system call number) and rdi (stdout file descriptor). Than we putto rsi register (pointer to string), where %%str is local label to which is get first parameter ofmacro (pay attention that macro parameter access by $parameter_number) and end with 0 (every string must end with zero). Andwhich calculates string length. After this we call system call with syscall instruction and that's all.Now we can use it:NASM supports following standard macros:We can useandfor data structure defintion. For example:And now we can make instance of our structure:We can include other assembly files and jump to there labels or call functions withdirective.It was a fifth part of series 'say hello to x64 assembly', if you will have a questions/suggestions write me a comment or ping me at twitter for discussing. In next part we will talk about difference between Intel syntax and AT&T and look at gas assembly. If you're interesting in some additional themes about assembly and x86_64 programming write me a comment and I will try to write blog post about it in near time.All another parts you can find - here All source code you can find as every time - here English is not my first language, so you'll find mistakes in blog post please write me in comments or drop me email