cjones26
cjones26

Reputation: 3804

x86 Intel Assembly -- Are these two statements equivalent?

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!

Upvotes: 0

Views: 319

Answers (3)

Jerry Coffin
Jerry Coffin

Reputation: 490583

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.

Upvotes: 4

Femaref
Femaref

Reputation: 61497

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.

Upvotes: 0

Roman Ryltsov
Roman Ryltsov

Reputation: 69724

You can do

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

and this will be equal to your first snippet.

Upvotes: 2

Related Questions