NASM Preprocessor NASM preprocessor q NASM contains a

  • Slides: 15
Download presentation
NASM Preprocessor

NASM Preprocessor

NASM preprocessor q NASM contains a powerful macro processor, which supports conditional assembly, multi-level

NASM preprocessor q NASM contains a powerful macro processor, which supports conditional assembly, multi-level file inclusion, two forms of macro (single-line and multi-line), and a `context stack' mechanism for extra macro power. q Preprocessor directives all begin with a % sign. q The preprocessor collapses all lines which end with a backslash () character into a single line. Thus: %define THIS_VERY_LONG_MACRO_IS_DEFINED_TO THIS_VALUE will work like a single-line macro without the backslashnewline sequence.

Single-line macros q %define – defines single-line macro (c-style). %define ctrl 0 x 1

Single-line macros q %define – defines single-line macro (c-style). %define ctrl 0 x 1 F & %define param(a, b) ((a)+(a)*(b)) mov byte [param(2, ebx)], ctrl 'D' expand to mov byte [(2)+(2)*(ebx)], 0 x 1 F & 'D' q When the expansion of a single-line macro contains tokens which invoke another macro, the expansion is performed at invocation time, not at definition time. %define a(x) 1+b(x) %define b(x) 2*x mov ax, a(8) will evaluate in the expected way to mov ax, 1+2*8

Single-line macros (cont) q Macros defined with %define are case sensitive. You can use

Single-line macros (cont) q Macros defined with %define are case sensitive. You can use %idefine to define all the case variants of a macro at once. q There is a mechanism which detects when a macro call has occurred as a result of a previous expansion of the same macro, to guard against circular references and infinite loops. q You can overload single-line macros: %define foo(x) 1+x %define foo(x, y) 1+x*y The preprocessor will be able to handle both types of macro call, by counting the parameters you pass.

Single-line macros (cont) q %undef– undefines defined single-line macro %define foo goo %undef foo

Single-line macros (cont) q %undef– undefines defined single-line macro %define foo goo %undef foo mox ax, foo - will expand to the instruction mov eax, foo, since after %undef the macro foo is no longer defined. q To have a reference to an embedded single-line macro resolved at the time that it is embedded, as opposed to when the calling macro is expanded, you need a different mechanism to the one offered by %define. The solution is to use %xdefine, or it's caseinsensitive counterpart %xidefine.

Single-line macros (cont) q %define is. True 1 %define is. False is. True %define

Single-line macros (cont) q %define is. True 1 %define is. False is. True %define is. True 0 val 1: db is. False ; val 1 = ? %define is. True 1 val 2: db is. False ; val 2 = ? %xdefine is. True 1 %xdefine is. False is. True %xdefine is. True 0 val 1: db is. False ; val 1=? %xdefine is. True 1 val 2: db is. False; val 2=?

Single-line macros (cont) q %define is. True 1 %define is. False is. True %define

Single-line macros (cont) q %define is. True 1 %define is. False is. True %define is. True 0 val 1: db is. False ; val 1 = 0 %define is. True 1 val 2: db is. False ; val 2 = 1 %xdefine is. True 1 %xdefine is. False is. True %xdefine is. True 0 val 1: db is. False ; val 1=1 %xdefine is. True 1 val 2: db is. False; val 2=1 q In the left case, val 1 is equal to 0, and val 2 is equal to 1. This is because, when a single-line macro is defined using %define, it is expanded only when it is called. As is. False expands to is. True, the expansion will be the current value of is. True. The first time it is called that is 0, and the second time it is 1. q In the left case, each time that is. False is called, it expands to 1, as that is what the embedded macro is. True expanded to at the time that is. False was defined.

multiple-line macros q Works with %macro prologue 1 push ebp mov ebp, esp sub

multiple-line macros q Works with %macro prologue 1 push ebp mov ebp, esp sub esp, %1 %endmacro my_func: prologue 12 … %endmacro mechanism. this macro gets one parameter means: the first parameter of the macro my_func: push ebp mov ebp, esp sub esp, 12 q With a macro taking more than one parameter, subsequent parameters would be referred to as %2, %3 and so on.

multiple-line macros (cont) q Multi-line macros, like single-line macros, are case-sensitive, unless you define

multiple-line macros (cont) q Multi-line macros, like single-line macros, are case-sensitive, unless you define them using the alternative directive %imacro. q If you need to pass a comma as part of a parameter to a multiline macro, you can do that by enclosing the entire parameter in braces. %macro silly 2 %2: db %1 %endmacro silly 'a', letter_a silly 'ab', string_ab silly {13, 10}, crlf ; letter_a: db 'a' ; string_ab: db 'ab' ; crlf: db 13, 10

multiple-line macros (cont) q As with single-line macros, multi-line macros can be overloaded by

multiple-line macros (cont) q As with single-line macros, multi-line macros can be overloaded by defining the same macro name several times with different amounts of parameters. q Reserved words can also be overloaded: %macro push 2 push %1 push %2 %endmacro

multiple-line macros (cont) q Macros with a minimum amount of parameters can be defined:

multiple-line macros (cont) q Macros with a minimum amount of parameters can be defined: %macro name 2+ • The mark %2 will be replaced with second parameter and whatever follows it. q Macros with min. to max. amount of parameters: %macro name 0 -1 “default” • Here “default” will replace %1 in case none is given when invoked.

multiple-line macros – local labels q Defining a macro with an internal label: %macro

multiple-line macros – local labels q Defining a macro with an internal label: %macro retz 0 jnz %%skip ret %%skip: %endmacro • In every ‘retz’ invocation, the preprocessor will create some unique label of the form: . . @2345. skip

Gdb-GNU Debugger q Gdb is the standard debugger of the GNU Operating system q

Gdb-GNU Debugger q Gdb is the standard debugger of the GNU Operating system q You can run Gdb from the console by typing gdb execute_file_name q Adding a breaking point by typing: break label q Start debugging by typing: run parameters (for argv)

Gdb syntax • layout asm – represents your code after preprocessing (it seems quite

Gdb syntax • layout asm – represents your code after preprocessing (it seems quite different from the code you wrote but understandable). • si – one step forward • c – continue to run the code until the next break point. • q – quit gdb • p $eax – prints the value in eax • x $esp+4 – prints the address in esp + 4 hexadecimal and the value (dword) that stores in this address. It is possible to use label instead of esp. type x again will print the next dword in memory.