Game Development Reference
In-Depth Information
from,ifthesourceisavailable.Youcanseethatevenaclosebracecanhaveassembly
instructions, usually to return to the calling function or to destroy a C++ object.
The first lines of assembly, pushing various things onto the stack and messing with
EBP and ESP, establish a local stack frame. The value 8E8h is the size of the stack
frame, which is 2,280 bytes.
Check out the assembly code for the for loop. The beginning of the loop has seven
lines of assembly code. The first two initialize the loop variable and jump over the
lines that increment the loop variable. Skip over the guts of the loop for now and
check out the last three assembly lines. Collectively, they call the destructor for the
MyClass object and skip back to the beginning part of the loop that increments
the loop variable and performs the exit comparison.
ve ever wondered
why the debugger always skips back to the beginning of for loops when the exit con-
dition is met, there ' s your answer. The exit comparison happens at the beginning.
The inside of the loop has two C++ statements: one to construct the MyClass object
and another to call strcat() . Notice the assembly code that makes these calls work.
In both cases, values are pushed onto the stack by the calling routine. The values are
pushed from right to left, that is to say that the last variable in a function call is
pushed first. What this means for you is that you should be mindful of setting the
next statement. If you want to skip a call, make sure that you skip any assembly
statements that push values onto the stack, or your program will lose its mind.
One last thing: Look at all the code that follows the closing brace of SetTheIP() . There
are two calls here to CheckStackVars() and CheckESP() . What the heck are those
things? These are two functions inserted into the exit code of every function in debug
builds that perform sanity checks on the integrity of the stack. You can perform a little
experiment to see how these things work. Put a breakpoint on the very first line of Set-
TheIP() , skip over all the stack frame homework, and set the next statement to the one
where the buffer gets initialized. The program will run fine until the sanity check code
runs. You
If you
'
'
ll get a dialog box telling you that your stack has been corrupted.
It
s nice to know that this check will keep you from chasing ghosts. If you mistakenly
screw up the stack frame by moving the instruction pointer around, these sanity
checks will catch the problem.
'
Peppering the Code
If you have an elusive bug that corrupts a data structure or even the memory system,
you can hunt it down with a check routine. This assumes that the corruption is
somewhat deterministic, and you can write a bit of code to see if it exists. Write
this function and begin placing this code in strategic points throughout your game.
 
 
Search WWH ::




Custom Search