0

I'm solving a school assignment and so far everything has been quite simple. However, I encountered the following piece of code:

mov 0x8(%ebp), %eax    ;load pointer function argument into eax
mov (%eax), %eax       ;dereference the pointer
test %eax, %eax        ;
sete %dl               ;test if it was 0
lea 0x1(%eax), %ecx    ;???
mov 0x8(%ebp), %eax    ;load the argument again
mov %ecx, (%eax)       ;store whatever is in ecx on the pointed-to address
...

I'm really at my wit's end here, does the register have an address? Way I see it, the lea would store the address of eax (offset by 1) into ecx, but that doesn't make sense, does it?

rkhb
  • 14,159
  • 7
  • 32
  • 60
  • Without knowing the high-level code this assembler corresponds to, it's going to be difficult to figure out the purpose... – Oliver Charlesworth May 17 '14 at 14:26
  • Indeed, what that `lea` does is `ecx = eax + 1`. Whether or not that makes sense depends on what the code is supposed to do, which isn't particularly clear from the information provided. – Michael May 17 '14 at 14:33
  • Well that's kind of the point of the task, to figure out what the code does. This is just a snippet of what I thought was necessary to figure out what the lea instuction does. – user2524502 May 17 '14 at 14:33

1 Answers1

0

lea 0x1(%eax), %ecx loads the register %ecx with %eax + 1. The lea instruction is commonly used for arithmetic computations, thanks to the relatively sophisticated addressing modes of this instruction set.

If you do not identify %eax as an address, this is likely what is happening here (as Oli says, we don't have enough context to tell).

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • That might just be it! So if we had 3 operand instructions, this would be somewhat equal to addi ecx, eax, 1? – user2524502 May 17 '14 at 14:38
  • @user2524502 Yes, exactly. Also `lea`, unlike `add`, does not affect the flags, which is sometimes useful to remember when hand-writing assembly. This sometimes allows sequences like `add …; lea …; lea …; jne … /* branch depending on the result of add */` – Pascal Cuoq May 17 '14 at 14:39