Can anyone tell me what the following code in x86 ASM does? It's only part of a larger file, but it's just this bit that's getting me down.
find_max: 6 .LFB0: 7 .cfi_startproc 8 pushq %rbp 9 .cfi_def_cfa_offset 16 10 movq %rsp, %rbp 11 .cfi_offset 6, -16 12 .cfi_def_cfa_register 6 13 movl %edi, -20(%rbp) 14 movl -20(%rbp), %eax 15 cltq 16 movl a(,%rax,4), %eax 17 movl %eax, -4(%rbp) 18 movl -20(%rbp), %eax 19 movl %eax, -8(%rbp) Specifically,
- What's initially in %edi on line 13?
- Why is the code referencing -20(%rbp)?
- And what exactly does line 16 do?
- What's the wisdom behind switching behind the 32-bit registers and the 64-bit registers (for instance in the case of line 15)?
The C code I disassembled to get this goes something like the following:
extern int a[]; int find_max(int n) { int max = a[n]; int pos = n; int x; while (n > 0) { n--; x = a[n]; if (x > max) { max = x; pos = n; } } return pos; }