# How does "mov eax, [ebp + 8]" calculate ebp+8?

Hey,

I'm trying to learn Assembly a bit (and I know I really should get a good non-electronic book on it), and I was wondering about the following.

Consider an instruction like:
[code]
mov eax, [ebp + 8]
[/code]
How does the Assembler translate ebp+8 into the MOV instruction (basically, how does it turn it into machine code)?

The reason I am wondering is because it cannot do:
[code]
mov eax, ebp + 8
[/code]
You would need to use
[code]
lea eax, [ebp + 8]
[/code]

I guess I'm a bit confused about why the one thing can be done and the other can't.

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry

• : Hey,
:
: I'm trying to learn Assembly a bit (and I know I really should get a
: good non-electronic book on it), and I was wondering about the
: following.
:
: Consider an instruction like:
: [code]:
: mov eax, [ebp + 8]
: [/code]:
: How does the Assembler translate ebp+8 into the MOV instruction
: (basically, how does it turn it into machine code)?
:
: The reason I am wondering is because it cannot do:
: [code]:
: mov eax, ebp + 8
: [/code]:
: You would need to use
: [code]:
: lea eax, [ebp + 8]
: [/code]:
:
: I guess I'm a bit confused about why the one thing can be done and
: the other can't.
:
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry
Doesn't it use one of the addressing modes to do that? Sorry, don't know that much about assembly.
• k, I'll try to clear things up a bit.

LEA (Load Effective Address) is an excellent instruction for asm coders, because it is practically the only instruction that allows you to do multiple calculations in one instruction while the calculations may have nothing to do with calculating an address. Notice that unlike other instructions, using this one does not access any memory although it does use addressing. You can only use those multiple additions with addressing, not anywhere else.

Just a bit of extra information - addressing allows you to add up 4 items:
the [color=Blue]base[/color] register, the [color=Green]index[/color] register with the [color=Red]multiplier[/color] of the index(2,4 or 8) and a [color=Purple]constant[/color]:

[code]lea eax, [leftbr][color=Blue]ecx[/color] + [color=Green]edx[/color]*[color=Red]4[/color] + [color=Purple]1000h[/color][rightbr][/code]
I'd recommend that you get a good debugger, that way you can type in asm instructions and see the matching machine code next to it. I like to use [link=http://www.ollydbg.de]OllyDebug[/link], but you can use whichever you find best :-)
• : k, I'll try to clear things up a bit.
:
: coders, because it is practically the only instruction that allows
: you to do multiple calculations in one instruction while the
: calculations may have nothing to do with calculating an address.
: Notice that unlike other instructions, using this one does not
: access any memory although it does use addressing. You can only use
:
: Just a bit of extra information - addressing allows you to add up 4
: items:
: the [color=Blue]base[/color] register, the
: [color=Green]index[/color] register with the
: [color=Red]multiplier[/color] of the index(2,4 or 8) and a
: [color=Purple]constant[/color]:
:
: [code]: lea eax, [leftbr][color=Blue]ecx[/color] + [color=Green]edx[/color]*[color=Red]4[/color] + [color=Purple]1000h[/color][rightbr][/code]:
: I'd recommend that you get a good debugger, that way you can type in
: asm instructions and see the matching machine code next to it. I
: can use whichever you find best :-)

Thanks for the explanation.

I think I get it now. I might try Olly out some day

Best Regards,
Richard

The way I see it... Well, it's all pretty blurry