assemblyx86x86-16bootloaderbios

nasm bios int 13h read sector issue


im trying to make a simple bootloader that loads a simple kernel and the problem is that the kernel code doesnt run i do see prints from the bootloader but not from the kernel here is my code:

boot.asm:

bits 16
org 0x7c00

;here set to vga mode
mov bx, MsgVga
call bios_print

mov ah, 0x00
mov al, 0x03
int 0x10

mov bx, MsgGreet
call bios_print

mov bx, MsgKernel
call bios_print

;here load the kernel
mov ah, 02h ;read sectors
mov al, 1
mov ch, 0
mov cl, 2
mov dh, 0
mov dl, 0
mov ax, 0x0000
mov es, ax
mov bx, 0x1000
int 13h

jc .KernelErr

test ah, ah
jnz .KernelErr

push es
push bx
retf

.KernelErr:
mov bx, MsgKernelErr
call bios_print

jmp $

bios_print:
        ;bx should point to the string
.l:
        mov al, [bx]
        test al, al
        jz .d
        inc bx
        mov ah, 0x0e
        int 0x10

        jmp .l

.d:
        ret

MsgGreet db 'Hello from cool bootloader!', 0
MsgVga db 'Setting video mode to VGA...', 0
MsgKernel db 'Attempting to load kernel...', 0
MsgKernelErr db 'Error while loading kernel. Entering infinite loop...', 0
times 510 - ($-$$) db 0

dw 0xAA55

kernel.asm:

bits 16
org 0x1000

section .text
        global _start

_start:
        ;here do stuff
        mov ah, 0x0E
        mov al, '-'
        int 0x10

        hlt

section .data
        text db 'cool text here', 0

times 512 - ($-$$) db 0

the 'cool text here is just so i recognize it in a hex dump'

this is how i assemble and run it(from linux terminal):

nasm -f bin boot.asm -o boot.bin
nasm -f bin kernel.asm -o kernel.bin
cat boot.bin kernel.bin > os.img
qemu-system-i386 -fda os.img

when running i only see the greet and attempting to load kernel messages(vga not showing cuz screen gets cleared from the bios interupt) but the dash from the kernel doesnt get printed

here is an hexdump of os.img:

user@fedora:~/os$ hexdump -C os.img
00000000  bb 69 7c e8 39 00 b4 00  b0 03 cd 10 bb 4d 7c e8  |.i|.9........M|.| 
00000010  2d 00 bb 86 7c e8 27 00  b4 02 b0 01 b5 00 b1 02  |-...|.'.........| 
00000020  b6 00 b2 00 b8 00 00 8e  c0 bb 00 10 cd 13 72 07  |..............r.| 
00000030  84 e4 75 03 06 53 cb bb  a3 7c e8 02 00 eb fe 8a  |..u..S...|......| 
00000040  07 84 c0 74 07 43 b4 0e  cd 10 eb f3 c3 48 65 6c  |...t.C.......Hel| 
00000050  6c 6f 20 66 72 6f 6d 20  63 6f 6f 6c 20 62 6f 6f  |lo from cool boo| 
00000060  74 6c 6f 61 64 65 72 21  00 53 65 74 74 69 6e 67  |tloader!.Setting| 
00000070  20 76 69 64 65 6f 20 6d  6f 64 65 20 74 6f 20 56  | video mode to V| 
00000080  47 41 2e 2e 2e 00 41 74  74 65 6d 70 74 69 6e 67  |GA....Attempting| 
00000090  20 74 6f 20 6c 6f 61 64  20 6b 65 72 6e 65 6c 2e  | to load kernel.| 
000000a0  2e 2e 00 45 72 72 6f 72  20 77 68 69 6c 65 20 6c  |...Error while l| 
000000b0  6f 61 64 69 6e 67 20 6b  65 72 6e 65 6c 2e 20 45  |oading kernel. E| 
000000c0  6e 74 65 72 69 6e 67 20  69 6e 66 69 6e 69 74 65  |ntering infinite| 
000000d0  20 6c 6f 6f 70 2e 2e 2e  00 00 00 00 00 00 00 00  | loop...........| 
000000e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................| 
* 
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.| 
00000200  b4 0e b0 2d cd 10 f4 00  63 6f 6f 6c 20 74 65 78  |...-....cool tex| 
00000210  74 20 68 65 72 65 00 00  00 00 00 00 00 00 00 00  |t here..........| 
00000220  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................| 
* 
00000400  00 00 00 00 00 00 00 00                           |........| 
00000408

and some info from gdb before and after the bios int to read sectors:
before(at start of bootloader 0x7C00):


gdb) x/32xb 0x00001000 
0x1000: 0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00 
0x1008: 0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00 
0x1010: 0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00 
0x1018: 0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00

after(got to 0x00001000):

(gdb) x/32xb 0x00001000
0x1000: 0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00
0x1008: 0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00
0x1010: 0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00
0x1018: 0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00

Any help is greatly appriciated!


Solution

  • You overwrote AX register when doing mov ax, 0x0000 so AH and AL don't have proper values when calling int 13h.

    (8-bit AH and AL registers are high and low bytes of 16-bit AX register)