0

Very simple question here...I'm trying to wrap my head around assembler and curious as to whether these to operations are equivalent:

mov [ebx], 5

and

lea esi, ebx
mov esi, 5

Thanks!

cjones26
  • 3,459
  • 1
  • 34
  • 51

3 Answers3

4

No. mov [ebx], 5 puts the value 5 into the address pointed to by ebx (at least that's the general idea of what it should do. MASM, for one, will reject it because it doesn't know what size of destination you want, so you'd need mov byte ptr [ebx], 5, or mov word ptr [ebx], 5, etc.)

The second copies ebx into esi, then copies 5 into esi. It doesn't (even attempt to) move anything into memory. What you're (apparently) looking for would be more like:

lea esi, ebx
mov [esi], 5

Again, you'll run into the same thing though: you'll need to specify byte ptr or word ptr, or whatever. Also note that in this case, it's rather wasteful to use lea -- you're doing the exact equivalent of mov esi, ebx. You normally only use lea when you want to do a more complex address calculation, something like: lea esi, ebx*4+ebp.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    +1 Deleted my answer, though I think you're still missing `[ebx]` in the `lea` instruction. – user786653 Oct 10 '11 at 21:39
  • @user786653: There may be *some* assembler that requires brackets for the `lea` (and MASM, for one, might well allow but ignore them) but they're not normally required. – Jerry Coffin Oct 10 '11 at 21:44
  • 1
    Well I can only talk about the assemblers I regularly use NASM (which requires it) and GAS (which requires it in AT&T mode (`lea (%ebx), %esi` rather than `lea %ebx, %esi`) as well as `.intel_syntax` same as NASM). – user786653 Oct 10 '11 at 21:53
  • @user786653: how odd -- I'm nearly certain I remember a conversation a few years ago with one of the developers of NASM where he said that it only used the brackets when there was going to be a reference to memory. – Jerry Coffin Oct 10 '11 at 21:57
  • Well, `lea` _does_ take a reference to memory [Vol. 2A 3-579](http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html), it doesn't do any dereferencing, though. Hence why it's often used for simple arithmetic calculations that don't actually involve memory references e.g. `lea eax, [eax+eax*2]` for `eax *= 3`. – user786653 Oct 10 '11 at 22:05
  • The source operand for `lea` [uses memory operand syntax and machine encoding](https://stackoverflow.com/questions/46597055/using-lea-on-values-that-arent-addresses-pointers/46597375#46597375) even though it's a shift-and-add instruction. It's totally weird and non-standard to omit the brackets, except in the MASM-syntax case of `lea esi, symbol_name` because `symbol_name` without `OFFSET` is a memory operand. NASM won't assemble this: `foo.asm:1: error: invalid combination of opcode and operands`. Using `lea esi,[ebx]` to copy a register is totally pointless vs. `mov esi,ebx` anyway... – Peter Cordes Jul 03 '18 at 04:11
  • But note that `lea esi, symbol_name` is also useless (in 32-bit mode) compared to `mov esi, OFFSET symbol_name` which is faster and 1 byte shorter. In 64-bit mode, RIP-relative LEA is useful. – Peter Cordes Jul 03 '18 at 04:14
2

You can do

lea esi, [ebx]
mov [esi], 5

and this will be equal to your first snippet.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • Thanks! Out of curiosity, what would "lea esi, ebx" do? – cjones26 Oct 10 '11 at 21:37
  • As already pointed out to in other responses, there is no such thing as `lea esi, ebi`. `lea` itslef will (l)oad (e)ffective (a)ddress, that is you need to provide address argument rather then just register. Hence `[ebx]`. – Roman R. Oct 10 '11 at 21:39
  • Check this for details on `LEA` http://stackoverflow.com/questions/1658294/x86-asm-whats-the-purpose-of-the-lea-instruction – Roman R. Oct 10 '11 at 21:40
0

No, they aren't, mov [ebx], 5 moves 5 to the memory location designated in ebx, while

lea esi, ebx
mov esi, 5

Calculates the memory address into esi (in this case it just copies ebx to esi) and then moves 5 into esi, not writing to memory.

Femaref
  • 60,705
  • 7
  • 138
  • 176