1 / 15

NASM Preprocessor

NASM Preprocessor. NASM preprocessor. 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.

hasad
Download Presentation

NASM Preprocessor

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. NASM Preprocessor

  2. NASM preprocessor • 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. • Preprocessor directives all begin with a % sign. • 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 backslash-newline sequence.

  3. Single-line macros • %define – defines single-line macro (c-style).%define ctrl 0x1F & %define param(a, b) ((a)+(a)*(b))mov byte [param(2,ebx)], ctrl 'D' expand to mov byte [(2)+(2)*(ebx)], 0x1F & 'D' • 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 movax,a(8) will evaluate in the expected way to mov ax,1+2*8

  4. Single-line macros (cont) • Macros defined with %define are case sensitive. You can use %idefine to define all the case variants of a macro at once. • 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. • 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.

  5. Single-line macros (cont) • %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. • 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 case-insensitive counterpart %xidefine.

  6. Single-line macros (cont) • %define isTrue 1 %xdefineisTrue 1 %define isFalseisTrue %xdefineisFalseisTrue%define isTrue 0 %xdefineisTrue 0 val1: db isFalse ; val1 = ?val1: db isFalse ; val1=?%define isTrue 1 %xdefineisTrue 1 val2: db isFalse ; val2 = ?val2: db isFalse; val2=?

  7. Single-line macros (cont) • %define isTrue 1 %xdefineisTrue 1 %define isFalseisTrue %xdefineisFalseisTrue%define isTrue 0 %xdefineisTrue 0 val1: db isFalse ; val1 = 0 val1: db isFalse ; val1=1%define isTrue 1 %xdefineisTrue 1 val2: db isFalse ; val2 = 1 val2: db isFalse; val2=1 • In the left case, val1 is equal to 0, and val2 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 isFalse expands to isTrue, the expansion will be the current value of isTrue. The first time it is called that is 0, and the second time it is 1. • In the left case, each time that isFalse is called, it expands to 1, as that is what the embedded macro isTrue expanded to at the time that isFalse was defined.

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

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

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

  11. multiple-line macros (cont) • 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. • 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.

  12. multiple-line macros – local labels • 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

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

  14. 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.

More Related