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/nasmdoc4.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 Source code myMacro . โข myMacro . โข myMacro . โข Expanded code . . . macro definition macro name macro body (statements) NASM preprocessor macro usage
Single-line macros โข %define โ defines single-line macro โข a macro is expanded only when it is called โข Example:%define ctrl 0x1F & %define param(a, b) ((a)+(a)*(b))mov byte [param(2,ebx)], ctrl 'D' expands to by NASM preprocessor mov byte [(2)+(2)*(ebx)], 0x1F & 'D' โข Example: โข %define a(x) 1+b(x)%define b(x) 2*x expands to mov ax,1+2*8 mov ax, a(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 โข 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. %define foo 1+ ebx A macro with no parameters prohibits the definition of the same name as a macroย withย parameters, and vice versa.
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 isTrue 1 %xdefine isTrue 1 %define isFalse isTrue %xdefine isFalse isTrue %define isTrue 0 %xdefine isTrue 0 val1: db isFalse ; val1 = ?val1: db isFalse ; val1=?%define isTrue 1 %xdefine isTrue 1 val2: db isFalse ; val2 = ?val2: db isFalse; val2=?
Single-line macros (cont) โข In the left case, when โisFalseโ macro uses %define, so 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 right case, each time that โisFalseโ is called, it expands to 1, as that is what the macro โisTrueโ expanded to at the time that โisFalseโ was defined. โข %define isTrue 1 %xdefine isTrue 1 %define isFalse isTrue %xdefine isFalse isTrue %define isTrue 0 %xdefine isTrue 0 val1: db isFalse ; val1 = 0 val1: db isFalse ; val1=1%define isTrue 1 %xdefine isTrue 1 val2: db isFalse ; val2 = 1val2: db isFalse; val2=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 โฆ %endmacromechanism โข Macro parameters would be referred to as %1, %2, %3 and so on Example: %macro foo 1 push ebp mov ebp, esp sub esp, %1 %endmacro my_func: foo 12 my_func: push ebp mov ebp, esp sub esp,12 this macro gets one parameter first parameter of the macro NASM preprocessor
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 multi-line macro, we can do that by enclosing the entire parameter in braces. โข Example: โข %macro foo 2 %2: db %1 %endmacro foo 'a', letter_a letter_a: db 'a' foo 'ab', string_ab string_ab: db 'ab' foo {13,10}, crlf 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. label1: ; a non-local label ..@ 2345.skip : ; this is a macro label .local: ; this is really label1.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. โข 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. %macro name min - max <default parameters list>
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 macroName numOfParams + The mark %numOfParams will be replaced with numOfParamsโ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, 0x40 int 0x21 %endmacro writefile [fileHandle],"hello, world",13,10
Use โe option to get a source code with all your macros expanded. > nasm -e sample.s Macro Expansion
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 Output: num = 0 Got the number 0 num = 1 Got the number 1 num = 2 Out of bound 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โ); }
Jump table - example section .data jt: dd label_1 dd label_2 str0: db "Got the number 0",10,0 str1: db "Got the number 1",10,0 str2: db "Out of bound",10,0 str3: db "num = %d",10,0 section .text global jumper extern printf jumper: push ebp mov ebp, esp pusha mov ebx, dword [ebp+8] push ebx push str3 call printf ; print num add esp, 8 cmp ebx,0 ; check if num is in bounds jb out_of cmp ebx , 1 ja out_of shl ebx,2 ; num = num * 4 jmp dword [ebx + jt] ; jump according to address ; in table label_1: push str0 call printf add esp, 4 jmp end label_2: push str1 call printf add esp, 4 jmp end out_of: push str2 call printf add esp, 4 jmp end end: popa pop ebp ret to be able to jump in a table of dwords 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โ); }
ืฉืืืืช ืืืจื ืืืืื
ืฉืืื 1 ื ืชืื ืืช ืืืืืจืืช ืืืืืช: x: dw 1y: db 2z: db 3 ืืฉ ืืืืคืื ืืช x,y,z ื-2 ืืืืฆืขืืช ืคืงืืื ืืืช. ื ืืชื ืืื ืื ืฉืืื overflow
ืฉืืื 1 ื ืชืื ืืช ืืืืืจืืช ืืืืืช: x: dw 1y: db 2z: db 3 ืืฉ ืืืืคืื ืืช x,y,z ื-2 ืืืืฆืขืืช ืคืงืืื ืืืช. ื ืืชื ืืื ืื ืฉืืื overflow ืชืฉืืื: ื ืืคืื ืืช ืื ืืืืื ื- 2: shl dword [x], 1
ืฉืืื 2 ืขืืื ื ืืืืฉ ืงืจืืื ืืคืื ืงืฆืื ืืื ืืจืืืื ืืื, ืฉืืชืืืชื ื ืืฆืืช ืืจืืืกืืจ eax. ืืฉ ืืกืื ืืช ืืงืื ืฉืื ืืืฆืข ืืืช ื ืืื. push next_apush eaxretnext_a: push eaxpush eaxret push next_ajmpeaxnext_a: call eax
ืฉืืื 2 ืขืืื ื ืืืืฉ ืงืจืืื ืืคืื ืงืฆืื ืืื ืืจืืืื ืืื, ืฉืืชืืืชื ื ืืฆืืช ืืจืืืกืืจ eax. ืืฉ ืืกืื ืืช ืืงืื ืฉืื ืืืฆืข ืืืช ื ืืื. push next_apush eaxretnext_a: push eaxpush eaxret push next_ajmpeaxnext_a: call eax
ืฉืืื 3 ืืจืืืกืืจ eax ื ืืฆื ืืขืจื -1. ืืฉ ืืจืฉืื 5 ืคืงืืืืช ืฉืื ืืช ืฉืื ืืืช ืืื ืชืืจืื ืืื ืฉืืจืืืกืืจ eax ืืืื ืืขืจื 1.
ืฉืืื 3 ืืจืืืกืืจ eax ื ืืฆื ืืขืจื -1. ืืฉ ืืจืฉืื 5 ืคืงืืืืช ืฉืื ืืช ืฉืื ืืืช ืืื ืชืืจืื ืืื ืฉืืจืืืกืืจ eax ืืืื ืืขืจื 1. ืชืฉืืื moveax, 1 add eax, 2 negeax shreax, 31 and eax, 1
ืฉืืื 4 ื ืชืื ื ืืืืจืช macro ืืืื, ืืื ื ืชืื ืื ืืืืจืื: %macro print 3pushamoveax, 4 ; writemovebx, %1 ; file descriptormovecx, %2 ; addressmovedx, %3 ; byte countint 0x80popa%endmacro section .rodataFile:dd 1MJ: db โBeat itโ, 10, 0 ืืืื ืืืฉืืืืฉืื ืืืืื ืืืงืจื ืืืจืื ืืคืขืืื ืื ื ืืื ื ืฉื ืืชืืื ืืช: movebx, MJprint 1, ebx, 9 print 1, MJ, 9 print dword [File], MJ, 9 movedx, 9print 1, MJ, edx
ืฉืืื 4 ื ืชืื ื ืืืืจืช macro ืืืื, ืืื ื ืชืื ืื ืืืืจืื: %macro print 3pushamoveax, 4 ; writemovebx, %1 ; file descriptormovecx, %2 ; addressmovedx, %3 ; byte countint 0x80popa%endmacro section .rodataFile:dd 1MJ: db โBeat itโ, 10, 0 ืืืื ืืืฉืืืืฉืื ืืืืื ืืืงืจื ืืืจืื ืืคืขืืื ืื ื ืืื ื ืฉื ืืชืืื ืืช: movebx, MJprint 1, ebx, 9 print 1, MJ, 9 print dword [File], MJ, 9 movedx, 9print 1, MJ, edx
ืฉืืื 5 ืขืืื ื ืืืืฉ ืืช ืงืืข ืืงืื ืืื: int a, b, x; x = blah(a,&b) ืืื ืงืืข ืืงืื ืฉืืืฆืข ืืืช ื ืืื ? a) push a c) push dword b push b push dword [a] call blah call blah add esp, 8 add esp, 8 mov [x], eax mov [x], eax b) push dword [b] d) push dword [b] push dword a push dword a call blah call blah add esp, 8 add esp, 8 mov [x], eax pop dword [x]
ืฉืืื 5 ืขืืื ื ืืืืฉ ืืช ืงืืข ืืงืื ืืื: int a, b, x; x = blah(a,&b) ืืื ืงืืข ืืงืื ืฉืืืฆืข ืืืช ื ืืื ? a) push a c) push dword b push b push dword [a] call blah call blah add esp, 8 add esp, 8 mov [x], eaxmov [x], eax b) push dword [b] d) push dword [b] push dword a push dword a call blah call blah add esp, 8 add esp, 8 mov [x], eax pop dword [x]
ืฉืืื 6 Gloat: shl ebx, 2 jmp [ebx+Tab] Tab: dd F4 dd F3 dd F2 dd F1 F1: add ebx, 4 F2: add ebx, 4 F3: add ebx, 4 F4: shr ebx, 2 ret ืื ืชืืืืจ ืืคืื ืงืฆืื Gloat ืืจืืืกืืจ ebx (ืขืืืจ ebx ืืื 0 ื- 3) ? ื) 0 ื) ebx ืืืืงืช 2 ื) 2 ืืืืงืช ebx ื) ebx ืืคืื 2
ืฉืืื 6 Gloat: shl ebx, 2 jmp [ebx+Tab] Tab: dd F4 dd F3 dd F2 dd F1 F1: add ebx, 4 F2: add ebx, 4 F3: add ebx, 4 F4: shr ebx, 2 ret ืื ืชืืืืจ ืืคืื ืงืฆืื Gloat ืืจืืืกืืจ ebx (ืขืืืจ ebx ืืื 0 ื- 3) ? ื) 0 ื) ebx ืืืืงืช 2 ื) 2 ืืืืงืช ebxื) ebx ืืคืื 2
ืฉืืื 7 โข ืืจืฆืื ื ื ืืืชืื ืงืื ืืฉืืืืฉ ืจื-ืคืขืื, ืฉืืืคืื ืืช ืขืจื eax ืคื 3. ืืืฆืขืืช 2 ืืคืฉืจืืืืช: ืฉืืืืฉ ืืืงืจื triple ืื ืงืจืืื ืืคืื ืงืฆืื Triple: โข %macro triple 0 mov ebx, eax add eax, eax add eax, ebx %endmacro โข Triple: mov ebx, eax add eax, eax add eax, ebx ret ื) ืืืื ืจืืฆื ื-2 ืืืคืฉืจืืืืช ืืืชื ืืื ืืืฆืืข. ื) ืืฉืืืืฉ ื- macro ืืืืจ ืืืชืจ, ืืื ืืืจืฉ ืืืชืจ ืืืืจืื ืืงืื. ื) ืืฉืืืืฉ ืืคืื ืงืฆืื ืืืืจ ืืืชืจ, ืืื ืืืจืฉ ืืืชืจ ืืืืจืื ืืงืื. ื) ืืคืื ืงืฆืื Triple ืื ืืืืื ืืขืืื, ืื ืืื ืื ืืืฆืืื ืืฉืชื ืื ืืืืืกื ืืช
ืฉืืื 7 โข ืืจืฆืื ื ื ืืืชืื ืงืื ืืฉืืืืฉ ืจื-ืคืขืื, ืฉืืืคืื ืืช ืขืจื eax ืคื 3. ืืืฆืขืืช 2 ืืคืฉืจืืืืช: ืฉืืืืฉ ืืืงืจื triple ืื ืงืจืืื ืืคืื ืงืฆืื Triple: โข %macro triple 0 mov ebx, eax add eax, eax add eax, ebx %endmacro โข Triple: mov ebx, eax add eax, eax add eax, ebx ret ื) ืืืื ืจืืฆื ื-2 ืืืคืฉืจืืืืช ืืืชื ืืื ืืืฆืืข. ื) ืืฉืืืืฉ ื- macro ืืืืจ ืืืชืจ, ืืื ืืืจืฉ ืืืชืจ ืืืืจืื ืืงืื. ื) ืืฉืืืืฉ ืืคืื ืงืฆืื ืืืืจ ืืืชืจ, ืืื ืืืจืฉ ืืืชืจ ืืืืจืื ืืงืื. ื) ืืคืื ืงืฆืื Triple ืื ืืืืื ืืขืืื, ืื ืืื ืื ืืืฆืืื ืืฉืชื ืื ืืืืืกื ืืช