0
0
0x0000002F2022-01-07 09:28:14
Programming
0x0000002F, 2022-01-07 09:28:14

How do variables work in a low-level concept?

I know what a variable is. This is a kind of named "container" for storing information and getting it by name. But I don't quite understand how it works at the low level of the language. That is, when writing and reading a variable by name (for example, the classic myVar), how does the language understand that this is exactly that and not another variable? Perhaps he googled badly.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
dollar, 2022-01-07
@0x0000002F

If you do not delve into the registers and the subtleties of addressing, then the variables are stored in memory at certain addresses . This is their whole point.
The variable name exists only at the programming language level. Although it depends on the language. In scripting languages, for example, the name is also stored in memory, but these are already nuances. In a compiled language, a variable name is needed only for clarity and understanding of the program logic, and during compilation this name is turned into an address.
A finished compiled program (for example, an exe file) is executed by a processor that is not aware of any variables. For the processor, there are simply instructions by type to write such and such a value to such and such an address, etc.
By the way, programming languages ​​are designed to be convenient and readable for humans. This format is not very convenient for a car. Therefore, the text of the program is translated into machine code (or into bytecode in the case of scripting languages), and only then is it executed at a lower level. Thus, the programming language itself does not do or understand anything. It's just a set of rules on how to describe your algorithms. The programmer writes a program, therefore he feeds this program to the compiler (or interpreter), translating it into code, and then the processor works with the code. Something like this.
To really understand, you need to get acquainted with the assembler. IMHO, it is very useful to go through the basics of assembler at the beginning, so that later you can approach the study of higher-level programming languages ​​with understanding.

W
Wataru, 2022-01-07
@wataru

There are many levels here.
The simplest thing is that the compiler remembers that myvar is a variable that lies right there in memory. Roughly speaking, all these variable names are aliases for memory addresses. If he sees in the code myvar = a + 1;, then he generates an instruction that reads from the fixed address of the variable a, adds 1, and stores the result in the address for the variable myvar.
Things get a little more complicated because of optimizing compilers. They may not keep some variable in RAM at all, but only in the form of a processor register.

S
SOTVM, 2022-01-07
@sotvm

processor registers
are not there :)
there is only 0 | 1 in the specified address,
if you pull the owl on the globe, then the assembler is closest to this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question