02/03/2019
Chapter 8
MACRO
FUNCTION
Macro
Definition : macro is a predefined set of instructions that
can easily be inserted wherever needed
After defined, macro can be used as many times as
necessary
Macro must be defined before of using
Macro can be used in text section
There are 2 types of macros : single-line macro and multi-
line macro
1
02/03/2019
Single – line macro
Single-line macros are defined using the %define
directive.
Example : %define mulby4(x) shl x, 2
Use the macro by entering : mulby4 (rax)
Explain : in the source, which will multiply the
contents to the rax register by 4 (via shifting two
bits).
Multi-Line Macros
Multi-line macros can include a varying number of
lines (including one). The multi-line macros are
more useful and the following sections will focus
primarily on multi-line macros.
Macro Definition : before using
Syntax :
%macro <name> <number of arguments>
; [body of macro]
%endmacro
The arguments can be referenced within the macro by
%<number>, with %1 being the first argument, and
%2 the second argument, and so forth.
2
02/03/2019
In order to use labels, the labels within the macro
must be prefixing the label name with a %%.
This will ensure that calling the same macro
multiple times will use a different label each time.
For example, a macro definition for the absolute
value function would be as follows:
%macro abs 1
cmp %1, 0
jge %%done
neg %1
%%done:
%endmacro
Using a Macro
Example : given declaration as follows
qVar dq 4
Invoke (call) abs macro (twice)
mov eax, -3
abs eax
abs qword [qVar]
The list file will display the code as follows (for
the first invocation):
3
02/03/2019
27 00000000 B8FDFFFFFF mov eax, -3
28 abs eax
29 00000005 3D00000000 <1> cmp %1, 0
30 0000000A 7D02 <1> jge %%done
31 0000000C F7D8 <1> neg %1
32 <1> %%done:
The macro will be copied from the definition into the
code, with the appropriate arguments replaced in the body
of the macro, each time it is used. The <1> indicates
code copied from a macro definition. In both cases, the
%1 argument was replaced with the given argument; eax
in this example.
Macro Example
; Example Program to demonstrate a simple macro
;****************************************
; Define the macro
; called with three arguments:
; aver <lst>, <len>, <ave>
%macro aver 3
mov eax, 0
mov ecx, dword [%2] ; length
mov r12, 0
lea rbx, [%1]
4
02/03/2019
%%sumLoop:
add eax, dword [rbx+r12*4] ; get list[n]
inc r12
loop %%sumLoop
cdq
idiv dword [%2]
mov dword [%3], eax
%endmacro
;***************************************;
Data declarations
section .data
; -----
; Define constants
EXIT_SUCCESS equ 0 ; success code
SYS_exit equ 60 ; code for terminate
; Define Data.
section .data
list1 dd 4, 5, 2, -3, 1
len1 dd 5
ave1 dd 0
5
02/03/2019
list2 dd 2, 6, 3, -2, 1, 8, 19
len2 dd 7
ave2 dd 0
;***************************************section
.text
global _start
_start:
; Use the macro in the program
aver list1, len1, ave1 ; 1st, data set 1
aver list2, len2, ave2
last:
mov rax, SYS_exit ; exit
mov rdi, EXIT_SUCCESS ; success
syscall
Functions
Functions and procedures (i.e., void functions),
help break-up a program into smaller parts
making it easier to code, debug, and maintain.
Function calls involve two main actions:
Linkage : Since the function can be called from
multiple different places in the code, the function must
be able to return to the correct place in which it was
originally called.
Argument Transmission : The function must be able to
access parameters to operate on or to return
results (i.e., access call-by-reference parameters).
6
02/03/2019
Function Declaration
A function must be written before it can be used.
Functions are located in the code segment. The
general format is:
global <procName>
<procName>:
; function body
ret
A function may be defined only once.
Functions cannot be
A function definition should be started and ended
before the next function’s definition can be started.
Linkage
The linkage is about getting to and returning from
a function call correctly. There are two instructions
that handle the linkage, call <funcName> and ret
instructions.
The call transfers control to the named function,
and ret returns control back to the calling routine.
The call works
Push RIP
Jump to label
Ret instruction
POP RIP
Jump to address
7
02/03/2019
The function calling or linkage instruction is
summarized as follows:
Argument Transmission
Argument transmission refers to sending information
(variables, etc.) to a function and obtaining a result as
appropriate for the specific function.
Transmitting values to a function is referred to as call-
byvalue.
Transmitting addresses to a function is referred to as call-
by-reference.
There are various ways to pass arguments to and/or from a
function
Placing values in register
Easiest, but has limitations (i.e., the number of registers).
Used for first six integer arguments.
Used for system calls.
8
02/03/2019
Globally defined variables
Generally poor practice, potentially confusing, and will
not work in many cases.
Occasionally useful in limited circumstances.
Putting values and/or addresses on stack
No specific limit to count of arguments that can be
passed.
Incurs higher run-time overhead.
In general, the calling routine is referred to as the
caller and the routine being called is referred to as
the callee.
Parameter Passing
As noted, a combination of registers and the stack is used
to pass parameters to and/or from a [Link] first six
integer arguments are passed in registers as follows:
The seventh and any additional arguments are passed on
the stack.
9
02/03/2019
when the function is completed, the calling routine is
responsible for clearing the arguments from the stack
Instead of doing a series of pop instructions, the
stack pointer, rsp, is adjusted as necessary to clear the
arguments off the stack.
Since each argument is 8 bytes, the adjustment would be
adding [(number of arguments) * 8] to the rsp
For value returning functions, the result is placed in the
A register based on the size of the value being returned.
Specifically, the values are returned as follows:
The rax register may be
used in the function as
needed as long as the
return value is set
appropriately before
returning.
10
02/03/2019
Register Usage
some registers are expected to be preserved across a
function call. That means that if a value is placed in a
preserved register or saved register and the function must
use that register, the original value must be preserved by
placing it on the stack, altered as needed, and then
restored to its original value before returning to the
calling routine
The temporary registers (r10 and r11) and the argument
registers (rdi, rsi, rdx, rcx, r8, and r9) are not
preserved across a function call This means that any of
these registers may be used in the function without the
need to preserve the original value.
None of the floating-point registers are preserved across
a function call
11
02/03/2019
Call Frame
The items on the stack as part of a function call
are referred to as a call frame (also referred to as
an activation record or stack frame).
The possible items in the call frame include:
Return address (required).
Preserved registers (if any).
Passed arguments (if any).
Stack dynamic local variables (if any).
For example, assuming a function call has eight (8)
arguments and assuming the function uses rbx, r12, and
r13 registers (and thus must be pushed), the call frame
would be as follows:
12
02/03/2019
Red Zone
In the Linux standard calling convention, the first 128-
bytes after the stack pointer, rsp, are reserved. For
example, extending the previous example, the call frame
would be as follows:
Example, Statistical Function 1 (leaf)
Example will demonstrate calling a simple void
function to find the sum and average of an array
of numbers
The High-Level Language (HLL) call for C/C++
is as follows:
stats1(arr, len, sum, ave);
The array, arr, is call-by-reference and the length,
len, is call-by-value. The arguments for sum and
ave are both call-by-reference (since there are no
values as yet)
13
02/03/2019
Caller
There are 4 arguments, and all arguments are passed in
registers in accordance with the standard calling
convention. The assembly language code in the calling
routine for the call to the stats function would be as
follows:
; stats1(arr, len, sum, ave);
mov rcx, ave ; 4th arg, addr of ave
mov rdx, sum ; 3rd arg, addr of sum
mov esi, dword [len] ; 2nd arg, value of len
mov rdi, arr ; 1st arg, addr of arr
call stats1
Callee
The function being called, the callee, must perform the
prologue and epilogue operations (as specified by the
standard calling convention) before and after the code to
perform the function goal
For this example, the function must perform the
summation of values in the array, compute the integer
average, return the sum and average values
14
02/03/2019
Example, Statistical Function2 (non-leaf)
This extended example will demonstrate calling a
simple void function to find the minimum, median,
maximum, sum and average of an array of numbers.
The HighLevel Language (HLL) call for C/C++ is as
follows:
stats2(arr, len, min, med1, med2, max, sum, ave);
For this example, it is assumed that the array is sorted in
ascending order
the median will be the middle value. For an even length
list, there are two middle values, med1 and med2, both
of which are returned
15
02/03/2019
Caller
There are 8 arguments and only the first six can be passed
in registers. The last two arguments are passed on the stack
The assembly language code in the calling routine for the
call to the stats function would be as follows:
Callee
The function must perform the summation of values in
the array, find the minimum, medians, and maximum,
compute the average, return all the values.
When call-by-reference arguments are passed on the
stack, two steps are required to return the value.
Get the address from the stack.
Use that address to return the value.
16
02/03/2019
17
02/03/2019
The call frame for this
function would be as
follows:
In this example, the
preserved registers rpb and
then r12 is pushed. When
popped, they must be popped
in the exact reverse order r12
and then rpb in order to
correctly restore their
original values.
18
02/03/2019
19