将参数传递给函数-推入或寄存器 [英] Passing parameters to a function -- push or registers

查看:43
本文介绍了将参数传递给函数-推入或寄存器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个函数,执行以下操作:

I want to write a function that does something like:

def addme(x, y):
    return x + y

我知道已经有关于此的指令,但是我正在练习如何在函数中传递args.传递args的正确方法是什么?我看过类似的东西:

I know there is already an instruction for this, but I'm practicing how to pass args in a function. What would be the proper way to pass args? I have seen something like:

# addme(5,2)
push $2
push $5
call addme
add $16, %rsp

这是传递args的正确方法,方法是将它们以相反的顺序推入 stack ,然后在执行函数调用后重设堆栈指针?我还听说了参数是在%rdi,%rsi,... 中传递的.一种是正确的,另一种是不正确的,或者正确的方法是什么?

Is this the correct way to pass in args, by pushing them to the stack in reverse order and then after doing the function call resetting the stack pointer? I have also heard that the parameters are passed in %rdi, %rsi, .... Is one correct and the other is incorrect, or what is the proper way to do this?

推荐答案

这是传递args的正确方法吗?

Is this the correct way to pass in args ...

这取决于通话惯例:

对于64位x86 Windows程序,参数在寄存器 rcx rdx r8 r9 .

For 64-bit x86 Windows programs the arguments are passed in registers rcx, rdx, r8 and r9.

对于64位x86 Linux程序,寄存器 rdi rsi rcx rdx ,<使用code> r8 r9 .

And for 64-bit x86 Linux programs the registers rdi, rsi, rcx, rdx, r8 and r9 are used.

但是,尤其是在16位DOS和Windows中,以及在32位Windows中,混合使用不同的调用约定是很常见的.

However, especially in 16-bit DOS and Windows but also in 32-bit Windows it was common to mix up different calling conventions.

在64位Windows或Linux上仍然可以这样做:

This is still possible in 64-bit Windows or Linux:

如果您编写自己的编译器或完全用汇编语言编写程序,则可以决定对某些函数使用不同的调用约定-例如,如果函数名开头为,则在堆栈上传递所有参数而不是寄存器 onstk _ .

If you write your own compiler or if you write your program completely in assembly language, you can decide to use a different calling convention for certain functions - for example passing all arguments on the stack instead of registers if the function name begins with onstk_.

如果我正确理解了您的示例,则您不会使用C或C ++,而是会使用Python之类的语言.

If I understand your example correctly, you don't use C or C++ but some language like Python.

我知道某些编程语言(例如Java或Matlab;我不了解Python)在本机"语言中使用完全不同的调用约定.(编译或汇编)代码被调用.所以也许您必须以完全不同的方式传递参数.

I know that some programming languages (like Java or Matlab; I don't know about Python) use a completely different calling convention when "native" (compiled or assembly) code is called. So maybe you'll have to pass the arguments in a completely different way.

这篇关于将参数传递给函数-推入或寄存器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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