0

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
carl.hiass
  • 1,526
  • 1
  • 6
  • 26
  • What operating system are you programming for? The calling convention depends on that. – fuz Aug 30 '20 at 10:40

1 Answers1

3

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.

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38
  • thanks, could you please provide a link to learn more about the 64-bit Linux calling convention? – carl.hiass Aug 30 '20 at 06:59
  • 1
    @carl.hiass It is described in [this Wikipedia article](https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI). Because the calling convention is not only used in Linux, it is not named "Linux calling convention". – Martin Rosenau Aug 30 '20 at 07:01