How to store array and pointer value inputs in assembly?

余生颓废 提交于 2019-12-24 05:59:55

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!