Register for your free account! | Forgot your password?

Go Back   elitepvpers > MMORPGs > Conquer Online 2 > CO2 Programming
You last visited: Today at 01:32

  • Please register to post and access all features, it's quick, easy and FREE!

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.

Reply
 
Old   #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?
Syst3m_W1z4rd is offline  
Old 03/02/2011, 11:57   #2


 
Korvacs's Avatar
 
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.
Korvacs is offline  
Old 03/02/2011, 12:09   #3
 
elite*gold: 0
Join Date: Nov 2010
Posts: 1,162
Received Thanks: 370
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.
Syst3m_W1z4rd is offline  
Old 03/02/2011, 13:10   #4
 
Nullable's Avatar
 
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.
Nullable is offline  
Thanks
2 Users
Old 03/02/2011, 14:51   #5


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
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.
Korvacs is offline  
Thanks
1 User
Old 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)
IAmHawtness is offline  
Thanks
1 User
Old 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.
Syst3m_W1z4rd is offline  
Old 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.
_tao4229_ is offline  
Thanks
1 User
Reply


Similar Threads 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.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.