简体   繁体   English

ARM 程序集 - 访问参数与返回值?

[英]ARM assembly - access parameter vs return value?

I have a function prototype int Palindrome(const char *c_style_string);我有一个函数原型int Palindrome(const char *c_style_string);

In ARM v8 assembly, I believe that the parameter is stored in register w0.在 ARM v8 汇编中,我相信参数存储在寄存器 w0 中。 However, isn't this also the register that ret outputs the value of?但是,这不也是ret输出值的寄存器吗?

If so, what do I need to do so that values do not get overwritten?如果是这样,我需要做什么才能使值不会被覆盖? I was thinking something like mov w0, w1 at the beginning of my code so that I refer to c_style_string as w1 whenever I parse through it, and then edit w0 to store an int...would this be right?我在我的代码开头想像mov w0, w1这样的东西,这样每当我解析它时我都会将 c_style_string 称为 w1,然后编辑 w0 来存储一个 int ......这对吗?

Thank you!谢谢!

You may want to write your assembly code in compliance with the ABI for ARM 64-bit Architecture .您可能希望编写符合ABI for ARM 64-bit Architecture 的汇编代码。
In the example above, you could keep the address for c_style_string in a 'Callee-saved' register (X19-X29)', and copy it to x0/w0 every time you are calling a Palindrome() - I am assuming here Palindrome() is a C function, and is therefore itself compliant with the ARCH 64-bit ABI.在上面的示例中,您可以将 c_style_string 的地址保存在“Callee-saved”寄存器 (X19-X29)' 中,并在每次调用 Palindrome() 时将其复制到 x0/w0 - 我在这里假设为 Palindrome( ) 是一个 C 函数,因此它本身符合 ARCH 64 位 ABI。
A desirable side-effect would be that your C code could call always your assembly code, and vice-versa.一个理想的副作用是您的 C 代码可以始终调用您的汇编代码,反之亦然。

IMHO, your best solution is to write the C function, or minimal function, then tell the compiler to output the assembly language.恕我直言,您最好的解决方案是编写 C 函数或最小函数,然后告诉编译器输出汇编语言。 This will show the calling interface for functions.这将显示函数的调用接口。

You could also look up the register passing convention in your compiler's documentation.您还可以在编译器的文档中查找寄存器传递约定。

If you want to preserve register values, you should use the PUSH instruction (or it's equivalent, depending on ARM mode or Thumb mode).如果要保留寄存器值,则应使用 PUSH 指令(或等效指令,具体取决于 ARM 模式或 Thumb 模式)。 Also remember to POP the registers before the end of the function.还要记住在函数结束之前 POP 寄存器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM