问题
I want to write a function that does something like:
def addme(x, y):
return x + y
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
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?
回答1:
Is this the correct way to pass in args ...
This depends on the calling convention:
For 64-bit x86 Windows programs the arguments are passed in registers rcx, rdx, r8 and r9.
And for 64-bit x86 Linux programs the registers rdi, rsi, rcx, rdx, r8 and r9 are used.
However, especially in 16-bit DOS and Windows but also in 32-bit Windows it was common to mix up different calling conventions.
This is still possible in 64-bit Windows or Linux:
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_.
If I understand your example correctly, you don't use C or C++ but some language like 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.
来源:https://stackoverflow.com/questions/63654216/passing-parameters-to-a-function-push-or-registers