assemblygccx86-64gnu-assemblerintel-syntax

too many references for 'mov' when using two 64-bit registers?


I'm trying to compile one very simple program, but I get 2 errors that I can not fix:

1.s:13:Error: too many memory references for 'mov'
1.s:15:Error: too many memory references for 'lea'

Here is my code, I hope someone sees my mistake.

.intel_syntax noprefix
.data
message: .asciz "Hello World\n"
.text
.global main
main:
    push rbp
    mov rbp, rsp
    lea rdi, message
    call printf
    mov rax, 0
    pop rbp
    ret
    

Solution

  • That code assembles + links just fine for me, on my x86-64 Arch GNU/Linux system:
    gcc -no-pie foo.S

    Those error messages match what I get with gcc -c -m32 foo.S, so probably you're using a 32-bit system where -m32 is the default for your GCC. If gcc -m64 -no-pie works, you can assemble + link it, otherwise you'll have to upgrade your system to 64-bit.

    In 32-bit mode, RBP and so on aren't register names, so GAS just treats them as unknown symbol names. And of course x86 doesn't allow instructions with two explicit memory operands like mov mem2, mem1. (And lea needs a register destination, and mov rax, 0 also errors on the ambiguous operand-size for the store.)

    (I had to use -no-pie because you wrote lea rdi, message instead of lea rdi, [rip+message] to use RIP-relative addressing instead of 32-bit absolute: How to load address of function or label into register)