Practical Session 6 NASM Preprocessor NASM contains a





























![6 שאלה Gloat: shl ebx, 2 jmp [ebx+Tab] Tab: dd F 4 dd F 6 שאלה Gloat: shl ebx, 2 jmp [ebx+Tab] Tab: dd F 4 dd F](https://slidetodoc.com/presentation_image_h/1d58e36ce48a891cc33e6172b584c36b/image-30.jpg)
![6 שאלה Gloat: shl ebx, 2 jmp [ebx+Tab] Tab: dd F 4 dd F 6 שאלה Gloat: shl ebx, 2 jmp [ebx+Tab] Tab: dd F 4 dd F](https://slidetodoc.com/presentation_image_h/1d58e36ce48a891cc33e6172b584c36b/image-31.jpg)


- Slides: 33
Practical Session 6
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) * a `context stack' mechanism for extra macro power • Preprocessor directives all begin with a % sign * We are going to cover only this subsection of NASM macro processor. Use http: //www. nasm. us/doc/nasmdoc 4. html link to read more.
Macro - definition • Macro is a set of statements given a symbolic name • Macro is invoked, not called. A copy of the macro is inserted directly into the program • After being defined, NASM will substitute (expand) those statements whenever it finds the symbolic name macro definition Source code macro name Expanded code my. Macro macro body (statements) macro usage . my. Macro. . NASM preprocessor . .
Single-line macros • %define – defines single-line macro • a macro is expanded only when it is called Example: %define ctrl 0 x 1 F & %define param(a, b) ((a)+(a)*(b)) mov byte [param(2, ebx)], ctrl 'D' expands to by NASM preprocessor mov byte [(2)+(2)*(ebx)], 0 x 1 F & 'D' Example: %define a(x) 1+b(x) %define b(x) 2*x mov ax, a(8) expands to mov ax, 1+2*8 by NASM preprocessor
Single-line macros (cont) • We can overload single-line macros. The preprocessor will be able to handle both types of macro call, by counting the parameters you pass. %define foo(x) 1+x %define foo(x, y) 1+x*y %define foo 1+ ebx A macro with no parameters prohibits the definition of the same name as a macro with parameters, and vice versa. • Macros defined with %define are case sensitive. We 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.
Single-line macros (cont) • %define - a macro resolved at the time that it is called (used) • %xdefine - a macro resolved at the time that it is defined Example: %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) • In the left case, when ‘is. False’ macro uses %define, so 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. • In the right case, each time that ‘is. False’ is called, it expands to 1, as that is what the macro ‘is. True’ expanded to at the time that ‘is. False’ was defined. %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
Single-line macros (cont) • %undef – undefines defined single-line macro Example: %define foo(x) 1+x %undef foo mov ax, foo(3) ; would not be expanded since ; after %undef the macro foo is no longer defined
Multiple-line macros • Works with %macro … %endmacro mechanism • Macro parameters would be referred to as %1, %2, %3 and so on Example: %macro foo 1 this macro gets one parameter push ebp mov ebp, esp sub esp, %1 %endmacro first parameter of the macro NASM preprocessor my_func: foo 12 my_func: push ebp mov ebp, esp sub esp, 12
Multiple-line macros (cont) • Multi-line macros are case-sensitive, unless we define them using the alternative directive %imacro. • If we need to pass a comma as part of a parameter to a multiline macro, we can do that by enclosing the entire parameter in braces. Example: %macro foo 2 %2: db %1 %endmacro foo 'a', letter_a foo 'ab', string_ab foo {13, 10}, crlf letter_a: db 'a' string_ab: db 'ab' crlf: db 13, 10 NASM preprocessor
Multiple-line macros (cont) • Multi-line macros can be overloaded by defining the same macro name several times with different amounts of parameters. (This time, no exception is made for macros with no parameters. ) • Reserved words can also be overloaded: Example: %macro push 2 push %1 push %2 %endmacro push ebx ; this line is not a macro call push eax, ecx ; but this one is a macro call Note: if define macro ‘push’ with one parameter, the original ‘push’ instruction would be overloaded.
Multiple-line macros – labels Defining a macro with an internal label: %macro retz 0 jnz %%skip ret %%skip: %endmacro In every ‘retz’ invocation, the preprocessor creates some unique label of the form: . . @2345. skip to substitute for the label %%skip, where the number 2345 changes with every macro call. If a label begins with the special prefix. . @, then it doesn’t interfere with the local label mechanism. label 1: . . @ 2345. skip : . local: ; a non-local label ; this is a macro label ; this is really label 1. local
Default Macro Parameters We supply a minimum and maximum number of parameters for a macro of this type; the minimum number of parameters are required in the macro call, and we provide defaults for the optional ones. %macro name min - max <default parameters list> Example: %macro foo 1 -3 eax, [ebx+2] • • could be called with between one and three parameters %1 would always be taken from the macro call (minimal number of parameters) %2, if not specified by the macro call, would default to eax %3, if not specified by the macro call, would default to [ebx+2] We may omit parameter defaults from the macro definition, in which case the parameter default is taken to be blank. This can be useful for macros which can take a variable number of parameters, since the %0 token allows us to determine how many parameters were really passed to the macro call.
Greedy Macro Parameters If invoke the macro with more parameters than it expects, all the spare parameters get lumped into the last defined one. %macro. Name num. Of. Params + The mark %num. Of. Params will be replaced with num. Of. Params’s parameter and whatever follows it. Example: %macro writefile 2+ jmp %%endstr %%str: db %2 %%endstr: mov dx, %%str mov cx, %%endstr - %%str mov bx, %1 mov ah, 0 x 40 int 0 x 21 %endmacro writefile [file. Handle], "hello, world", 13, 10
Macro Expansion Use –e option to get a source code with all your macros expanded. > nasm -e sample. s
Jump table • Jump table is – a graceful way to implement “switch - case” mechanism – used to select a function to be evoked • We will construct a array of the jump addresses. • For each number will jump to the corresponding entry in the jump table. switch ( letter ) { case 'A': upper ++; case ‘a': lower ++; default : total ++; }
Jump table - example main. c extern void jumper(int); int main (int argc , char* argv) { jumper (0); jumper (1); jumper (2); return 0; } jumper(i) should be implemented as follows: printf (“num = %d”, i); switch (i) { case ‘ 0': printf (“Got the number 0”); case ‘ 1': printf (“Got the number 1”); default : printf (“Out of bound”); } Output: num = 0 Got the number 0 num = 1 Got the number 1 num = 2 Out of bound
section. data jt: dd dd label_1 label_2 str 0: str 1: str 2: str 3: db db "Got the number 0", 10, 0 "Got the number 1", 10, 0 "Out of bound", 10, 0 "num = %d", 10, 0 global extern jumper printf push mov pusha mov push call add cmp jb cmp ja shl jmp ebp, esp section. text jumper: to be able to jump in a table of dwords Jump table - example ebx, dword [ebp+8] ebx str 3 printf ; print num esp, 8 ebx, 0 ; check if num is in bounds out_of ebx , 1 out_of ebx, 2 ; num = num * 4 dword [ebx + jt] ; jump according to address ; in table label_1: push call add jmp str 0 printf esp, 4 end label_2: push call add jmp str 1 printf esp, 4 end out_of: push call add jmp str 2 printf esp, 4 end: popa pop ret ebp printf (“num = %d”, i); switch (i) { case ‘ 0': printf (“Got the number 0”); case ‘ 1': printf (“Got the number 1”); default : printf (“Out of bound”); }
4 שאלה : בזכרון נתונים וכן , הבאה macro הגדרת נתונה %macro print pusha mov mov int popa %endmacro section . rodata File: dd MJ: db a) b) c) d) mov print mov print 3 eax, 4 ebx, %1 ecx, %2 edx, %3 0 x 80 ; write ; file descriptor ; address ; byte count 1 “Beat it”, 10, 0 : התוכנית של נכונה לא לפעולה יגרום במקרו הבאים מהשימושים איזה ebx, MJ 1, ebx, 9 1, MJ, 9 dword [File], MJ, 9 edx, 9 1, MJ, edx
4 שאלה : בזכרון נתונים וכן , הבאה macro הגדרת נתונה %macro print pusha mov mov int popa %endmacro section . rodata File: dd MJ: db a) b) c) d) mov print mov print 3 eax, 4 ebx, %1 ecx, %2 edx, %3 0 x 80 ; write ; file descriptor ; address ; byte count 1 “Beat it”, 10, 0 : התוכנית של נכונה לא לפעולה יגרום במקרו הבאים מהשימושים איזה ebx, MJ 1, ebx, 9 1, MJ, 9 dword [File], MJ, 9 edx, 9 1, MJ, edx
5 שאלה : הבא הקוד קטע את לממש עלינו int a, b, x; x = blah(a, &b) ? נכון זאת שיבצע הקוד קטע מהו a) push a c) push dword b push dword [a] call blah add esp, 8 mov [x], eax b) push dword [b] push dword a call blah add esp, 8 mov [x], eax d) push dword [b] push dword a call blah add esp, 8 pop dword [x]
5 שאלה : הבא הקוד קטע את לממש עלינו int a, b, x; x = blah(a, &b) ? נכון זאת שיבצע הקוד קטע מהו a) push a c) push dword b push dword [a] call blah add esp, 8 mov [x], eax b) push dword [b] push dword a call blah add esp, 8 mov [x], eax d) push dword [b] push dword a call blah add esp, 8 pop dword [x]
6 שאלה Gloat: shl ebx, 2 jmp [ebx+Tab] Tab: dd F 4 dd F 3 dd F 2 dd F 1: add ebx, 4 F 2: add ebx, 4 F 3: add ebx, 4 F 4: shr ebx, 2 ret ? (3 - ל 0 בין ebx )עבור ebx ברגיסטר Gloat מה תחזיר הפונקציה 2 כפול ebx ( ד ebx בחזקת 2 ( ג 2 בחזקת ebx ( ב 0 ( א
6 שאלה Gloat: shl ebx, 2 jmp [ebx+Tab] Tab: dd F 4 dd F 3 dd F 2 dd F 1: add ebx, 4 F 2: add ebx, 4 F 3: add ebx, 4 F 4: shr ebx, 2 ret ? (3 - ל 0 בין ebx )עבור ebx ברגיסטר Gloat מה תחזיר הפונקציה 2 כפול ebx ( ד ebx בחזקת 2 ( ג 2 בחזקת ebx ( ב 0 ( א

































