Tuesday, August 17, 2010

Switching To Mode 13h (In Practice)

Start at the Beginning
Table of Contents

The following is a complete C program that switches into mode 13h then back into text mode. Anything between /* and */ is a comment - the compiler ignores it. We use it to describe exactly what the program is doing when the code itself is unclear. Read the following program closely, including all the comments.

(click here for setmode.c)

Type the above program out in RHIDE and save it to a file called setmode.c. You don't need to copy the comments - in fact, a better thing to do is replace my comments with your own, explaining your own understanding of the code. This will force you to think hard about it.

Now we're going to 'compile' our code. Compilation is the process of taking source code, analyzing it, and turning it into machine code the computer can load and execute. As our program gets more complex, we'll need to have a more involved compile step. For now, it should be fairly simple.

gcc setmode.c -o setmode.exe

gcc is the name of DJGPP's compiler. setmode.c is the name of the file you want to compile, of course. The -o stands for output - it tells GCC where to output the result of compilation.

If it worked, GCC won't display anything. If it outputs an error, check your code, find the mistake, and try again. GCC's error messages tend to be cryptic, but they do tend to lead you in the right direction. To see what I mean, let's fake an error message. Go to line 55:

set_video_mode(TEXT_MODE);

and change it to read

set_video_mode(TXT_MODE);

That is, remove the E in TEXT.

You'll get the following output:

setmode.c: In function 'main':
setmode.c:55: error: 'TXT_MODE' undeclared (first use in this function)
setmode.c:55: error: (Each undeclared identifier is reported only once
setmode.c:55: error: for each function it appears in.)


Now, the first thing to understand is that this is all one error message. The first line tells you the following errors are all inside main(). The next three lines are just GCC splitting the error message up. So the error really is:

setmode.c:55: error: 'TXT_MODE' undeclared (first use in this function) (Each undeclared identifier is reported only once for each function it appears in.)

So this should be clear. The error is in video.c on line 55. The problem is GCC doesn't know what TXT_MODE is. It's undeclared, because we didn't declare TXT_MODE - we declared TEXT_MODE! You need to be very precise with computers. Change TXT_MODE back to TEXT_MODE and it should compile without a hitch - as long as you don't have any errors of your own to fix.

Running the program - by typing setmode at the DOS prompt - should do pretty much nothing (it'll clear the DOS back buffer, actually). That's because all our program does is change video modes and instantaneously switch back. It doesn't wait around in the new video mode or draw anything to the screen in the new video mode. We'll get there soon.

No comments:

Post a Comment