0

When I compile a simple C program into assembly form on a x86-64 machines, I notice that each function call generally start with such instructions (w/ all optimization turned off)

pushq %rbp
movq  %rsp, %rbp

In the first instruction, we decrement the stack pointer by 8 bytes, hence growing the stack to make room for saving the value in frame pointer. In the second instruction, we copy the value in stack pointer to frame pointer, so frame pointer now points to the base address of next stack frame.

Various articles confirms my understanding: when we say movq %rsp, %rbp we are copying the value from first operand (i.e. source, in this case %rsp) to third operand (i.e. destination, in this case %rbp).

However, when I consult the reference manual of x86-64 (https://www.felixcloutier.com/x86/movq ) I found a conflicting semantics on movq. According to the reference, it seems this instruction is copying the value from second operand to the first operand, in the reversed order.

I'm confused, which one is the correct semantics? Is there any good online authoritative source for x86 instructions?

torez233
  • 193
  • 8
  • 2
    you're confused by at&t vs intel syntax – Iłya Bursov Dec 03 '22 at 03:12
  • 2
    That's a different `movq`, in your code you have the AT&T syntax `movq` which is just `mov` in the manual, you got unlucky here because there also exists a `movq` in Intel syntax (an MMX/SSE instruction) – harold Dec 03 '22 at 03:13
  • Got it, it is confusing because in official reference manual they are using intel syntax but in actual practice a different syntax, and I was not aware of this. After some googling it seems we can have compiler (assembler) generate a specific syntax. – torez233 Dec 03 '22 at 03:40
  • @harold i see, I thought movq is a variant of mov – torez233 Dec 03 '22 at 03:42
  • [What does AT&T syntax do about ambiguity between other mnemonics and operand-size suffixes?](https://stackoverflow.com/q/27990177) explains that the operands always disambiguate `movq xmm/m64, xmm` from `mov r/m64, r64` (`mov` with an AT&T `q` operand-size suffix). – Peter Cordes Dec 03 '22 at 07:34

0 Answers0