Chapter08 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

1

Macro & Function


CHAPTER 08
2

• Single-line macro
Macros • Multi-line macro
Macros
 A predefined set of instructions that can easily be inserted wherever
needed.

 Once defined, the macro can be used as many times as necessary.

 Macro definitions should be placed in the source file before the data
and code sections and being used in the text (code) section.

 There are 2 types of macros:

 Single-line

 Multi-line
Macros
Source code Compiled code

a a’s machine code


macro a

a a’s machine code


Single-line Macros
 Defined using the %define directive

%define mulby4(x) shl x,2

Use the macro to multiply the contents of eax register by 4:

...

mulby4(eax)
Multi-line Macros
 The general format:
%macro <name> <number of arguments>
...
...
%endmacro
 The arguments can be referenced within the macro by %<number>,
with %1 being the first argument, and %2 the second argument…
%macro abs 1
cmp %1, 0
jge %%done
neg %1
%%done: ; label must be prefixed with %%
...
%endmacro
Multi-line Macros
%macro writeStr 2 section .data
mov edx,%2 msg1 db 'Welcome to the world of, ',10
mov ecx,%1 len1 equ $-msg1
mov ebx,1 msg2 db 'linux assembly programming',10
mov eax,4 len2 equ $-msg2
int 0x80 section .text
%endmacro global _start
_start:
writeStr msg1,len1
writeStr msg2,len2
_exit:
mov eax,1
int 0x80
Multi-line Macros
%macro writeStr 2 section .data
mov edx,%2 msg db ‘Assembly programming',10
mov ecx,%1 mlen equ $-msg
mov ebx,1 KEY equ 0x88
mov eax,4 section .text
int 0x80 global _start
%endmacro _start:
%macro encrypt 3 encrypt msg, mlen, KEY
mov esi,%1 writeStr msg,mlen
mov ecx,%2-1 encrypt msg, mlen, KEY
%%loop_label: writeStr msg,mlen
xor byte[esi],%3
inc esi _exit:
loop %%loop_label mov eax,1
%endmacro int 0x80
9

• Caller vs Callee
Functions • Calling conventions
Declaration
 Functions are located in the _start:
push msg1
code (text) segment. push len1
call writeStr
 The general format:
_exit:
global <procName>
mov eax,1
<procName>: int 0x80
... writeStr:
ret push ebp
mov ebp,esp
The function is invoked by placing mov edx,[ebp+8]
call <procName> in the calling program mov ecx,[ebp+12]
mov ebx,1
mov eax,4
int 0x80
leave
ret
Stack Frame
 The items on the stack as part of a function call

 Possible items include:

1. Return address (required)

2. Passed arguments (if any)


[ebp-8] Low mem esp
3. Local variables (if any)
local var 2
 The stack-based arguments are accessed [ebp-4] local var 1
relative to rbp|ebp, each item pushed ebp
is a qword/dword which return address [ebp+8]
uses 8/4 bytes. arg 1 [ebp+12]
High mem arg 2
Caller rules

 Pass parameters to the subroutine, by pushing them onto the stack


The parameters should be pushed in the inverted order (i.e. last
parameter first). Since the stack grows down, the first parameter will
be stored at the lowest address
 Call the subroutine, by using the call instruction.
This instruction places the return address on
top of the parameters on the stack, and
Low mem
branches to the subroutine code.
return address
This invokes the subroutine, which should follow arg 1
the callee rules below. arg 2
High mem
Caller rules

0x8bff9130
call 0x8bff9130
0x8bff8430 func(arg1, arg2)
0x8bff8435
Stack area

0x8bff8435
arg 1
arg 2
Callee rules
1. Push the value of EBP onto the stack, then copy ESP into EBP.
This action maintains the base pointer, EBP which is used by
convention as a point of reference for finding parameters and local
variables on the stack.
2. Set spaces for local variables by subtracting ESP by the size of all
local variables.
esp Stack area
3. Proceed subroutine body code.
Local var 2
4. Before return:
esp Local var 1
Leave return value in EAX, leave
ebp
Restore EBP by executing mov esp, ebp
0x8bff8435
pop ebp
return by ret arg 1
arg 2
Example
section .data writeStr:
msg1 db 'Welcome to the world of, ',10 push ebp
len1 equ $-msg1
mov ebp,esp
msg2 db 'linux assembly programming',10
mov edx,[ebp+8]; get len1 from stack
len2 equ $-msg2
section .text mov ecx,[ebp+12]; get msg1 from stack
global _start mov ebx,1
_start: mov eax,4
push msg1 int 0x80
push len1
leave
call writeStr
ret
push msg2
push len2 esp
ebp
call writeStr
_exit: return addr return addr
mov eax,1 len1 len1
int 0x80 msg1 msg1

High mem addr High mem addr


Linking multiple object files

 Commonly used functions may Display.asm

be defined in separate file like libraries. section .text


global write_string
 Functions that may be called outside
write_string:
needed to be declared as global.
push ebp
 Library asm will be compiled to object mov ebp,esp
code but it’s not executable. mov esi,[ebp+8]

nasm –g –f elf display.asm → display.o mov ecx,[ebp+12]


write_str esi,ecx
 There might be multiple library functions
leave
be implemented this way lib1.o, lib2.o, …
ret
Linking multiple object files

 To use the library functions in caller program, prog.asm

they needed to be declared with %macro sys_exit 0

extern keyword before the .text section moveax,1


int 0x80
 The caller program then be compiled to
%endmacro
object code (e.g. prog.asm → prog.o) section .bss

 Executable file will be created by linking with bMem resb 32


extern write_hex
others library object codes:
extern space
ld –g –o <executable file> <list of object files> extern newline

 List of object files are separate by space. section .text


global _start
_start:
Calling assembly from C program
1. Define assembly code that will be 3. C program (test.c)
invoked from C (mem.asm): #include <stdio.h>
section .data extern display_memory();
bMem db ‘Text in Assembly’ int main()
extern write_hex ; defined in display.asm {
extern space ; which already been display_memory();
extern newline ; compiled to display.o }
section .text
global display_memory
display_memory: 4. Compile c program & link with compiled

... asm code:

... gcc test.c mem.o display.o –o test.out

ret
2. Compile:
nasm –f elf mem.asm
Calling assembly from C program
 Passing parameters  Assembly code

#include <stdio.h> section .text

extern write_string(); global write_string

int main() write_string:

{ push ebp
mov ebp,esp
char *s=“String is allocated in C\n”;
mov esi,[ebp+8]
int k = 0;
mov ecx,[ebp+12]
while (s[k++]!=NULL);
write_str esi,ecx
write_string(s,k);
leave
return 0;
ret
}

You might also like