Monday, August 16, 2010

Switching To Mode 13h (In Theory)

Start at the Beginning
Table of Contents

To switch into Mode 13h, we load register AH with 0 to specify a video mode switch; load register AL with 0x13, the video mode we want to switch into; and then call interrupt 0x10, which handles graphics functions. Simple, right?

Not really. To understand what all this means, you need to understand a bit about CPUs. Basically, the CPU can only operate on data that it has loaded into tiny, special pieces of onboard (i.e. on the CPU) memory called registers. To execute a simple line of code like

y = y + 5;

the CPU will first load the contents of y from wherever it's stored in memory into a register, add 5 to it, and then copy it back to the memory location that corresponds to y.

Two of the registers on x86 CPUs (the type of CPUs in most desktops these days, and the type of CPU we're programming for) are called AH and AL. They each hold a single byte. They're actually each one half of a larger register called AX. There's also BX, CX, DX (and their respective half-registers), SP, SI, DI, BP, CS, DS SS, ES...and so on. The world of low-level coding on the x86 architecture is deeply messy and unpleasant. Be happy your compiler takes care of shuffling data from memory to the registers and back again.

When you send an 'interrupt' to the CPU, the CPU stops whatever it's doing and executes whatever code is assigned to that interrupt. Most code these days doesn't ever need to deal with this, but it's a useful way to give programs access to system functions. The BIOS is a set of functions like this, stored in ROM on the motherboard, that are accessed via interrupt. Interrupt 0x10 is reserved for graphics functions - the precise function selected is determined by whatever number is in AH. 0 is the "switch video mode" function. It switches the computer to whatever video mode corresponds to the number in AL. Since we want to switch to mode 13h, we store 0x13 in AL.

No comments:

Post a Comment