'What is the default register state when program launches (asm, linux)?

When the program launches (Linux ELF executable), are there zeros in eax, ebx, etc. or can there be anything?

(I'm not doing any calls or using extern libraries).

On my machine the registers are zeroed, but can I rely on such behavior in a new process when writing asm programs?



Solution 1:[1]

For AMD64 or x86-64 systems (64 bits) on Linux, the x86-64 ABI defines the initial content of registers.

There are similar specifications for i386 ABI, ARM ABI etc.

See wikipedia pages on ELF and ABI

Solution 2:[2]

x86-64 System V ABI

3.4.1 "Initial Stack and Register State" (Basile linked to the PDF version of this):

  1. %rsp points to the stack

    The stack pointer holds the address of the byte with lowest address which is part of the stack. It is guaranteed to be 16-byte aligned at process entry

  2. %rdx a function pointer that the application should register with atexit if it's non-zero.

    a function pointer that the application should register with

  3. %rbp is unspecified but the userland should set it to the base frame.

    The content of this register is unspecified at process initialization time, but the user code should mark the deepest stack frame by setting the frame pointer to zero.

  4. Everything else undefined.

Linux then follows it "because" the LSB says so.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Basile Starynkevitch
Solution 2