Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are assembly variables implemented?

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
like image 611
David Avatar asked Feb 06 '23 23:02

David


1 Answers

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.

like image 78
Chris Dodd Avatar answered Mar 07 '23 08:03

Chris Dodd