0

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
    
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Marijam
  • 5
  • 2
  • `rpb` is a typo for `rbp`, but that should just assemble as a store to that symbol name. Are you sure you're assembling in 64-bit mode? In 32-bit mode, `rdi` and `rsp` are just symbol names, not registers. `gcc -c foo.s` doesn't give any errors on my x86-64 GNU/Linux desktop. – Peter Cordes Mar 30 '21 at 20:14
  • (The code won't of course link because of that typo, and `lea` is pointless if you're not going to use RIP-relative addressing like `lea rdi, [RIP + message]`, though. And your code is in the .data section, not .text.) – Peter Cordes Mar 30 '21 at 20:16
  • @PeterCordes I corrected that typo and added .text section, but it is the same – Marijam Mar 30 '21 at 20:28
  • Yeah, like I said, the typo doesn't explain the error message. Likely you're assembling in 32-bit mode, like `gcc -m32`, which would only be the default on a 32-bit system. – Peter Cordes Mar 30 '21 at 20:31

1 Answers1

3

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)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847