Possible Duplicate:
What’s the purpose of the LEA instruction?
LEA instruction?
So I'm working on the binary bomb assignment for class (it has a bunch of phases where you have to step through the assembly code of a program and find a passphrase to decode the "bomb").
I can't complete my current phase because I don't understand the lea command. I've read that it's commonly used for arithmetic, but I just don't understand how it does it.
The command I'm looking at in particular is
lea -0x18(%ebp), %ebx
lea -0x8(%ebp), %esi
followed by a
mov -0x4 (%ebx), %eax
add -0x8(%ebx), %eax
in the next line eax and ebx are compared, if they're equal the program continues, else the bomb explodes.
I've figured enough out about this phase to know it wants 6 numbers, the first two being 0 and 1. After that it does some manipulations to determine if the rest of the sequence is correct (I'm assuming the lea commands are what i need to decode to find the next numbers).
Now what I couldn't find is what the -0x18 in particular refers to. what's the negative sign do? does it indicate subtraction? is it looking 18 bytes before ebp?
Thanks for any help here.
Load Effective Address calculates its src operand in the same way as the mov instruction does, but rather than loading the contents of that address into the dest operand, it loads the address itself.
The effective address is the location of an operand of the instruction, since the operand is the data to be accessed. Immediate instructions use their operand to hold the data needed to complete the instruction.
The lea instruction copies an “effective address” from one place to another. Unlike mov, which copies data at the address src to the destination, lea copies the value of src itself to the destination.
The DUP directive tells the assembler to duplicate an expression a given number of times. For example, 4 DUP(2) is equivalent to 2, 2, 2, 2. Some examples: Z. DD 1, 2, 3.
The LEA instruction computes a memory address using the same arithmetic that a MOV instruction uses. But unlike the MOV instruction, the LEA instruction just stores the computed address in its target register, instead of loading the contents of that address and storing it.
Consider your first LEA instruction:
lea -0x18(%ebp), %ebx
This instruction computes the sum of -0x18 and the value in the EBP register. It gets some result S. It stores S in the EBX register.
In the addend -0x18, the “-” is a negative sign and the “0x” means it's a hexadecimal constant. So the addend is negative 1816, which is -2410. So this LEA instruction simply subtracts 24 from the value in EBP and stores the result in EBX.
Contrast this with your MOV instruction:
mov -0x4(%ebx), %eax
This instruction computes the sum of -0x4 and the value in the EBX register. It gets some result S. Then it fetches the value of the word at address S in memory, getting some value M. It stores M in the EAX register.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With