如何在汇编中存储数组和指针值输入? [英] How to store array and pointer value inputs in assembly?

查看:28
本文介绍了如何在汇编中存储数组和指针值输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个函数有数组输入和其他类型的值如

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?

如果我像这样存储它们

pushq %rbp
movq %rsp, %rbp
pushq %rbp

那么,输入的地址是什么?

Then, what is the address of the inputs?

我觉得

16(rbp)=n 
24(rbp)=&a
32(rbp)=&b 
40(rbp)... 40(rbp,n,8)=arr.

是真的吗?

推荐答案

主要的 64 位 ABI 是

The main 64-bit ABIs are

如果前四 (4) 个参数属于 INTEGER 类,则它们都使用寄存器.
指针和整数类型属于此类.
数组衰减为指针1,指针在 ABI 级别作为 64 位整数传递.
就像这个函数需要四个整数一样.

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.

对于 Windows 编程 arr[], n, &a, &bRCX, RDX, 中传递R8R9.
对于 Linux 编程 arr[], n, &a, &bRDIRSIRDXRCX.

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.

要访问数组,需要做指针运算.假设 RDI 持有指向数组的指针(读取指向数组第一个元素的指针):

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]

var ab 可以作为普通指针访问(假设 Linux ABI)

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

参数 n 可以直接从 RSI 读取.

The argument n can be read directly from RSI.

请注意,您对堆栈仍有一些要求,尤其是在 Windows 上.
您可以阅读相关 ABI 了解更多信息.

Note that you still have some requirement for the stack, particularly on Windows.
You can read the relevant ABI for more information.

1 严格来说不是 C++ 规范中decay"动词的正确用法.

1 Not strictly a correct use of the "decay" verb as found in the C++ specs.

这篇关于如何在汇编中存储数组和指针值输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆