Windows x64 Assembly function call Convention

Source: Internet
Author: User
Tags windows x64

I have recently written some optimizations for string functions and used x64 Assembly. I am also the first time in this article, so I would like to share with you.

 

X86: Also known as x32, it indicates the Intel x86 architecture, that is, Intel's 32-bit 80386 assembly instruction set.

X64: EM64T of AMD64 and Intel, excluding IA64. You can search for the differences between the three.

 

Register changes in x64 compared with x86,

 

We can see from the figure that the main change in the X64 architecture to the X86 architecture is to double all the original registers. For example, EAX is now expanded to RAX. At the same time, in addition, a new version is added from R8 ~ R15 these 8 64-bit senders have a little bit of experience with the back-to-back (some of the features of the family are the many registers ).

 

Then there are some changes as follows:

  • The default function call Convention for x64 is fast call, that is, ABI is fast call;
  • When a function is called, the first four parameters are stored from left to right in the RCX, RDX, R8, and R9 registers, and the remaining parameters are added to the stack from left to right;
  • The caller is responsible for allocating a 32-Byte "shadow space" on the stack to store the values of the four registers that store the call parameters (that is, the first four call parameters );
  • When a parameter smaller than 64-bit is passed, the high bit is not filled with zero (for example, only ecx is passed). If the parameter is greater than 64-bit, it must be passed by address;
  • When the return value of the called function is an integer, the return value is stored in RAX;
  • The called function is not responsible for clearing the stack, and the caller is responsible for clearing the stack;
  • RAX, RCX, RDX, R8, R9, R10, and R11 are volatile and do not require special protection. Other registers need protection. (Only eax, ecx, and edx in x86 are volatile)
  • The stack must be 16 bytes aligned. The "call" command will return an 8-byte value to the stack (note: the value of the original RIP instruction register before the function call, the stack is not correct (because the four registers RCX, RDX, R8, and R9 are exactly 32 bytes, 16 bytes aligned, and now 8 bytes are added ). Therefore, all functions called by non-leaf nodes must adjust the stack RSP address to 16n + 8 to align the stack.
  • For R8 ~ R15 registers. We can use r8, r8d, r8w, and r8b to represent the 64-bit, low 32-bit, low 16-bit, and low 8-bit of r8 registers.

 

Some other minor issues to be aware:

  • Note that AMD64 does not support the push 32bit register instruction. The best way is to use the 64-bit register for push and pop, that is, push rbx instead of push ebx.
  • In addition, the RBP stack base pointer of the X64 platform is discarded and used only as a common register, all stack operations are completed through the RSP pointer.

 

Legacy problems

The above are all call conventions on Windows, that is, the call conventions used on Visual Studio. It is not clear whether the GCC function call conventions are consistent. If you have any knowledge, please give us some advice, from the 64-bit assembly of asmlib, GCC seems to use rdi rather than rcx as the first parameter.

 

Example:

; Example code 1.asm; Syntax: GoASMDATA SECTIONtext db 'Hello x64! ', 0 caption db 'my First x64 application', 0 code sectionstart: sub rsp, 28 h; stack reserved shadow space (40 + 8) bytes xor r9d, r9d; r9lea r8, caption; r8lea rdx, text; rdxxor rcx, rcx; rcxcall MessageBoxAadd rsp, 28 h; caller resume stack ret

 

References

Windows X64 function call Convention and assembly code analysis | http://kelvinh.github.io/blog/2013/08/05/windows-x64-calling-conventions/

X64 parameter transfer | http://hyperiris.blog.163.com/blog/static/1808400592011715111957863/

Windows X64 Assembly entry (1) | http://wenku.baidu.com/view/3093d52d453610661ed9f4b0.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.