practical session 6

33
Practical Session 6

Upload: venus

Post on 24-Feb-2016

43 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Practical Session 6

Practical Session 6

Page 2: 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.

Page 3: Practical Session 6

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 codemyMacro

.myMacro

.myMacro

.

Expanded code

.

.

.

NASM preprocessor

macro definition macro name

macro body (statements)

macro usage

Page 4: Practical Session 6

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

Page 5: Practical Session 6

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.

Page 6: Practical Session 6

Single-line macros (cont)

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=?

โ€ข %define - a macro resolved at the time that it is called (used)

โ€ข %xdefine - a macro resolved at the time that it is defined

Page 7: Practical Session 6

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 = 1 val2: db isFalse; val2=1

Page 8: Practical Session 6

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

Page 9: Practical Session 6

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 push ebp mov ebp, esp sub esp, %1 %endmacro

my_func: foo 12 my_func: push ebp

mov ebp, esp sub esp,12

first parameter of the macro

this macro gets one parameter

NASM preprocessor

Page 10: Practical Session 6

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

Page 11: Practical Session 6

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 %1push %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.

Page 12: Practical Session 6

Multiple-line macros โ€“ labelsDefining 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: [email protected] 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

Page 13: Practical Session 6

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>

Page 14: Practical Session 6

Greedy Macro ParametersIf 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

Page 15: Practical Session 6

Macro ExpansionUse โ€“e option to get a source code with all your macros expanded.

> nasm -e sample.s

Page 16: Practical Session 6

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 ++;}

Page 17: Practical Session 6

main.cextern void jumper(int);int main (int argc , char* argv){

jumper (0);jumper (1);jumper (2);return 0;

}

Jump table - example

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 = 0Got the number 0num = 1Got the number 1num = 2Out of bound

Page 18: Practical Session 6

section .datajt: dd label_1

dd label_2

str0: db "Got the number 0",10,0str1: db "Got the number 1",10,0str2: db "Out of bound",10,0str3: db "num = %d",10,0

section .textglobal jumperextern printf

jumper:push ebpmov ebp, esppushamov ebx, dword [ebp+8]push ebxpush str3call printf ; print numadd esp, 8cmp ebx,0 ; check if num is in boundsjb out_ofcmp ebx , 1ja out_ofshl ebx,2 ; num = num * 4jmp dword [ebx + jt] ; jump according to address

; in table

label_1: push str0call printfadd esp, 4jmp end

label_2: push str1call printfadd esp, 4jmp end

out_of: push str2call printfadd esp, 4jmp end

end: popapop ebpret

Jump table - example

to b

e ab

le to

jum

p in

a ta

ble

of d

wor

ds

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โ€);}

Page 19: Practical Session 6

ืœืžื‘ื—ืŸ ื—ื–ืจื” ืฉืืœื•ืช

Page 20: Practical Session 6

1ืฉืืœื” : ื”ื‘ืื•ืช ื”ื”ื’ื“ืจื•ืช ื ืชื•ื ื•ืช

x: dw 1y: db 2z: db 3

ืืช ืœื”ื›ืคื™ืœ -x,y,zื™ืฉ . 2ื‘ ืื—ืช ืคืงื•ื“ื” ื‘ืืžืฆืขื•ืชืฉืื™ืŸ ืœื”ื ื™ื— overflowื ื™ืชืŸ

Page 21: Practical Session 6

1ืฉืืœื” : ื”ื‘ืื•ืช ื”ื”ื’ื“ืจื•ืช ื ืชื•ื ื•ืช

x: dw 1y: db 2z: db 3

ืืช ืœื”ื›ืคื™ืœ -x,y,zื™ืฉ . 2ื‘ ืื—ืช ืคืงื•ื“ื” ื‘ืืžืฆืขื•ืชืฉืื™ืŸ ืœื”ื ื™ื— overflowื ื™ืชืŸ

- ืชืฉื•ื‘ื”: ื‘ ื”ืžื™ืœื” ื›ืœ ืืช :2ื ื›ืคื•ืœ

shl dword [x], 1

Page 22: Practical Session 6

2ืฉืืœื” , ืืจื’ื•ืžื ื˜ื™ื ืœืœื ืœืคื•ื ืงืฆื™ื” ืงืจื™ืื” ืœืžืžืฉ ืขืœื™ื ื•

ื‘ืจื’ื™ืกื˜ืจ ื ืžืฆืืช ืืช. eaxืฉื›ืชื•ื‘ืชื” ืœืกืžืŸ ื™ืฉืฉ .ืœืื”ืงื•ื“ ื ื›ื•ืŸ ื–ืืช ื™ื‘ืฆืข

a) push next_apush eaxretnext_a:

b) push eaxpush eaxret

c) push next_ajmp eaxnext_a:

d) call eax

Page 23: Practical Session 6

2ืฉืืœื” , ืืจื’ื•ืžื ื˜ื™ื ืœืœื ืœืคื•ื ืงืฆื™ื” ืงืจื™ืื” ืœืžืžืฉ ืขืœื™ื ื•

ื‘ืจื’ื™ืกื˜ืจ ื ืžืฆืืช ืืช. eaxืฉื›ืชื•ื‘ืชื” ืœืกืžืŸ ื™ืฉืฉ .ืœืื”ืงื•ื“ ื ื›ื•ืŸ ื–ืืช ื™ื‘ืฆืข

a) push next_apush eaxretnext_a:

b) push eaxpush eaxret

c) push next_ajmp eaxnext_a:

d) call eax

Page 24: Practical Session 6

3ืฉืืœื” ื”ืขืจืš -eaxื‘ืจื’ื™ืกื˜ืจ ืœืจืฉื•ื. 1ื ืžืฆื 5ื™ืฉ

ืœื›ืš ืฉื•ื ื•ืชืคืงื•ื“ื•ืช ืชื’ืจื•ื ืžื”ืŸ ืื—ืช ืฉื›ืœื”ืขืจืš eaxืฉื‘ืจื’ื™ืกื˜ืจ .1ื™ื”ื™ื”

Page 25: Practical Session 6

3ืฉืืœื” ื”ืขืจืš -eaxื‘ืจื’ื™ืกื˜ืจ ืœืจืฉื•ื. 1ื ืžืฆื 5ื™ืฉ

ืœื›ืš ืฉื•ื ื•ืชืคืงื•ื“ื•ืช ืชื’ืจื•ื ืžื”ืŸ ืื—ืช ืฉื›ืœื”ืขืจืš eaxืฉื‘ืจื’ื™ืกื˜ืจ .1ื™ื”ื™ื”

ืชืฉื•ื‘ื”

mov eax, 1add eax, 2neg eaxshr eax, 31and eax, 1

Page 26: Practical Session 6

4ืฉืืœื” ื”ื’ื“ืจืช :macroื ืชื•ื ื” , ื‘ื–ื›ืจื•ืŸ ื ืชื•ื ื™ื ื•ื›ืŸ ื”ื‘ืื”

%macro print 3pushamov eax, 4 ; writemov ebx, %1 ; file descriptormov ecx, %2 ; addressmov edx, %3 ; byte countint 0x80popa

%endmacrosection .rodataFile: dd 1MJ: db โ€œBeat itโ€, 10, 0

: ื”ืชื•ื›ื ื™ืช ืฉืœ ื ื›ื•ื ื” ืœื ืœืคืขื•ืœื” ื™ื’ืจื•ื ื‘ืžืงืจื• ื”ื‘ืื™ื ืžื”ืฉื™ืžื•ืฉื™ื ืื™ื–ื”a) mov ebx, MJ

print 1, ebx, 9b) print 1, MJ, 9c) print dword [File], MJ, 9d) mov edx, 9

print 1, MJ, edx

Page 27: Practical Session 6

4ืฉืืœื” ื”ื’ื“ืจืช :macroื ืชื•ื ื” , ื‘ื–ื›ืจื•ืŸ ื ืชื•ื ื™ื ื•ื›ืŸ ื”ื‘ืื”

%macro print 3pushamov eax, 4 ; writemov ebx, %1 ; file descriptormov ecx, %2 ; addressmov edx, %3 ; byte countint 0x80popa

%endmacrosection .rodataFile: dd 1MJ: db โ€œBeat itโ€, 10, 0

: ื”ืชื•ื›ื ื™ืช ืฉืœ ื ื›ื•ื ื” ืœื ืœืคืขื•ืœื” ื™ื’ืจื•ื ื‘ืžืงืจื• ื”ื‘ืื™ื ืžื”ืฉื™ืžื•ืฉื™ื ืื™ื–ื”a) mov ebx, MJ

print 1, ebx, 9b) print 1, MJ, 9c) print dword [File], MJ, 9d) mov edx, 9

print 1, MJ, edx

Page 28: Practical Session 6

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]

Page 29: Practical Session 6

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]

Page 30: Practical Session 6

6ืฉืืœื” Gloat: shl ebx, 2

jmp [ebx+Tab] Tab: dd F4

dd F3dd F2dd F1

F1: add ebx, 4F2: add ebx, 4F3: add ebx, 4F4: shr ebx, 2

ret

ื”ืคื•ื ืงืฆื™ื” ืชื—ื–ื™ืจ - 0ื‘ื™ืŸ ebxืขื‘ื•ืจ ) ebxื‘ืจื’ื™ืกื˜ืจ Gloatืžื” ( ?3ืœ

( 0ื( ( 2ื‘ื—ื–ืงืช ebxื‘ ( ebxื‘ื—ื–ืงืช 2ื’ 2ื›ืคื•ืœ ebxื“

Page 31: Practical Session 6

6ืฉืืœื” Gloat: shl ebx, 2

jmp [ebx+Tab] Tab: dd F4

dd F3dd F2dd F1

F1: add ebx, 4F2: add ebx, 4F3: add ebx, 4F4: shr ebx, 2

ret

ื”ืคื•ื ืงืฆื™ื” ืชื—ื–ื™ืจ - 0ื‘ื™ืŸ ebxืขื‘ื•ืจ ) ebxื‘ืจื’ื™ืกื˜ืจ Gloatืžื” ( ?3ืœ

( 0ื( ( 2ื‘ื—ื–ืงืช ebxื‘ 2ื›ืคื•ืœ ebxื“( ebxื‘ื—ื–ืงืช 2ื’

Page 32: Practical Session 6

7ืฉืืœื” โ€ข , - ืขืจืš ืืช ืฉืžื›ืคื™ืœ ืคืขืžื™ ืจื‘ ืœืฉื™ืžื•ืฉ ืงื•ื“ ืœื›ืชื•ื‘ : 2ืžื•ืฆืขื•ืช. 3ืคื™ eaxื‘ืจืฆื•ื ื ื• ืืคืฉืจื•ื™ื•ืช

ื‘ืžืงืจื• ืœืคื•ื ืงืฆื™ื” tripleืฉื™ืžื•ืฉ ืงืจื™ืื” : Tripleืื•โ€ข %macro triple 0

mov ebx, eaxadd eax, eaxadd eax, ebx

%endmacroโ€ข Triple: mov ebx, eax

add eax, eax add eax, ebx

ret- ืœ( ืจื™ืฆื” ื‘ื–ืžืŸ .2ื ื‘ื™ืฆื•ืข ื–ืžืŸ ืื•ืชื• ื”ืืคืฉืจื•ื™ื•ืช

- ื‘( ื”ืฉื™ืžื•ืฉ .macroื‘ , ืœืงื•ื“ ื–ื™ื›ืจื•ืŸ ื™ื•ืชืจ ื“ื•ืจืฉ ืื‘ืœ ื™ื•ืชืจ ืžื”ื™ืจ. , ืœืงื•ื“( ื–ื™ื›ืจื•ืŸ ื™ื•ืชืจ ื“ื•ืจืฉ ืื‘ืœ ื™ื•ืชืจ ืžื”ื™ืจ ื‘ืคื•ื ืงืฆื™ื” ื”ืฉื™ืžื•ืฉ ื’

ื”ืคื•ื ืงืฆื™ื”( , Tripleื“ ืžื”ืžื—ืกื ื™ืช ืžืฉืชื ื™ื ืžื•ืฆื™ืื” ืœื ื”ื™ื ื›ื™ ืœืขื‘ื•ื“ ื™ื›ื•ืœื” ืœื

Page 33: Practical Session 6

7ืฉืืœื” โ€ข , - ืขืจืš ืืช ืฉืžื›ืคื™ืœ ืคืขืžื™ ืจื‘ ืœืฉื™ืžื•ืฉ ืงื•ื“ ืœื›ืชื•ื‘ : 2ืžื•ืฆืขื•ืช. 3ืคื™ eaxื‘ืจืฆื•ื ื ื• ืืคืฉืจื•ื™ื•ืช

ื‘ืžืงืจื• ืœืคื•ื ืงืฆื™ื” tripleืฉื™ืžื•ืฉ ืงืจื™ืื” : Tripleืื•โ€ข %macro triple 0

mov ebx, eaxadd eax, eaxadd eax, ebx

%endmacroโ€ข Triple: mov ebx, eax

add eax, eax add eax, ebx

ret- ืœ( ืจื™ืฆื” ื‘ื–ืžืŸ .2ื ื‘ื™ืฆื•ืข ื–ืžืŸ ืื•ืชื• ื”ืืคืฉืจื•ื™ื•ืช

- ื‘( ื”ืฉื™ืžื•ืฉ .macroื‘ , ืœืงื•ื“ ื–ื™ื›ืจื•ืŸ ื™ื•ืชืจ ื“ื•ืจืฉ ืื‘ืœ ื™ื•ืชืจ ืžื”ื™ืจ. , ืœืงื•ื“( ื–ื™ื›ืจื•ืŸ ื™ื•ืชืจ ื“ื•ืจืฉ ืื‘ืœ ื™ื•ืชืจ ืžื”ื™ืจ ื‘ืคื•ื ืงืฆื™ื” ื”ืฉื™ืžื•ืฉ ื’

ื”ืคื•ื ืงืฆื™ื”( , Tripleื“ ืžื”ืžื—ืกื ื™ืช ืžืฉืชื ื™ื ืžื•ืฆื™ืื” ืœื ื”ื™ื ื›ื™ ืœืขื‘ื•ื“ ื™ื›ื•ืœื” ืœื