X86与ARM平台下的参数传递机制


X86与ARM平台区别参考:一文看懂arm架构和x86架构有什么区别_bleauchat的博客-CSDN博客_arm x86

1. X86下的参数传递

参数传递:

(1)32位程序使用栈传递。

(2)64位程序根据参数的个数而不同, 当参数1~6个,使用寄存器传递;参数大于6个,多出来的参数使用栈传递。

2. ARM下的参数传递

  • AAPCS标准简介

AAPCS(ARM Architecture Procedure Call Standard): ARM架构过程调用标准。它规定了一些子程序间调用的基本规则,这些规则包括子程序调用过程中寄存器的使用规则,数据栈的使用规则,参数的传递规则等等……。AAPCS是ATPCS的改进版。

1.1 AAPCS-32

image-20211008213006945

  1. r0-r3(a1-a4) 用作传入函数参数,传出函数返回值。在子程序调用之间,可以将 r0-r3 用于任何用途。被调用函数在返回之前不必恢复 r0-r3。—如果调用函数需要再次使用 r0-r3 的内容,则它必须保留这些内容。
  2. r4-r8、r10 和 r11(v1-v5、v7 和 v8)被用来存放函数的局部变量。其中,只有 v1-v4 可以被整个 Thumb 指令集统一使用,但 AAPCS 并不要求 Thumb 代码只使用那些寄存器。如果被调用函数使用了这些寄存器,它在返回之前必须恢复这些寄存器的值。
  3. r9 的作用是特定于平台的。不需要这种特殊寄存器的虚拟平台可以将 r9 指定为附加的被调用者保存的变量寄存器 v6。它可以在位置无关数据模型中将其指定为静态基 (SB),或者在具有线程本地存储的环境中将其指定为线程寄存器 (TR)。
  4. r11 可以作为帧指针 FP
  5. r12 是内部调用暂时寄存器 IP。它在过程链接胶合代码(例如,交互操作胶合代码)中用于此角色。在过程调用之间,可以将它用于任何用途。被调用函数在返回之前不必恢复 r12。
  6. 寄存器 r13 是栈指针 SP。它不能用于任何其它用途。**SP **中存放的值在退出被调用函数时必须与进入时的值相同。
  7. 寄存器 r14 是链接寄存器 LR。如果您保存了返回地址,则可以在调用之间将 r14 用于其它用途,程序返回时要恢复
  8. 寄存器 r15 是程序计数器 PC。它不能用于任何其它用途。

关于参数与返回值

子程序之间通过寄存器r0 - r3传递子程序前4个参数,剩余参数通过压栈传递,8个字节的double类型由两个寄存器一起传递。

子程序的返回值也使用r0 - r3传递,若是组合类型,超过4字节的将结果存在内存中,然后返回内存地址.

1.2 AAPCS-64

image-20211008220419570

The first eight registers, r0-r7, are used to pass argument values into a subroutine and to return result values from a function. They may also be used to hold intermediate values within a routine (but, in general, only between subroutine calls). Registers r16 (IP0) and r17 (IP1) may be used by a linker as a scratch register between a routine and any subroutine it calls (for details, see §5.3.1.1, Use of IP0 and IP1 by the linker). They can also be used within a routine to hold intermediate values between subroutine calls. The role of register r18 is platform specific. If a platform ABI has need of a dedicated general purpose register to carry inter-procedural state (for example, the thread context) then it should use this register for that purpose. If Procedure Call Standard for the ARM 64-bit Architecture ARM IHI 0055B Copyright © 2010-2013 ARM Limited. All rights reserved. Page 15 of 32 the platform ABI has no such requirements, then it should use r18 as an additional temporary register. The platform ABI specification must document the usage for this register

关于参数传递与返回值:

前八个寄存器 r0-r7 用于将参数值传递给子程序并从函数返回结果值。

参考资料:

  1. C语言在ARM中函数调用时,栈是如何变化的? - 云+社区 - 腾讯云 (tencent.com)
  2. AAPCS - 学习笔记_沉思-CSDN博客_aapcs
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值