Declares a variable command

Declares / assigns a variable in a high-level language such as C ++, an explicit instruction?

eg. x = 5;

It will be processed by the bootloader and processed as status information, right?

This is not an instruction, but a state object, unlike something like a for loop, which is an instruction that makes it a way for the processor?

edit: OK to clarify a bit more. I am not talking about optimization. Suppose no. I am talking about the end result of a compiled program in executable file format. In conditions when the compiler decides not to use the MOV instruction, will data 5 exist in the data segment of the executable files or somewhere else?

Is it possible for 5 to exist as data without being an instruction, and that data is later loaded into memory? Or, in general, x = 5 leads to the mov command when the program runs.

+4
source share
8 answers

If your variable is a primitive type (int, char, etc.):

For a global or static variable no. This is just an entry in the BSS or DATA segment (depending on whether it was initialized or not), no executable code is required. Except, of course, if the initializer needs to be evaluated at runtime.

For a local variable, if it is not initialized, usually the first implies an assembly instruction, the others do not. This is because the space allocation for them is usually done by adding an offset to the stack pointer (in fact, subtraction - the stack grows backward). When you declare your first int variable, "ADD SP, 4" is generated; for the second, he just changed to "ADD SP, 8". This instruction will not be in the place where you declare your variable, but instead at the beginning of the function, because there should be allocated the whole stack space for local variables.

If you initialize a local variable at creation, then you will have a MOV command to load the value into its location on the stack. This instruction will be in the same place as the declaration regarding the rest of the code.

These rules for local variables do not imply any optimization. One of the common forms of optimization is the use of CPU registers as variables, in this case distribution is not required, but initialization will generate an instruction. In addition, sometimes these registers must retain their values, so you will see the PUSH instruction at the beginning and POP at the end of the function.

The rules for objects when the constructor is not involved (or the constructor is nested) are much more complicated, but the same logic is used. If you do not have a built-in constructor, you will need at least instructions to call it.

0
source

You ask whether a variable declaration will translate into an assembly statement in the same way as adding or removing? If so, then the general answer is that there is no direct translation into the assembly.

There may be instructions that facilitate declaring a variable, such as updating the stack pointer to make space for a variable. But there is no x86 build instruction that says declares a variable of this type on the stack.

Most of my experience is with x86 and amd64 chips, so there may be instructions for other processors, but I do not know about them (but it would be interesting to read about them if they really exist).

The purpose of the variable is slightly different. the general case x = 5 translates into some type of assembly instruction (for example, writing a value to a register). However, it is very difficult to be specific with C ++, because there are many optimizations and special settings that can lead to the fact that a particular line does not have a translation into machine code.

+4
source

It depends on how it is used.

Typically, the compiler will try to make each line as possible as possible.

If it is used in only one place, you can put your buttons, which are hardcoded into machine code, and not waste space on the stack.

If it is used for mathematical or algorithmic operations, and its value can change, space for the variable can be allocated on the stack. Again, if it is used often enough, the compiler can simply leave it in the register.

Answer: it depends. Compile it and view the result using the debugger machine code window.

One possible actual translation:

MOV AX, 5 
+3
source

Well in assembly language, usually the mov command will be used to load 5 into a register, a stack variable, or a heap variable representing x.

But the optimizer may decide to use this as a constant value (for example, #define), or he may decide to completely remove it if it is not used, and he decides where to put it (register, stack variable, heap)

So, I hope it answers your question, but as you can see, assigning a variable in C ++ abstracts a ton of things and that’s very good!

FYI: I learned tons of compiler assembly builds. In this post, the assignment operation is optimized in the build operation OR, which is very good: What is more readable (C ++ =)

+1
source

It depends on the compiler and on its optimizations performed. If it performs the destruction of dead storage, then it may well omit the emitting assembler code for the variable. Consider

 for(.....) i+= n; i = 1; return i; 

It can be easily optimized for this, since i records will be overwritten by a later assignment

 i = 1; return 1; 

And if assignment 1 to I did not happen, and the loop would execute m times, the compiler could optimize it for

 i += n * m; return i; 

Including the optimizing increment i completely (and in the previous example, too) if i is local and will not change any global state. If i has a mutable qualified type, the compiler should omit these optimizations. He must take every step as described in the language description. But even then, different compilers could generate different assemblers / instructions depending on the capabilities of the target processor.

+1
source

In general, you may not know how the compiler is going to translate your C ++ instructions into assembly instructions. From a C ++ point of view, declaring a variable that includes an assignment (for example, "int x = 5;") is an instruction. If you run your program in the debugger, it will stop at that line. But who knows what the compiler will do with it. (The whole variable can be optimized for everyone that you know.)

+1
source

Exactly how much is done at compile time, link time, load time and runtime will depend on all kinds of things. The implementation is not even guaranteed that there is anything in memory that is identified as x. The C ++ standard describes some memory layouts and runtime restrictions, but it does this only for specifics: it also defines observable behavior and explicitly indicates that an implementation can do as it wishes if all observable behavior matches (as-if rule ").

This means that the only reasonable answer will be based on what implementations really do, and the answer is that it depends.

Why do you want to know if a particular line of code translates into a specific machine code? If you make this clear, perhaps we can answer a question that is really useful to you.

+1
source

You did not specify what type x is. If x is not a pod type and has a constructor that accepts int than

 x = 5 

may have other effects than β€œstore 5 somewhere,” as the syntax seems to imply.

 #include <iostream> int i; class X { int _y; public: X(int y) : _y(y) { i ++; // changes a global variable std::cout << "got " << y << std::endl; // does IO }; }; 

Now

 X x = 5; 

has a completely different meaning. Therefore, I would say that in the general case in C ++ x = 5, this statement is like any other, since it can have any side effects (for example, in my example: changing the global state or doing IO).

+1
source

All Articles