I am trying to learn assembly and there a couple of instructions whose purpose I do not fully understand.
C code
#include
Q1. sub rsp, 32
This is allocating space that is used to store some data. Although it allocates 32 bytes, the code is only using the first 16 bytes of that allocated space, a qword at [rbp-8] (0:edi) and a qword at [rbp-16] (rdi).
Q2. lea rax, [rip + L_.str] and mov rdi, rax
The lea is getting the address of a string stored in the "code" segement. It's moved to rdi which is used as one of the parameters for printf.
Q3. mov dword ptr [rbp - 4], 0 ... mov dword ptr [rbp - 8], edi
This stores a 64-bit little endian value composed of 0:edi at [rbp - 8]. I'm not sure why it's doing this, since it never loads from that qword later on.
It's normal for un-optimized code to store their register arguments to memory, where debug info can tell debuggers where to look for and modify them, but it's unclear why clang zero-extends argc
in edi
to 64 bits.
More likely that 0
dword is something separate, because it if the compiler really wanted to store a zero-extend argc
, compilers will zero-extend in registers with a 32-bit mov
, like mov ecx, edi
; mov [rbp-8], rcx
. Possibly this extra zero is a return-value temporary which it later decides not to use because of an explicit return 0;
instead of the implicit one from falling off the end of main
? (main
is special, and I think clang does create an internal temporary variable for the return value.)
Q4 mov qword ptr [rbp - 16], rsi ... mov rsi, qword ptr [rbp - 16]
Optimization off? It stores rsi then loads rsi from [rbp - 16]. rsi holds your argv function arg ( == &argv[0]
). The x86-64 System V ABI passes integer/pointer args in RDI, RSI, RDX, RCX, R8, R9, then on the stack.
... mov rsi, qword ptr [rsi + 8]
This is loading rsi with the contents of argv[1]
, as the 2nd arg for printf
. (For the same reason that main's 2nd arg was in rsi).
The x86-64 System V calling convention is also the reason for zeroing AL before calling a varargs function with no FP args.
Q5. mov dword ptr [rbp - 20], eax
Optimization off? It's storing the return value from printf, but never using it.