Is stack space reservation necessary for functions with less than four arguments?

Just started to learn the x64 build, and I have a question about functions, arguments, and the stack. As far as I understand, the first four arguments to the function are passed to the rcx, rdx, r8 and r9 registers (and xmm0-xmm3 for floats) on Windows. Thus, a trivial add function with four parameters would look like this:

add: mov r10, rcx add r10, rdx add r10, r8 add r10, r9 mov rax, r10 ret 

However, I came across documentation that mentions this :

. At a minimum, each function should reserve 32 bytes (four 64-bit values) on the stack. This space allows you to easily rewrite the registers passed to the function to a well-known location of the stack. A call function is not required to distinguish between the parameters of the input register on the stack, but the reservation of the stack space ensures that it can if necessary.

So, do I need to reserve stack space even if my functions take four parameters or less, or is this just a recommendation?

+18
assembly stack 64bit cpu-registers
Sep 12 '11 at 18:39
source share
2 answers

Your quote is from the "challenge" part of the document. At the very least, you do not need to worry about this unless you call other functions from your assembler code. If you do, then you must respect, among other things, the red zone and stack alignment considerations that the recommendation you recommend is intended to provide.

EDIT: This post clarifies the difference between the "red zone" and the "shadow space".

+13
Sep 12 '11 at 18:55
source share
— -

I just came across this without knowing, and it seems to be so. The first two commands in GetAsyncKeyState, for example, overwrite the stack above the return value in the 0x20 byte region, which you must reserve for the called party to use for parameters:

 user32.GetAsyncKeyState - mov [rsp+08],rbx user32.GetAsyncKeyState+5- mov [rsp+10],rsi user32.GetAsyncKeyState+A- push rdi user32.GetAsyncKeyState+B- sub rsp,20 
0
Mar 27 '15 at 22:59
source share



All Articles