[C++]if statement, someting interesting.

03/02/2011 11:38 Syst3m_W1z4rd#1
I found this quiet interesting, when I was reading an article about disassembly in C++.
Quote:
One of the main statements people use is this if statement which logically compares values. Using this function we can choose which path of execution our program should take.

If statement can also be very , very complex and very simple

Take a look at the following examples.
Code:
If(I ==0)
//do function

//continue
Now what if we had something like this
Code:
If(I==0)
{
    int i2 = 0;
}
i2 = 3; //error can't access i2 because it's not in your scope

        // it's in the if statements scope
Because of this we know that compiler generates a stack frame for each If statement with brackets right? Wrong!.

I2 is accessible to main in reality but the compiler keeps it hidden, the reason I'm telling you this is because to reverse engineer if statements you must completely understand them.
I found it really interesting, because I didn't know the variable actually existed outside the if statement without being defined outside.

The original article:
[Only registered and activated users can see links. Click Here To Register...]

What do you think?
03/02/2011 11:57 Korvacs#2
Yeah, if you take a look at how an application is constructed in assembly then this becomes extremely obvious.
03/02/2011 12:09 Syst3m_W1z4rd#3
Quote:
Originally Posted by Korvacs View Post
Yeah, if you take a look at how an application is constructed in assembly then this becomes extremely obvious.
Yup, but what I found interesting was that you could access it outside the if statement, if I understanded it right.
03/02/2011 13:10 Nullable#4
It isn't that interesting if you look at the assembly output, in pseudo asm:
Code:
push ebp ;prolog, set the stack
mov ebp, esp
cmp eax, 0 ;assuming variable 'I' resides in eax
jnz if_block_end
sub esp, 4 ;number of bytes that make up an int, this reserves space for i
mov [ebp-4], 0 ;set the value of i to 0
;if block code ends here
if_block_end:
. . .
;you can access [ebp-4] anywhere here, it is still valid.
mov esp, ebp ;epilog, reset the stack pointer back to what it was before
retn ;return to the caller
So no magic, it's how things work.
03/02/2011 14:51 Korvacs#5
Quote:
Originally Posted by Syst3m_W1z4rd View Post
Yup, but what I found interesting was that you could access it outside the if statement, if I understanded it right.
Well, only outside the compiler, the compiler just ties things up with a bow for you, to make it easier for you to write an application, writing a large application like a server in assembly is certainly possible, but extremely difficult and time consuming, hence compilers exist to make it easier to create applications, thats all it comes down to really.
03/02/2011 15:40 IAmHawtness#6
What Nullable said. Also
PHP Code:
Dim Whatever As Boolean TRUE
Dim MyPointer 
As Integer Ptr

If Whatever TRUE Then
    Dim MyInt 
As Integer 100
    MyPointer 
= @MyInt
End 
If

*
MyPointer 500 
The variable is still accessible outside the scope, the compiler even allows us to access it (This is FreeBASIC. C++ and any other language that works with pointers will allow it too though)
03/02/2011 19:52 Syst3m_W1z4rd#7
Ahh I get it. Nevermind.

Thanks for clearing up.
03/04/2011 05:19 _tao4229_#8
This happens with C# and IL too.