Page 1 of 1

int 0x13 causes function not to return

Posted: Mon Mar 20, 2023 12:08 pm
by einspieler13
Hello, I try to write an 16bit boot program but I encounter a weird problem. I have written an function that takes the drive, cylinder, head, segment, and an memory address, and writes from the segment into the specified address it's contents.
I call functions with returns and arguments like this: first push a value for the return, then push the arguments in reverse order and then the function gets called. And my function looks like this:

Code: Select all

; param 1 drive
; param 2 cylinder
; param 3 head
; param 4 sector
; param 5 buffer
; return error (error = 1)
bios_load_chs:
    mov bx, sp
    ; drive, param 1
    mov ax, WORD[ss:bx+2]
    mov dl, al
    ; cylinder, param 2 
    mov ax, WORD[ss:bx+4]
    mov ch, al
    ; head, param 3
    mov ax, WORD[ss:bx+6]
    mov dh, al
    ; sector, param 4
    mov ax, WORD[ss:bx+8]
    mov cl, al
    ; buffer, param 5 (write location is in es:bx)
    mov bx, WORD[ss:bx+10]
    mov ah, 2
    ; reading only one segment, explanation why: https://wiki.osdev.org/Disk_access_using_the_BIOS_(INT_13h)
    mov al, 1
    clc
    int 0x13
    mov bx, sp
    ; error handeling
    jnc BIOSLOADSHSNOERROR
    mov WORD[ss:bx+12], 1
    ret
BIOSLOADSHSNOERROR:
    mov WORD[ss:bx+12], 0
    ret
And I call it with:

Code: Select all

push 0 ; return
push 0x7f00 ; buffer
push 4 ; sector
push 0 ; head
push 0 ; cylinder
mov ah, 0
mov al, BYTE[DISKDIM + DISKDIM_DRIVE_OFFSET]
push ax ; drive
call bios_load_chs
pop ax ; arg 1
pop ax ; arg 2
pop ax ; arg 3
pop ax ; arg 4
pop ax ; arg 5

call print_num
pop ax ; return
The stack should look like this (the elements are btw. words):
|return|buffer|sector|head|cylinder|drive|call address| ---growing direction---->
(my stack segment is at 0x6fff and the stack/base pointer gets initialized at 0xffff)
How ever my function doesn't return. And even more interesting is, if I print a character before the "int 0x13" it gets printed, but if I try to print an character after the interrupt I get nothing.

Re: int 0x13 causes function not to return

Posted: Tue Mar 21, 2023 10:28 pm
by alexfru
The read will overwrite memory at es:bx. Where does this pair of registers point? Is BIOS by any chance overwriting the stack or code of your program?

Re: int 0x13 causes function not to return

Posted: Wed Mar 22, 2023 1:24 am
by rdos
alexfru wrote:The read will overwrite memory at es:bx. Where does this pair of registers point? Is BIOS by any chance overwriting the stack or code of your program?
Or the interrupt vector table. :-)

Re: int 0x13 causes function not to return

Posted: Wed Mar 22, 2023 4:13 am
by einspieler13
yes, it did overwrite itself...
Thanks for the replies, I completely overlooked this, I am sorry for such an simple question it will not happen again (I hope). Next time I will use a label for the next free segment instead of "calculating" it.