I'm trying to figure out a point of confusion. Whenever I look up the LEA address, people in explanations say that it loads the memory address of the source register, and puts it into the destination register. Hence I assume the destination register has some hex memory address, not a direct value. However, I also see LEA used for arithmetic calculations; for example -0x1(%ebx), %eax, where in this case, -1 is being subtracted from the value stored in $ebx, and that result value is getting stored into $eax. But this isn't a memory address, it's an actual calculation value? So what's happening here?
Asked
Active
Viewed 29 times
0
-
It's a shift-and-add instruction that uses the memory-operand syntax and machine encoding. (And on some CPUs the AGU hardware, but that's not important). – Peter Cordes Sep 29 '17 at 23:10
-
Would you be able to give a quick break-down of what you mean by memory-operand syntax? Like is it dealing with pointers? – Yuerno Sep 29 '17 at 23:52
-
1`-1(%ebx)` has parentheses, so it's a memory operand. For any instruction other than LEA, it would be a load or store (or both). Pointers are just integers in assembly language. – Peter Cordes Sep 30 '17 at 00:00
-
Memory address is value too. The `ebx` is 32 bit register in CPU, that means that there are physically 32 "cells" on CPU chip holding current-level indicating whether the bit contains 0 or 1 (well, I'm ignoring the fact that modern x86 CPU has many more physical registers and renaming unit which is assigning "ebx" to any of them as the need turns out, describing this from the programmer point of view, where the `ebx` is simple 32 bit register). And if you **interpret** those 32 bits as integer value using powers of two for successive bits, you get values from 0 to 2^32-1 (works as address too) – Ped7g Sep 30 '17 at 08:12
-
1And as you can see, I didn't mention "hex" anywhere. Because it's not there anywhere. "hex" is formatting of value in text for humans, the compiler will translate "mov ebx,0x05" string into bits encoding the `mov ebx,imm` instruction plus the immediate value ending with ..0101 bits. Writing `mov ebx,5` would encode in exactly the same bit pattern. And when you get "hex" content of registers in debugger, that's formatting of output, creating `5F` string on screen from bit pattern `01011111`. The value itself is independent of formatting, formatting is decided by the outputting code. – Ped7g Sep 30 '17 at 08:16
-
Thank you for the explanations, I appreciate it! – Yuerno Sep 30 '17 at 13:46