0

I am solving a practice paper on assembly language, and in the paper, there is a question as follows on assembly language(sorry I am only able to include a picture of the assembly instructions as this is printed on a paper).

I am specifically quite unsure about what the instruction with address 0x4006f5 doing.

Up till that instruction, I can understand that out of the array of numbers we have given as input, the code is summing certain elements and comparing them to 50. Up till that particular instruction is executed for the first time, I have deciphered that the code has currently stored the sum of the first element, last element and 1 in the stack, after the last entry of the array numbers[] (on the position %rsp+24). I am also able to understand that up till this instruction, we now have the sum of the first, last and third elements and 1 in the register %rdx(which, at the moment is such that %rdx points at %rsp+24, from what I could understand). So, I am confused about why we are doing this lea instruction. Won't it just overwrite the sum we just got in %edx?

enter image description here

Jester
  • 56,577
  • 4
  • 81
  • 125
user202004
  • 151
  • 8
  • 3
    `lea` really just does arithmetic. In this case it is simply doing `edx = edx + eax + 2`. (Remember `edx` is the low half of `rdx`. It really does `edx = rdx + rax + 2` but throws away the high 32 bits, so equivalent to `edx = edx + eax + 2`.) Yes, it does overwrite edx, or rather it modifies it in place. So now I guess edx contains first + last + third + 1 + whatever was in eax + 2. Why that arithmetic operation is useful, is a separate question for you to answer. – Nate Eldredge Oct 20 '21 at 14:25
  • Ah alright. I have one more question then. I may have interpreted this incorrectly, but then won't it overwrite the two LSB bytes of the value stored from %rsp+24 onwards? I don't think that is supposed to happen in the function however, which is why I am proceeding with this assumption very cautiously. – user202004 Oct 20 '21 at 14:29
  • 1
    `lea` does not write to memory. The calculated value is written into the array by the next `mov` which does a full 4 byte write. – Jester Oct 20 '21 at 14:32
  • 2
    No, the `lea` instruction won't write to memory at all. It only writes to the `%edx` register. I'm not sure what you mean by your comment "%rdx points at %rsp+24". The value in a register might be a pointer (though at this point it probably isn't), but that doesn't mean the register somehow becomes an alias for a memory location. The register is always its own piece of storage; if you want to store to the memory that the register points to, you need a separate instruction like `mov %reg, (%rdx)`. It seems like you might have got your levels of indirection muddled somewhere. – Nate Eldredge Oct 20 '21 at 14:33
  • For reference, we are supposed to write the C code for read_six_numbers function. The only information we have, is that we passed a character pointer named input, and an array of six numbers called numbers to the read_six_numbers function. – user202004 Oct 20 '21 at 14:33
  • 1
    `read_six_numbers` is trivial. It's `sscanf(input, "%d %d %d %d %d %d", numbers, numbers+1, numbers+2, numbers+3, numbers+4, numbers+5);` Has very little to do with the listing shown in the question. Even less to do with finding the correct numbers that defuse the bomb :) – Jester Oct 20 '21 at 14:38
  • 3
    Also note that `LEA` doesn't set flags, as those are irrelevant for addresses. This can affect control flow afterwards – Mgetz Oct 20 '21 at 14:47
  • Thanks a lot for all the help everyone! I think I am starting to get a more clear picture of what it is doing now and where I was wrong.. – user202004 Oct 20 '21 at 14:52
  • Don't post pictures of text. Use code formatting to copy/paste the disassembly. Also, what LEA is doing here is basically answered by [Using LEA on values that aren't addresses / pointers?](https://stackoverflow.com/q/46597055) so I closed it as a duplicate. – Peter Cordes Oct 20 '21 at 22:37

0 Answers0