Chapter08 PDF
Chapter08 PDF
Chapter08 PDF
• Single-line macro
Macros • Multi-line macro
Macros
A predefined set of instructions that can easily be inserted wherever
needed.
Macro definitions should be placed in the source file before the data
and code sections and being used in the text (code) section.
Single-line
Multi-line
Macros
Source code Compiled code
...
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
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
ret
2. Compile:
nasm –f elf mem.asm
Calling assembly from C program
Passing parameters Assembly code
{ 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
}