Skip to content

Interrupt Descriptor Table (IDT) with Nasm macros

randcode-generator edited this page Sep 10, 2014 · 5 revisions

Rewrote the IDT with macros

The following macro is used for IDT. The macro has takes in an offset (the address of the handler).

%macro interrupt 1
    dw   %1                 ; offset(0:15)-address of interrupt function (handler)
    dw   8                  ; code segment (0x08)
    db   0                  ; hard coded all zeros
    db   10001110b          ; 1000b
                            ; 1-segment present flag
                            ; 00-privilage level
                            ; 0-hard coded value
                            ; 1110b
                            ; 1-size of gate 1-32 bit, 0-16 bit
                            ; 110-hard coded value for type interrupt gate
    db   0,0                ; offset(16:31)
%endmacro

The macro is invoked by calling the macro and passing in the address of the handler. In the following example, interrupt1 (label) is the handler, starting (label) is the start of the program, and org is 0x7c00.

interrupt interrupt1-starting+org

Note that without the starting label (interruput1+org) nasm will interpret this as address location not value. It will produce the same value, but it will be referenced to an address instead of just a value.

Example: With starting

interrupt interrupt1-starting+org

produces the following

00000050 E67C                <1>  dw %1

Notice it is outputting a value

Without starting

interrupt interrupt1+org

produces the following

00000050 [E67C]              <1>  dw %1

Notice it is referencing to a memory location

The following macro is used for interrupt handler (or function). First argument is the message, second argument is the line to print.

%macro interrupt_handler 2  
    lea     eax, [intMessage%1]  ; load address of msg to eax
    push    eax                 ; push into stack (this will be parameter 1)
    push    0                   ; x
    push    %2                  ; y
    call    print
    add     esp,12              ; pop off the stack
iret
%endmacro

Code 1 : IDT using macros

https://github.com/randcode-generator/RandCodeOS/blob/de11fee30f1f65ee4d2fc0d4efc5d4116b3de858/boot.asm