0

I'm learning x86(_64) assembly from trial and error. One thing I want to do is move the value from say -4 to -8 in the stack, but the GNU Assembler doesn't seem to like this:

error: invalid operand for instruction
    movl -4(%rsp), -16(%rsp)

And I'm not sure why. To me it logically makes sense, move the value from the stack at offset -4 into -16.

flooblebit
  • 477
  • 1
  • 3
  • 9
  • You can, but only by using existing instructions. Check your CPU ISA documentation to see which instructions exist (to move memory to memory in single instruction on x86 you can use `movs` instruction, but the setup for single move would be so costly, that it makes lot more sense to use 2x `mov` with spare register to store the value temporarily). EDIT *"To me it logically makes sense"* - yes, but assembly is not normal programming language & does not follow logical sense. It's almost 1:1 mapping of HW instructions into textual form, so it makes lot more sense from HW chip design, not as PL. – Ped7g Mar 20 '18 at 21:53
  • 1
    I.e. *"learning x86(_64) assembly from trial and error"* is asking for very rough ride... certainly trial and error is very important in assembly learning, but without the theory-first part it will be utter pain... – Ped7g Mar 20 '18 at 21:56
  • `push qword [ rsp + somewhereinstack ]` is a valid instruction, which mov data from one stack-memory-location to another one. – sivizius Mar 21 '18 at 10:08

1 Answers1

1

MOV does not have an opcode where both the source and the destination are memory addresses. You need to move either from a register or to a register (or both). To copy a value from one stack position to another, simply MOV into a register and then MOV into the destination.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • 1
    There are a zillion duplicates for this question, please close instead of re-answering common questions. See the FAQ section of https://stackoverflow.com/tags/x86/info for a link to one such duplicate. – Peter Cordes Mar 20 '18 at 22:50