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?

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.

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.

Ok, I see. Thank you very much.

Be a part of the DaniWeb community

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