Question for addressings like [ebp*2+edx+50]

Programming, for all ages and all languages.
Post Reply
User avatar
~
Member
Member
Posts: 1226
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Question for addressings like [ebp*2+edx+50]

Post by ~ »

Does something like [ebp*2+edx+50] gets calculated the same for the LEA instruction and for MOV or the rest of 32-bit instructions? From what the manuals say, it seems like that.

Also, how is that address calculated actually?
Does something like:

[ebp*2+edx+50]

Assuming that:

Default segment == always DS, except when the "base" register is EBP or ESP (in this case would be EDX)?
EBP = 3
EDX = 4

Would that be calculated like this?:

(3*2+4+50) == DS:60 -- (with cyclic 32-bit numbers if they overflow when added)

I have been trying to inspect the Bochs memory and seems that when EBP or ESP are the "base register" (e.g. if it instead was something like [ebp+edx*2+50] or [esp+edx*2+50]), the SS segment is used, LEA seems to work as described, and I'm fairly sure about everything else I have pointed out.

Is it correct?
YouTube:
http://youtube.com/@AltComp126

My x86 emulator/kernel project and software tools/documentation:
http://master.dl.sourceforge.net/projec ... 7z?viasf=1
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Question for addressings like [ebp*2+edx+50]

Post by bewing »

Yes, you basically have it right. That is the point of the LEA opcode, is to give you access to the values calculated by those addressing modes. The one thing you may not have right is that LEA stops before doing anything with segments. It just calculates the values inside the brackets.

If you are using any other opcode than LEA, then you are correct about the DS: and SS: segment usage.

LEA is nice for calculating some "multiples" in place. That is: LEA EBX, [EBX+ 2*EBX]
is an easy and really fast way to multiply by 3. 3, 5, and 9 become very interesting multipliers.

No EFLAGS bits are ever set with LEA, and in general you need to be careful about overflows when using that sort of 32bit/extended addressing.
But this is also nice if you are trying not to mess up your current EFLAGS settings.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Question for addressings like [ebp*2+edx+50]

Post by qw »

The general form is [BASE + INDEX * SCALE + DISPLACEMENT]. In your example the base is EDX because EBP is scaled. If the scale is omitted, [ebp + edx + 1234] and [edx + ebp + 1234] look the same, but the assembler will probably assume that the base is EBP in the first case (SS-relative) and EDX in the latter (DS-relative) because the first register is usually the base. GAS' syntax is not this ambiguous.

And like Bewing said, LEA calculates the offset only. Segment registers are used only when actually accessing memory.
geppyfx
Member
Member
Posts: 87
Joined: Tue Apr 28, 2009 4:58 pm

Re: Question for addressings like [ebp*2+edx+50]

Post by geppyfx »

General Programming forum, at the very least
Post Reply