[SOLVED] Int 0x13 doesn't want to load up my kernel, strange

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Rukog
Member
Member
Posts: 51
Joined: Sun Aug 01, 2021 5:24 pm

[SOLVED] Int 0x13 doesn't want to load up my kernel, strange

Post by Rukog »

Code: Select all

[bits 16]
[org 0x7C00]

Bootloader.Code:
;{
    Bootloader.Code.Entry:
    ;{
        mov     [BootDrive], dl     ; DL contains the drive ID used to boot.
        
          mov   al, "B"
          mov   ah, 0x0E
          mov   bh, 0x00
          mov   bl, 0x03
        int     0x10

          mov   ah, 0x02                ; Read sectors function
          mov   al, 1                   ; Number of sectors to read
          mov   ch, 0                   ; Cylinder index
          mov   dh, 0                   ; Head index
          mov   cl, 2                   ; Sector entry to start reading data
          mov   dl, [BootDrive]         ; Drive ID
          mov   bx, Kernel.Code.Entry   ; Pointer where to write the data read
        int     0x13
        
        jnc NoDiskReadError
        ;{
              mov   al, "E"
              mov   ah, 0x0E
              mov   bh, 0x00
              mov   bl, 0x03
            int     0x10
            
            jmp     $
        ;}
        NoDiskReadError:
        
        jmp     Kernel.Code.Entry
    ;}
    
    BootDrive:  db 0
;}
Bootloader.EndCode:

times (510 - Bootloader.EndCode) db 0   ; Fill bin file to reach 510 bytes
dw 0xAA55                              ; + 2 bytes

Kernel.Code.Entry:
;{
      mov   al, "K"
      mov   ah, 0x0E
      mov   bh, 0x00
      mov   bl, 0x03
    int     0x10

    jmp     $
;}

Code: Select all

nasm -f bin ..\Bootloader.asm -o Bootloader.bin
It shows me BE instead of BK

B if bootloader is ok
E if there is a disk error
K if kernel is ok
Last edited by Rukog on Mon Aug 02, 2021 11:34 am, edited 1 time in total.
Octocontrabass
Member
Member
Posts: 5565
Joined: Mon Mar 25, 2013 7:01 pm

Re: Int 0x13 doesn't want to load up my kernel, strange

Post by Octocontrabass »

You're using DS and ES, but you never set them to known values. You must set the segment registers to known values before you can use them.

It may also be a good idea to set up your own stack instead of relying on the BIOS stack. The BIOS stack can be extremely small.

Edit: another possible cause of the error is the size of your disk image. Have you padded it to at least two sectors big?
Rukog
Member
Member
Posts: 51
Joined: Sun Aug 01, 2021 5:24 pm

Re: Int 0x13 doesn't want to load up my kernel, strange

Post by Rukog »

Octocontrabass wrote:You're using DS and ES, but you never set them to known values. You must set the segment registers to known values before you can use them.

It may also be a good idea to set up your own stack instead of relying on the BIOS stack. The BIOS stack can be extremely small.

Edit: another possible cause of the error is the size of your disk image. Have you padded it to at least two sectors big?

Sorry you have short seeing the previous code and ive made a simple one since.
So I no more use DS and ES nor the stack.

For padding, here's a version that add it but still doesn't work.

Code: Select all

[bits 16]
[org 0x7C00]

Bootloader:
;{
    jmp     Bootloader.Code.Entry
    
    ;==========================================================================================================================================================================================================
    ; DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA
    ;==========================================================================================================================================================================================================
    Bootloader.Data:
    ;{
        .Drive:  db 0
    ;}
    Bootloader.EndData:
    ;==========================================================================================================================================================================================================
    ; END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA
    ;==========================================================================================================================================================================================================

    ;==========================================================================================================================================================================================================
    ; CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE
    ;==========================================================================================================================================================================================================
    Bootloader.Code:
    ;{
        .Entry:
        ;{
            mov     [Bootloader.Data.Drive], dl  ; DL contains the drive ID used to boot.
            
              mov   al, "B"
              mov   ah, 0x0E
              mov   bh, 0x00
              mov   bl, 0x03
            int     0x10

              mov   ah, 0x02                    ; Read sectors function
              mov   al, 1                       ; Number of sectors to read
              mov   ch, 0                       ; Cylinder index
              mov   dh, 0                       ; Head index
              mov   cl, 2                       ; Sector entry to start reading data
              mov   dl, [Bootloader.Data.Drive] ; Drive ID
              mov   bx, Kernel.Code.Entry       ; Pointer where to write the data read
            int     0x13
            
            jnc     .NoDiskReadError
            ;{
                  mov   al, "E"
                  mov   ah, 0x0E
                  mov   bh, 0x00
                  mov   bl, 0x03
                int     0x10
                
                jmp     $                   ; $ represent the current line address, thus infinite loop
            ;}
            .NoDiskReadError:
            
            jmp     Kernel.Code.Entry
        ;}
        .EndEntry:
    ;}
    Bootloader.EndCode:
    ;==========================================================================================================================================================================================================
    ; END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE
    ;==========================================================================================================================================================================================================
;}
EndBootloader:

times (510 - (EndBootloader - Bootloader)) db 0   ; Fill the 1st sector
BootMagicWord: dw 0xAA55                          ; ...

Kernel:
;{
    ;==========================================================================================================================================================================================================
    ; DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA      DATA
    ;==========================================================================================================================================================================================================
    Kernel.Data:
    ;{
        
    ;}
    Kernel.EndData:
    ;==========================================================================================================================================================================================================
    ; END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA      END_DATA
    ;==========================================================================================================================================================================================================

    ;==========================================================================================================================================================================================================
    ; CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE      CODE
    ;==========================================================================================================================================================================================================
    Kernel.Code:
    ;{
        .Entry:
        ;{
              mov   al, "K"
              mov   ah, 0x0E
              mov   bh, 0x00
              mov   bl, 0x03
            int     0x10

            jmp     $
        ;}
        End.Entry:
    ;}
    ;==========================================================================================================================================================================================================
    ; END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE      END_CODE
    ;==========================================================================================================================================================================================================
;}
EndKernel:

times (512 - (EndKernel - Kernel)) db 0   ; Fill the 2nd sector
Last edited by Rukog on Mon Aug 02, 2021 5:44 am, edited 1 time in total.
MichaelPetch
Member
Member
Posts: 797
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Int 0x13 doesn't want to load up my kernel, strange

Post by MichaelPetch »

What version of NASM are you using, this code produces an error when assembling. Anyway, when this doesn't work are you testing it in a virtual machine or emulator or on real hardware. If real hardware are you using USB in Floppy Disk Drive (FDD) emulation mode?

Although this is almost certainly not the cause of your issue - you should set DS to 0 (since you are using org 0x7c00) before reading and writing to memory in the bootloader. DS is not necessarily 0 when your code starts so you could be writing the data to memory you don't expect.
Rukog
Member
Member
Posts: 51
Joined: Sun Aug 01, 2021 5:24 pm

Re: Int 0x13 doesn't want to load up my kernel, strange

Post by Rukog »

MichaelPetch wrote:What version of NASM are you using, this code produces an error when assembling. Anyway, when this doesn't work are you testing it in a virtual machine or emulator or on real hardware. If real hardware are you using USB in Floppy Disk Drive (FDD) emulation mode?

Although this is almost certainly not the cause of your issue - you should set DS to 0 (since you are using org 0x7c00) before reading and writing to memory in the bootloader. DS is not necessarily 0 when your code starts so you could be writing the data to memory you don't expect.
Ive updated the code above and no more error.

It's the latest NASM version.
I am using both, Qemu and real PC and it's shows me BK on Qemu but not on PC.
I am using a USB drive and I flash it with HxD.

Ive added

Code: Select all

            xor     ax, ax
            mov     ds, ax
but still doesn't work, only on Qemu.
Klakap
Member
Member
Posts: 297
Joined: Sat Mar 10, 2018 10:16 am

Re: Int 0x13 doesn't want to load up my kernel, strange

Post by Klakap »

You also have to set ES because int 13h is using it.

Code: Select all

 xor ax, ax
 mov es, ax
On QEMU is this probably done but on your PC no.
Rukog
Member
Member
Posts: 51
Joined: Sun Aug 01, 2021 5:24 pm

Re: Int 0x13 doesn't want to load up my kernel, strange

Post by Rukog »

Klakap wrote:You also have to set ES because int 13h is using it.

Code: Select all

 xor ax, ax
 mov es, ax
On QEMU is this probably done but on your PC no.
oof this is so weird, Ive three PC and the
First one display B
Second one display BK
Third one display BB
Klakap
Member
Member
Posts: 297
Joined: Sat Mar 10, 2018 10:16 am

Re: Int 0x13 doesn't want to load up my kernel, strange

Post by Klakap »

Please can you post your actual code?

For the first one with B, I think problem lies in BPB. Some BIOSes are checking if your bootloader has BPB and if not, they are writing their one. So this computer is probably overwriting your code with BPB.
Last edited by Klakap on Mon Aug 02, 2021 7:32 am, edited 1 time in total.
Rukog
Member
Member
Posts: 51
Joined: Sun Aug 01, 2021 5:24 pm

Re: Int 0x13 doesn't want to load up my kernel, strange

Post by Rukog »

Klakap wrote:Please can you post your actual code?
Bootloader.asm
(7.04 KiB) Downloaded 48 times
Klakap
Member
Member
Posts: 297
Joined: Sat Mar 10, 2018 10:16 am

Re: Int 0x13 doesn't want to load up my kernel, strange

Post by Klakap »

You do not set stack pointer.

Code: Select all

 xor ax, ax
 mov ss, 0
 mov sp, 0x7C00 ;stack is going down so we can set it on this value
And as I stated, you should reserve space for BPB(more info in FAT), because some BIOSes can overwrite your code.
Klakap
Member
Member
Posts: 297
Joined: Sat Mar 10, 2018 10:16 am

Re: Int 0x13 doesn't want to load up my kernel, strange

Post by Klakap »

Also, BTW, you should replace

Code: Select all

 jmp $
with

Code: Select all

 halt:
  hlt
 jmp halt
Because jmp $ will cause overheat of your processor after some time. Command HLT will halt processor to next interrupt, so there is not overheating.
Rukog
Member
Member
Posts: 51
Joined: Sun Aug 01, 2021 5:24 pm

Re: Int 0x13 doesn't want to load up my kernel, strange

Post by Rukog »

Klakap wrote:You do not set stack pointer.

Code: Select all

 xor ax, ax
 mov ss, 0
 mov sp, 0x7C00 ;stack is going down so we can set it on this value
And as I stated, you should reserve space for BPB(more info in FAT), because some BIOSes can overwrite your code.
Nice it's evolving, my first PC shows me BE, my second is BK and third one is BE.

So Ive only one PC where INT 0x13 works lmao
Klakap
Member
Member
Posts: 297
Joined: Sat Mar 10, 2018 10:16 am

Re: Int 0x13 doesn't want to load up my kernel, strange

Post by Klakap »

I suggest you to try load your code on 0x10000. You also have to do it, otherwise you can load max 32 KB because of CS segment register.

Code: Select all

 mov ax, 0x1000
 mov es, ax
 ...
 mov bx, 0
 int 13h
 ...

 jmp 0x1000:0x0000 ;execute your code
So Ive only one PC where INT 0x13 works lmao
No, int 13h works, but we need to turn his work for our wants.
Rukog
Member
Member
Posts: 51
Joined: Sun Aug 01, 2021 5:24 pm

Re: Int 0x13 doesn't want to load up my kernel, strange

Post by Rukog »

Klakap wrote:I suggest you to try load your code on 0x10000. You also have to do it, otherwise you can load max 32 KB because of CS segment register.

Code: Select all

 mov ax, 0x1000
 mov es, ax
 ...
 mov bx, 0
 int 13h
 ...

 jmp 0x1000:0x0000 ;execute your code
So Ive only one PC where INT 0x13 works lmao
No, int 13h works, but we need to turn his work for our wants.
ok with this code, my both PC show me E

The last one shows me BK as usual.
Klakap
Member
Member
Posts: 297
Joined: Sat Mar 10, 2018 10:16 am

Re: Int 0x13 doesn't want to load up my kernel, strange

Post by Klakap »

Can you figure out if dl on non-working computers is 0x00 or 0x80?
Post Reply