首页 > 解决方案 > x64 参数和返回值调用约定

问题描述

我调用 Clang 12.0.0-Os -march=haswell来编译以下 C 程序:

int bar(int);

int foo(int x) {
  const int b = bar(x);
  if (x || b) {
      return 123;
  }
  return 456;
}

生成以下程序集:

foo:                                    # @foo
        push    rbx
        mov     ebx, edi
        call    bar
        or      eax, ebx
        mov     ecx, 456
        mov     eax, 123
        cmove   eax, ecx
        pop     rbx
        ret

https://gcc.godbolt.org/z/WsGoM56Ez

据我了解, foo 的调用者在 RAX/EAX 中设置了 x。foo 然后调用 bar,这不需要修改 RAX/EAX,因为 x 作为未修改的输入传递。

or eax, ebx指令似乎将输入 x 与 bar 的结果进行比较。这个结果如何在 EBX 中结束?服务于什么目的mov ebx,edi

标签: cassemblyx86-64calling-convention

解决方案


恐怕你误会了:

You can verify the basics by compiling a function like int foo(int x){return x;} - you'll see just a mov eax, edi.

Here is a commented version:

foo:                                    # @foo
        push    rbx           # save register rbx
        mov     ebx, edi      # save argument `x` in ebx
        call    bar           # a = bar()  (in eax)
        or      eax, ebx      # compute `x | a`, setting FLAGS
        mov     ecx, 456      # prepare 456 for conditional move
        mov     eax, 123      # eax = 123
        cmove   eax, ecx      # if `(x | a) == 0` set eax to 456
        pop     rbx           # restore register rbx
        ret                   # return value is in eax

The compiler optimizes x || b as (x | b) != 0 which allows for branchless code generation.

Note that mov doesn't modify the FLAGS, unlike most integer ALU instructions.


推荐阅读