0

I'm a total noob in programming. I started learning Assembly recently. I do AT&T in 32bit Ubuntu

When I code:

movl 12(%ebp), -4(%ebp)

imull 12(%ebp), -4(%ebp)

There is an error message: Too many memory reference for 'mov' and 'imul'

Why can't we use -4(%ebp) instead of normal register like %eax as placeholder

Does it mean that we can only use n(%ebp) as a source operands?

Thanks a lot, guys

mickl
  • 48,568
  • 9
  • 60
  • 89
Peetah
  • 63
  • 1
  • 6
  • near duplicate of [Why isn't movl from memory to memory allowed?](https://stackoverflow.com/q/33794169) . That's asking why, rather than what to do instead, though. – Peter Cordes May 30 '23 at 08:17

1 Answers1

0

You cannot pass whatever operand you can think of to assembly instructions; while the assembly syntax smooths some differences over and can make you think that any operand is admitted, in facts the same assembly instruction with different kinds of operands gets translated to different opcodes, and they are defined only for a handful of operand types combinations.

Does it mean that we can only use n(%ebp) as a source operands?

In line of principle, it depends from the specific instruction; for most instructions, you can use a memory operand as either source or destination operand, but not both.

Look at the reference documentation for mov¹: there's no variant that takes two memory operands (although there are variants with the memory operand as either the source or destination); you have to go through a temporary register.

As for imul, again, look at the documentation: there's no variant with two memory operands, you have to go through a register (typically eax, as its encoding is more compact).


  1. When reading the documentation, remember that rXX (XX being 8, 16, 32 or 64) means "a general purpose XX bits register", r/mXX means "a general purpose XX bits register or memory operand" and immXX means "an XX bits immediate value". Also, most documentation is in Intel syntax, so if you are used to AT&T you have to reverse the operands order.
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • I realised that n(%ebp) can be used as a destination operand It's true that it can not take 2 memory operands – Peetah Jan 24 '18 at 07:58
  • Thank you so much! – Peetah Jan 24 '18 at 07:58
  • You are welcome! If my answer helped you, you may upvote it and, when the option becomes available and only if no better answer comes up, mark it as accepted (that gives 15 points to me and 5 to you). Both these actions are not mandatory at all, but they do encourage other users to help you with your questions (and, in the case of accepting an answer, give points to you as well). – Matteo Italia Jan 24 '18 at 08:09