|
You last visited: Today at 01:32
Advertisement
[C++]if statement, someting interesting.
Discussion on [C++]if statement, someting interesting. within the CO2 Programming forum part of the Conquer Online 2 category.
03/02/2011, 11:38
|
#1
|
elite*gold: 0
Join Date: Nov 2010
Posts: 1,162
Received Thanks: 370
|
[C++]if statement, someting interesting.
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:
What do you think?
|
|
|
03/02/2011, 11:57
|
#2
|
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
|
Yeah, if you take a look at how an application is constructed in assembly then this becomes extremely obvious.
|
|
|
03/02/2011, 12:09
|
#3
|
elite*gold: 0
Join Date: Nov 2010
Posts: 1,162
Received Thanks: 370
|
Quote:
Originally Posted by Korvacs
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
|
#4
|
elite*gold: 0
Join Date: Nov 2009
Posts: 390
Received Thanks: 321
|
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
|
#5
|
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
|
Quote:
Originally Posted by Syst3m_W1z4rd
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
|
#6
|
elite*gold: 20
Join Date: Aug 2007
Posts: 1,749
Received Thanks: 2,199
|
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
|
#7
|
elite*gold: 0
Join Date: Nov 2010
Posts: 1,162
Received Thanks: 370
|
Ahh I get it. Nevermind.
Thanks for clearing up.
|
|
|
03/04/2011, 05:19
|
#8
|
elite*gold: 0
Join Date: Jun 2009
Posts: 787
Received Thanks: 314
|
This happens with C# and IL too.
|
|
|
 |
Similar Threads
|
WTT 90 Char on SRO for someting in ZSZC Water
10/31/2010 - Silkroad Online Trading - 0 Replies
WTT 90 Char on SRO for something in ZSZC Water (items and others NO CHARS)
Char pure STR glavie,f/f (lightning fire heuskal) full+5 full blue.A lot of avatar items,pet (low level but..)
No guild,contact me on IM or Skype.(im zzerolegend skype max.archer) char is on server
Oasis and has 20 M i think...i dunno :P
:handsdown: waiting:rolleyes:
|
WTT Steam Account For Someting On Fembria
03/01/2009 - Silkroad Online Trading - 1 Replies
Hey guys!
I'm here to offer you a steam account because i don't use it at all anymore. I"m looking for something on the fembria server, since they don't let me post a thread for it on the forum because it is a, account trade and they don't accept it. The account has only 4 games on it but they're nice! I have Counter Strike Source, Day OF Defeat Source, Half Life 2 Deathmatch and Half Life 2 Lost Coast. I really want to trade the account because i'm not using it and it's a shame no one uses...
|
{REQUEST} any1 have melee attack speed(item.edf or someting) pls share here
05/05/2008 - RF Online - 1 Replies
{REQUEST} any1 have melee attack speed(item.edf or someting) pls share here
|
All times are GMT +1. The time now is 01:32.
|
|