-1

Can I use mov with an indirect operand as both the source and destination?

E.g.

mov eax, OFFSET foo
mov esi, OFFSET bar
mov [eax],[esi + LENGTHOF bar]

From what I've tried I'm guessing you can't due to an invalid instruction operand error. But I haven't read anywhere that explicitly states you can't so I want to make sure it's not due to some other error.

LazerSharks
  • 3,089
  • 4
  • 42
  • 67
  • 1
    It's possible in one _special case_ ... for `mov [esp],[...]` / `mov [...],[esp]` you could use `push` / `pop` with a `m32` memory operand. The disadvantage (beyond the implicit need to have the 2nd address operand in `esp`) is that `esp` changes as well. – FrankH. Feb 17 '14 at 17:28
  • Related, basically a duplicate: [Why isn't movl from memory to memory allowed?](https://stackoverflow.com/q/33794169) although that spends more time on the "why not" part. Also [Why cannot do mov \[eax\], \[ebx\]](https://stackoverflow.com/q/54482505) and other older/newer duplicates have various versions of the same explanation. – Peter Cordes Jun 25 '22 at 22:17

1 Answers1

3

The x86 mov instruction does not support memory-to-memory moves.

Have a look at Volume 2: Instruction Set Reference, namely the MOV instruction. There are reg <- reg, mem <- reg, and reg <- mem forms, but no mem <- mem.

To move data from memory to memory, one must either use an intermediate register, or the movs instruction, which moves a value from address DS:ESI to ES:EDI. This is the reason those registers are named as such (source index, and destination index).

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328