1

The memory addressing general form (found it here) is:

[base + index*scale + disp]

So a correct instruction would be something like:

mov eax, [ebx + ecx*4 + 2]

And this instruction should be incorrect (but NASM found it to be correct!):

mov eax, [ebx + ecx + 123 + 5 - 2]

So why is this instruction correct (even though it does not conform to the memory addressing general form)?

I think it is correct because:

  • 123 + 5 - 2 are all considered to be one number (126) representing disp.
  • The documentation says: "If no scale is specified, scale is taken to be 1" (so ecx is effectively: ecx*1).
  • So the final result is: mov eax, [ebx + ecx*1 + 126].

Am I correct?

user8240761
  • 975
  • 1
  • 10
  • 15
  • 3
    Yes, that's correct. Assemblers can fold assembly-time constants for you, so things like `123 + 5 - 2` are collapsed. All of the parts that you see in the "general form" are optional except for one. – Cody Gray - on strike Jul 02 '17 at 14:59
  • Note that even something like `mov eax, [ebx - 2 + ecx + foo]` is correct—`foo` is treated as a symbol name and a relocation for `foo - 2` is emitted. – fuz Jul 02 '17 at 16:38
  • @fuz You mean `disp` can be a variable (for example `section .data foo DB 12`)? What about `scale`, can it also be a variable? – user8240761 Jul 02 '17 at 16:58
  • No! In that case `disp` would be the _address_ of the variable and _not the value_ of it. To put it another way: if disp=eax then it'd be `mov eax, offset disp` and not `mov eax, dword [disp]. – zx485 Jul 02 '17 at 17:51
  • @user8240761 Yes, `disp` can be a variable, but note that its address, not its value will be used. – fuz Jul 02 '17 at 18:04
  • @fuz What about `scale`, can it also be a variable (the address of the variable that is)? – user8240761 Jul 02 '17 at 19:30
  • @fuz Can `scale` also be a variable (the address of the variable that is)? – user8240761 Jul 02 '17 at 22:18
  • @user8240761 I don't think so. Since scale can only be one of 1, 2, 4, or 8, that wouldn't make much sense. – fuz Jul 02 '17 at 22:33

0 Answers0