I'm a little confused about how variables are implemented in Assembly. I know that the following declaration creates a named memory location in the program's data
section:
.section .data
var:
.long 23
Then you can access the variable such as:
movl var, %eax //read from var location
What I'm not clear on is how the above instructions are actually implemented. I was under the impression that a memory reference, even an absolute memory reference, needed to be loaded into a register before it could be used.
Does the assembler need to convert the above instruction into multiple instructions, such as:
leal var, %ebx
movl (%ebx), %eax
Or does the assembler keep track of the address of var
and access that location as though it were an absolute immediate access, e.g.:
movl ($addr_of_var), %eax
The x86 instruction set does not require memory addresses to be loaded into registers before they can be used -- pretty much all instructions can use direct or indirect memory references in the instruction. See the Intel architecture manual for more details.
The requirement to load addresses into registers first is a characterisic of some RISC cpus, like MIPS.
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