问题
If a function has input of array and other type of value such as
f(arr[],n,&a,&b)
Then, how can I store the inputs?
If I store them like
pushq %rbp
movq %rsp, %rbp
pushq %rbp
Then, what is the address of the inputs?
I think
16(rbp)=n
24(rbp)=&a
32(rbp)=&b
40(rbp)... 40(rbp,n,8)=arr.
Is it true?
回答1:
The main 64-bit ABIs are
- Microsoft x64 Calling Convention
- SYS V ABI
both use registers for the first four (4) arguments if they are of the class INTEGER.
Pointers and integral types are of such class.
Arrays decay into pointers1, pointers are passed as 64-bit integers at the ABI level.
So is like the function takes four integers.
For Windows programming arr[], n, &a, &b are passed in RCX, RDX, R8 and R9.
For Linux programming arr[], n, &a, &b are passed in RDI, RSI, RDX and RCX.
To access the array, you need to do pointer arithmetic. Supposing RDI holds the pointer to the array (read pointer to the first element of the array):
mov eax, DWORD [rdi] ;access arr[0]
mov ebx, DWORD [rdi + 04h] ;access arr[1]
The var a and b can be accessed as normal pointers (assuming Linux ABI)
mov eax, DWORD [rdx] ;Read a into eax
mov DWORD [rcx], eax ;b = a
The argument n can be read directly from RSI.
Note that you still have some requirement for the stack, particularly on Windows.
You can read the relevant ABI for more information.
1 Not strictly a correct use of the "decay" verb as found in the C++ specs.
来源:https://stackoverflow.com/questions/40507639/how-to-store-array-and-pointer-value-inputs-in-assembly