AMD64 (64-bit)

The AMD64 calling convention is used on 64-bit Unix-like systems by default. The idea is very similar except that a different set of registers is used and there is no shadow space requirement. Another difference is that the AMD64 calling convention allows up to 6 integer parameters and up to 8 floating point values to be passed via registers:

Parameter index
(zero based)
Integer/pointer
Floating point
0
RDI
XMM0
1
RSI
XMM1
2
RDX
XMM2
3
RCX
XMM3
4
R8
XMM4
5
R9
XMM5
6
on stack
XMM6
7
on stack
XMM7

The following macro instruction is a primitive implementation of such a mechanism. Just as in the case of the Microsoft x64 example, this one does not handle stack parameters:

macro amd64_call procName, [args]
{
a = 0
if ~args eq
forward
if a = 0
push rdi
mov rdi, args
else if a = 1
push rsi
mov rsi, args
else if a = 2
push rdx
mov rdx, args
else if a = 3
push rcx
mov rcx, args
else if a = 4
push r8
mov r8, args
else if a = 5
push r9
mov r9, args
else
display "This macro only supports up to 4 parameters", 10, 13
exit
end if
a = a + 1
end if
common
call procName
forward
if ~args eq
if a = 6
pop r9
else if a = 5
pop r8
else if a = 4
pop rcx
else if a = 3
pop rdx
else if a = 2
pop rsi
else if a = 1
pop rdi
end if
a = a - 1
end if
}

Using such a macro in 64-bit code intended to run on a Unix-like system for calling the procedure my_proc like this:

amd64_call my_proc, 128, 32

Would expand it into:

push rdi       ;Store RDI register on stack
mov rdi, 128 ;Load it with the first parameter
push rsi ;Store RSI register on stack
mov rsi, 32 ;Load it with the second parameter
call my_proc ;Call the my_proc procedure
pop rsi ;Restore RSI register
pop rdi ;Restore RDI register
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset