Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the purpose of mov %rax,%rax?

Tags:

gcc

assembly

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!

like image 638
xis Avatar asked Dec 15 '10 21:12

xis


People also ask

What is the RAX register used for?

The RAX register is used for return values in functions regardless of whether you're working with Objective-C or Swift.

What does Rax mean in assembly?

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.

What does (% Rax mean?

Thus (%rax) means to get the value of the pointer currently stored in %rax .

What is MOV RDI?

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.


1 Answers

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.

like image 63
Anon. Avatar answered Oct 17 '22 19:10

Anon.