Hello there.
I don't know if this should actually be posted in any of the C/C++ forums, but it is mainly an Assembly question so I take my chances here.

In C/C++ it is possible to declare local variables as:

if(statement){
    int var;
    // Use of the variables
}else{
    // Not using var
}

Compared to

int var;
if(statement){
   // Use of the variable
}else{
   // Not using var
}

But when we in class learn to translate C into assembly we have to declare all variables in a prologe (using LEAS). So my question is how the above examples would translate into assembly, is there any difference?

Re: C/C++ local variables in Assembly 80 80

Most/all C/C++ compilers have a swtich to allow you to view the assembled code

gcc's switch is -S

`-S'
Stop after the stage of compilation proper; do not assemble. The
output is in the form of an assembler code file for each
non-assembler input file specified.

Re: C/C++ local variables in Assembly 80 80

Your compiler will have read the entire function, and worked out in advance just how much space each local variable would need (and the total space).

At code generation time, the right amount of stack space can be reserved.

Re: C/C++ local variables in Assembly 80 80

Ok, I see. Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.