* We will type G to run the program until it fails
pause
g
* There seems to be a problem on line 198 of module kaboom1.
* Let's look at BOARD's definition to check its size
pause
x board 
* Ok, BOARD is 15 by 9
* Let's check I and J to see if they might be out of range
pause
v i
pause
v j
pause
* The _are_ out of range, so it might be a problem with
* the random number generation function.  Let's set the
* source mode to mixed and then start back at line 196 and
* step over a call to nRandom to see what value it returns.
pause
l #196
src both
pause
p =#196
pause
p
* We can see in the register window that the nRandom
* routine returned a big number in EAX.  It was supposed to
* be a number between 0 and 14.  The problem must lie
* in that routine.  Let's bring up its source
* code in the source window.
pause
src src
pause
l nRandom
pause
* We can't see the end of the line in nrandom.  We can use
* the offset command to shift the source window to the left
* by 8 characters.
pause
offset 8
pause
* That initialization of maxint to 0x7fff looks suspicious.
* In 32-bit protected mode, ints are 32-bits wide so we appear
* to have a portability problem.  In order to scale the return value
* from the rand routine to between zero and one, we must divide
* by 0x7fffffff, not 0x7fff.  Let's patch the value of maxint
* to 0x7fffffff and start at line 196 again.
pause
ed $kab2bug.nrandom.maxint 0x7fffffff
pause
v $kab2bug.nrandom.maxint
pause
offset 0
src both 
pause
p =$kaboom1#196 
pause
p 
pause
* Ok, the return code from nRandom in EAX looks good.
* It looks like nRandom is working correctly now.  Let's
* see if the program works now.  We'll switch the display
* back to source mode let the program continue from here.
pause
src src
pause
g
* Uh, oh.  We got a memory protection fault.  But where are
* we?  Let's use the KA command to do a stack backtrace.
pause
ka
pause
* There appears to be a problem in the DisplayChar routine.
* Our arguments look good, though.  Let's look at the value
* of the cPos pointer to see if it is valid.
pause
v cPos
pause
* There is the problem!  A segment selector value of B800
* is illegal in protected mode.  We should instead use selector
* 001C to reference the screen memory.
pause
* We can patch the scr_sel variable to 001C.
pause
ew scr_sel 1c
pause
v scr_sel
pause
* Let's continue starting back at line #96 and see if the
* program works now.
pause
g =#96
* Ok, we are all done.  You can terminate the debugging session
* with the Q command.
