Microsoft x64 (64-bit)

Microsoft uses its own calling convention in the 64-bit mode (long mode) using a mixed register/stack paradigm for passing procedure parameters. This means that only the first four parameters may be passed via registers and the rest (if any) should be pushed onto the stack. The following table illustrates which registers are used and in what manner:

Parameter index
(zero based)
Integer/pointer
Floating point
0
RCX
XMM0
1
RDX
XMM1
2
R8
XMM2
3
R9
XMM3

 

All of this looks quite clear, yet there are two things that we need to pay special attention to:

  • The stack must be aligned on a 16-bytes boundary
  • A 32-bytes shadow space on the stack is required--32 bytes between the last pushed stack parameter (if any) and the return address

The following macro instruction (ms64_call) is simplistic; it is a primitive implementation of this calling convention. This specific macro does not support stack parameters:

macro ms64_call procName, [args]
{
a = 0
if ~args eq
forward
if a = 0
push rcx
mov rcx, args
else if a = 1
push rdx
mov rdx, args
else if a = 2
push r8
mov r8, args
else if a = 3
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
sub rsp, 32 ; Allocate shadow space
call procName ; Call procedure
add rsp, 32 ; Free shadow space
forward
if ~args eq
if a = 4
pop r9
else if a = 3
pop r8
else if a = 2
pop rdx
else if a = 1
pop rcx
end if
a = a - 1
end if
}

Consider an example of calling the procedure labeled my_proc in the 64-bit code, using Microsoft x64 calling convention:

ms64_call my_proc, 128, 32

Such a macro instruction would be expanded to the following:

push rcx         ;Save RCX register on stack
mov rcx, 128 ;Load it with the first parameter
push rdx ;Save RDX register on stack
mov rdx, 32 ;Load it with the second parameter
sub rsp, 32 ;Create 32 bytes shadow space
call my_proc ;Call the my_proc procedure
add rsp, 32 ;Destroy shadow space
pop rdx ;Restore RDX register
pop rcx ;Restore RCX register
..................Content has been hidden....................

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