What’s the best way to remember the x86-64 System V arg register order?

If you remember C memcpy‘s arg order, and how rep movsb works, that’s most of the way to remembering x86-64 System V. The design makes memcpy(dst, src, size) cheap to implement with rep movsb, except leaving RCX unused in more functions because it’s needed for variable-count shifts more often than anything needs RDX. Then R8 … Read more

What could C/C++ “lose” if they defined a standard ABI?

The freedom to implement things in the most natural way on each processor. I imagine that c in particular has conforming implementations on more different architectures than any other language. Abiding by a ABI optimized for the currently common, high-end, general-purpose CPUs would require unnatural contortions on some the odder machines out there.

How do vararg functions find out the number of arguments in machine code?

The trick is that you tell them somehow else. For printf you have to supply a format string which even contains type information (which might be incorrect though). The way to supply this information is mainly user-contract and often error-prone. As for calling conventions: Usually the arguments are pushed onto the stack from left to … Read more

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

This depends entirely on the ABI for each platform. Since you mention eax and ebx let’s see what’s the case for x86 (as of Linux v5.17.5). In fs/binfmt_elf.c, inside load_elf_binary(), the kernel checks if the ABI specifies any requirements for register values at program loading: /* * The ABI may specify that certain registers be … Read more

why is data structure alignment important for performance?

Alignment helps the CPU fetch data from memory in an efficient manner: less cache miss/flush, less bus transactions etc. Some memory types (e.g. RDRAM, DRAM etc.) need to be accessed in a structured manner (aligned “words” and in “burst transactions” i.e. many words at one time) in order to yield efficient results. This is due … Read more

Is arm64-v8a compatible with armeabi-v7a?

Many modern Android devices (i.e. Nexus 5x) have AArch64 processors with arm64-v8a instruction set. Both – armeabi and armeabi-v7a – libraries run fine on these modern devices. Therefore, we can assume the answer to your question to be ‘YES’. See this for a breakdown of ABI management on Android: https://developer.android.com/ndk/guides/abis.html

How do C compilers implement functions that return large structures?

None; no copies are done. The address of the caller’s Data return value is actually passed as a hidden argument to the function, and the createData function simply writes into the caller’s stack frame. This is known as the named return value optimisation. Also see the c++ faq on this topic. commercial-grade C++ compilers implement … Read more