2

While investigating a crash, I came across the following code snippet and immediately recognized that the mov instruction should actually be movq to get the correct 64-bit register operation.

#elif defined(__x86_64__)
    unsigned long rbp;
    __asm__ volatile ("mov %%rbp, %0" : "=r" (rbp));
    sp = (void **) rbp;
#else

Further to this, I also found documentation that claims that the rbp register for x86-64 is general purpose and does not contain the address of the current frame. I have also found documentation that claims that rbp does contain the address of the current frame. Can someone clarify?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Chappelle
  • 65
  • 2
  • 9
  • `rbp` contains the base address if you don't use [`-fomit-frame-pointer` (or similar options)](http://stackoverflow.com/questions/14666665/trying-to-understand-gcc-option-fomit-frame-pointer) http://stackoverflow.com/questions/579262/what-is-the-purpose-of-the-frame-pointer – phuclv Apr 16 '15 at 17:05

1 Answers1

7

Regarding the first part of your question (movq instead of mov), the assembler (as, in this case), will recognize that your operand is 64 bits, and will correctly use movq. mov is not a valid instruction, it's a way to tell the assembler "use the right mov variant depending on the operands".

Regarding the second part, it's actually both: it's a general purpose register, in the sense that it can hold any value. It is also used as a stack-frame base pointer. The '2.4 Stack operation' section of the AMD64 Application programming manual says:

A stack is a portion of a stack segment in memory that is used to link procedures. Software conventions typically define stacks using a stack frame, which consists of two registers—a stack-frame base pointer (rBP) and a stack pointer (rSP)—

Frederik Deweerdt
  • 4,943
  • 2
  • 29
  • 31
  • No, `mov` is completely valid and is the actual opcode in Intel syntax. In AT&T syntax the suffix is also not required when the context can tell the operand size, just like in Intel syntax. ["If the suffix is not specified, and there are no memory operands for the instruction, GAS infers the operand size from the size of the destination register operand (the final operand)."](https://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax#Operation_Suffixes) – phuclv Apr 16 '15 at 17:14