Page 1 of 1

Creating IVT Handlers

Posted: Sun Aug 18, 2013 12:36 pm
by blackfireize
Hello,

Recently, I've been trying to implement a DOS compatibility layer into my OS; I'm hoping to begin this task by emulating the DOS interrupts 20h and 21h. I found on the Real mode assembly IV
(http://wiki.osdev.org/Real_mode_assembly_IV) page they describe creating your own IVT handlers which was ideal for what I am trying to do. I did get it working but I had a question about
why it worked, not how.

The reason why I ask is because, originally I had this code (for simplicity's sake I just assumed this was a bootloader):

Code: Select all

org 7C00h

start:
        mov ax, 7C00h
        mov ds, ax
        mov es, ax
        mov fs, ax
        
        xor ax, ax
        xor bx, bx
        mov es, ax

        mov al, 21h
        mov bl, 4h
        mul bx
        mov bx, ax
        
        mov word [es:bx], handler
        add bx, 2
        mov [es:bx], fs 
        mov ax, 7C00h
        mov es, ax

        int 21h
        cli
        hlt
        
handler:
        mov ah, 0Eh
        mov al, 'A'
        int 10h
        iret 
This code failed, although through some debugging I determined why, this line:

Code: Select all

mov [es:bx], fs
Was not setting the proper code segment for the handler, and I suppose perhaps this is my ignorance in assembly but shouldn't I have to set the segment to where it I was ORG'ed?
I did however change the code to this:

Code: Select all

mov word [es:bx], 0h
and it worked (I assume this is because the handler was in the same segment as the rest of the code?)

I am still a bit unsure of this could some perhaps help shed some light on this for me?

Thanks in advance

Re: Creating IVT Handlers

Posted: Sun Aug 18, 2013 2:06 pm
by xenos
According to the ORG directive, your code starts at 0x7c00. Your handler thus has an offset somewhat greater than 0x7c00. If you set your segment to 0x7c00, the vector becomes (segment << 4) + offset, which is somewhat greater than 0x83c00, and this is not where your handler is. So either you set the segment to 0, or set it to 0x7c0 (not 0x7c00) and change the ORG directive to 0.