Real mode assembly IV

From OSDev Wiki
Jump to navigation Jump to search
Difficulty level
Difficulty 2.png
Medium

In this fourth chapter of the Real mode assembly bare bones series, we're going to program the Interrupt Vector Table to create a system call interrupt for our use.

The IVT, in all it's Glory

The IVT is an array of 1024 bytes, arranged in 16-bit words. The address of the interrupt handler is stored with the offset word first, segment second. Thing's couldn't be easier.

Adding a BIOS INT Style Handler

My style of handler goes like this:

 inthandler:
   cmp ah,0
   je .ahzero
 
   cmp ah,1
   je .ahone
 
   cmp ah,2
   je .ahtwo
 
   ; ......
 
   mov si,msgBadAH
   call print_string
   cli
   hlt
 
 .ahzero:
   ; Do whatever needed here
   iret

But that's just an outline, you'd need to do it up however you need to use your calls. The FLAGS register is restored in an iret so you need to set it on the stack again depending on how it's set before the iret is done!

Adding the Handler to the IVT

This is fairly simple.

  1. First, null out ES.
  2. Set AL=interrupt number, and BL=4h.
  3. Multiply AL by BL, and then put the result (AX) in BX.
  4. Move the word that is the address of the start of your interrupt handler into [es:bx].
  5. Add 2 to BX.
  6. Move your handler's segment into [es:bx].
  7. Restore your original ES and you're done!
<- Real mode assembly III