c++assemblyx86-64calling-convention

Nulling %al register before calling print


Considering the code:

#include <cstdio>
int main()
{
  printf("null");
  return 0;
}

Why the assembly from Clang 3.3 for x86-64 GNU/Linux is:

main:                                   # @main
    pushq   %rax
    movl    $.L.str, %edi
    xorb    %al, %al
    callq   printf
    xorl    %eax, %eax
    popq    %rdx
    ret

.L.str:
    .asciz   "null"

What's the xorb %al, %al for? I know that %al is a lowest 8 bits of %rax register.

(Editor's note: Clang 3.4 and later will xor %eax, %eax as a more efficient way to zero AL.)


Solution

  • According to the sysv abi calling convention, before calling varargs functions the al register should be set to the number of vector registers used for passing arguments. In this case that's zero, and xor is a common idiom for zeroing a register.

    If you are interested in the calling convention, read the aforementioned abi doc.