I am currently learning ASM by disassembling some of C codes. One thing interested me is that the gcc compiler generates code like this
movq %rax,%rax
which is obviously meaningless. So what is the purpose of doing that?
I am wondering if it is used for waste a few cycles of CPU in order to improve the pipeline?
Thank you for your hint!
The RAX register is used for return values in functions regardless of whether you're working with Objective-C or Swift.
rax is the 64-bit, "long" size register. It was added in 2003 during the transition to 64-bit processors. eax is the 32-bit, "int" size register. It was added in 1985 during the transition to 32-bit processors with the 80386 CPU.
Thus (%rax) means to get the value of the pointer currently stored in %rax .
mov %rdi, -8(%rbp) The mov instruction moves the 1st argument (passed in %rdi) to its place on the stack. The argument occupies the first stack position (stack entries are 8 bytes) after the base pointer (%rbp). 0(%rbp) stores the previous frame's base pointer.
It is basically a no-op, yes.
The compiler does this because branching to an address aligned on a 4-byte boundary is faster than branching to an unaligned address. So if you have a loop, the compiler will insert "padding" just before the start of it in order to get it into alignment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With