Chapter 10: Beginner's Guide to Interrupts

10.1 Introduction
Since interrupts are one of the more advanced topics in BASIC programming, or programming in general, I'll start slow. First of all, what are interrupts? Well, to describe it lightly, interrupts are specific routines that are called to access the hardware/software. This is only a vague description, so don't quote me. Also note, most of what I discuss will be related to the QB4.5 compiler, for QBasic users, there's an interrupt emulator available which will work just as fine.
10.2 The QBasic Interrupt emulator?
Please see INTERRPT.ABC for some samples, they all work just as well. The "emulator" pretty much mimics what QB4.5 offers, so you won't need to do any modifications. In QB4.5, you need to invoke the compiler with QB /L What this does is load QB.QLB which contains the interrupt routine.
10.3 The AL, AH and AX story...
What is really annoying is the fact QB does not support AL and AH directly. Before I go on, AX is just one of several registers, which consists of an 8-bit low address (AL) and a 8-bit high address (AH).
       So AX = 0 0
               ^^^^^^^^ ^^^^^^^^
                  AH       AL
So you must be thinking, why do we care to split AX into AH and AL? Well, for one thing, it would make life a lot easier, this will become apparent once you do some examples of your own. In QB, AH and AL must be combined to form AX, which is both annoying, yet fairly trivial.
10.4 Putting things together
Okay, so where are we leading up to? Since we're talking about registers, that means Interrupts must read these registers for the required instructions. Every Interrupt is assigned an interrupt vector, you can think of this as just a number (let's not get ourselves too deep into this). This "number" is stored in AX, so now we're getting somewhere. How do we find this "number?" Well, that's a good question, ever heard of Ralf Brown's Interrupt List? Perhaps the most comprehensize interrupt list on the internet today. Aside from that, you can always find a good assembly book to get a short list of interrupts (most assembly books include them).
10.5 Simple Examples
Okay, enough of the boring technical stuff, let's get to the "nitty gritty". I'll assume you have some knowledge about bits and bytes, but I will try to cover as much as possible.
INT 10 - VIDEO - SET VIDEO MODE
        AH = 00h
        AL = desired video mode (see 10.6)
Return: AL = video mode flag (Phoenix, AMI BIOS)
            20h mode > 7
            30h modes 0-5 and 7
            3Fh mode 6
You can find this information in Ralf Brown's Interrupt List, but what does it mean? INT 10h is the interrupt for VIDEO. What VIDEO? Just general VIDEO routines, like setting the mode, writing to video, etc. All in one interrupt you ask? This may confuse you, I don't know, but this is the interrupt, now you have to specify the service routine "number." Remember that "number" we're talking about, well AH and AL is that "number." We just need to combine them to form AX. Oh yes, by the way, 10h is a hexidecimal number, in BASIC we'd write hexidecimal numbers like this: &H10 (prefixing '&H' in front). &H10 or 10h, is equivalent to 16 in decimal.
10.6 Setting up the video
BIOS     Display     Number     Adapter Cards
Mode(AL)  Lines    of Colors
---------------------------------------------
  0       40x25     B&W text    CGA, EGA, VGA
  1       40x25     Color text  CGA, EGA, VGA
  2       80x25     B&W text    CGA, EGA, VGA
  3       80x25     Color text  CGA, EGA, VGA
  4      320x200      4         CGA, EGA, VGA
  5      320x200      B&W       CGA, EGA, VGA
  6      640x200    2(on/off)   CGA, EGA, VGA
  7       80x25      Mono       MGA, EGA, VGA
  8      160x200      16        PCjr         
  9      320x200      16        PCjr         
Okay, there's obviously many more modes than the ones listed, but you can find that in Ralf Brown's Interrupt List. Let's (for the sake of argument) take AL = 1, so what are we trying to do?
   INT 10h (VIDEO)
   AH = 00h (SET Video Mode)
   AL = 1h (40x25 Video Mode)
Okay, we have it all now, we want to change the video mode to 40x25 color text. See, I told you I would go slow :)
10.7 Do it!!!
Slow enough for you? Okay, now let's combine AH and AL to form AX, there's a simple formula...
       AX = (2^8 * AH) + AL
     or
       AX = (256 * AH) OR AL
If you understand bit shifting and binary operators, this is fairly easy to understand. So anyway, that's the formula, there's also other tricks you can use, but just stick with this formula.
  Since AH = 00h and AL = 1h
  Then  
        AX = (256 * 0) + &H1
           = 1
10.8 Almost there...
We're almost ready, just one more technicality, to pass all the registers, we'll need to define a type structure.
  TYPE RegType
     ax    AS INTEGER
     bx    AS INTEGER
     cx    AS INTEGER
     dx    AS INTEGER
     bp    AS INTEGER
     si    AS INTEGER
     di    AS INTEGER
     flags AS INTEGER
  END TYPE
Read chapter 2 if you don't know what a TYPE is, or how to use it. RegType is defined in QB.BI, so you can copy and paste, or do what most people would do:
       '$INCLUDE: 'QB.BI'
After that, you're all set, we're now ready for the code:
  '$INCLUDE: 'QB.BI'
  DIM InRegs AS RegType, OutRegs AS RegType

  InRegs.AX = (256 * 0) + 1
  CALL INTERRUPT(&H10, InRegs, OutRegs)
I think the last line is self-explanatory, InRegs are the registers you pass, and OutRegs are the returned values of the registers after executing the interrupt. In some cases you want to know the return value, but in this example, forget it. After running the code, you'll kind of shake your head, and wonder if it really worked, well, exit QB, and see what happens, don't blame me! :)
10.9 Summary
Hopefully now you'll understand what interrupts are, how to call them, why combining AH and AL to form AX is both annoying and trivial. Pick up Ralf Brown's Interrupt List and try some more simple examples yourself! Of course I haven't even touched upon strings or how they're passed, this is tricky. I think you can figure out how to read the return values, and how to reverse the formula to obtain AL and AH from AX on your own.

Back to Contents Next: Chapter 11