简体   繁体   English

rdi 寄存器用于无参数函数的目的

[英]Purpose of rdi register for no argument function

Consider this simple function:考虑这个简单的函数:

struct Foo {
  int a;
  int b;
  int c;
  int d;
  int e;
  int f;
};

Foo foo() {
  Foo f;
  f.a = 1;
  f.b = 2;
  f.c = 3;
  f.d = 4;
  f.e = 5;
  f.f = 6;

  return f;
}

It generates the following assembly:它生成以下程序集:

0000000000400500 <foo()>:
  400500:       48 ba 01 00 00 00 02    movabs rdx,0x200000001
  400507:       00 00 00 
  40050a:       48 b9 03 00 00 00 04    movabs rcx,0x400000003
  400511:       00 00 00 
  400514:       48 be 05 00 00 00 06    movabs rsi,0x600000005
  40051b:       00 00 00 
  40051e:       48 89 17                mov    QWORD PTR [rdi],rdx
  400521:       48 89 4f 08             mov    QWORD PTR [rdi+0x8],rcx
  400525:       48 89 77 10             mov    QWORD PTR [rdi+0x10],rsi
  400529:       48 89 f8                mov    rax,rdi
  40052c:       c3                      ret    
  40052d:       0f 1f 00                nop    DWORD PTR [rax]

Based on the assembly, I understand that the caller created space for Foo on its stack, and passed that information in rdi to the callee.根据程序集,我知道调用者在其堆栈上为Foo创建了空间,并将该信息在rdi传递给被调用者。

I am trying to find documentation for this convention.我正在尝试查找此约定的文档。 Calling convention in linux states that rdi contains the first integer argument. linux 中的调用约定规定rdi包含第一个整数参数。 In this case, foo doesn't have any arguments.在这种情况下, foo没有任何参数。

Moreover, if I make foo take one integer argument, that is now passed as rsi (register for second argument) with rdi used for address of the return object.此外,如果我让foo接受一个整数参数,它现在作为rsi (注册第二个参数)传递,rdi 用于返回对象的地址。

Can anyone provide some documentation and clarity on how rdi is used in system V ABI?任何人都可以提供有关如何在系统 V ABI 中使用rdi一些文档和说明吗?

See section 3.2.3 Parameter Passing in the ABI docs which says:请参阅ABI 文档中的第3.2.3参数传递,其中说:

If the type has class MEMORY, then the caller provides space for the return value and passes the address of this storage in %rdi as if it were the first argument to the function.如果类型具有 MEMORY 类,则调用者为返回值提供空间并在 %rdi 中传递此存储的地址,就好像它是函数的第一个参数一样。 In effect, this address becomes a "hidden" first argument.实际上,这个地址变成了“隐藏的”第一个参数。

On return %rax will contain the address that has been passed in by the caller in %rdi.返回时 %rax 将包含调用者在 %rdi 中传入的地址。

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

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