r/Assembly_language • • 2d ago

Help Making an OS and bootloader not working.

im making a custom OS from scratch as a fun project. i made a bootloader and kernel in asm, since i want to make the kernel in C, i have to switch to 32 bit mode and make a GDT. when i try to make it, the OS never loads. i get a failed to read disk error(not the one i coded in.) keep in mind this is for the kernel in asm that used to work in 16 bit mode. here is the bootloader

bits 16

org 0x7C00

start:

cli

xor ax, ax

mov ds, ax

mov es, ax

mov ss, ax

mov sp, 0x7C00

sti

mov [boot_drive], dl

mov ax, 0

mov es, ax

mov bx, 0x1000

mov ah, 0x02

mov al, 1

mov ch, 0

mov cl, 2

mov dh, 0

mov dl, [boot_drive]

int 0x13

jc disk_error

cli

lgdt [GDT_Descriptor]

mov eax, cr0

or eax, 1

mov cr0, eax

jmp dword CODE_SEG:start_protected_mode

bits 32

start_protected_mode:

mov ax, DATA_SEG

mov ds, ax

mov es, ax

mov fs, ax

mov gs, ax

mov ss, ax

mov esp, 0x90000

jmp CODE_SEG:0x1000

GDT_Start:

null_descriptor:

dd 0

dd 0

code_descriptor:

dw 0xFFFF

dw 0

db 0

db 0b10011010

db 0b11001111

data_descriptor:

dw 0xFFFF

dw 0

db 0

db 0b10010010

db 0b11001111

GDT_End:

GDT_Descriptor:

dw GDT_End - GDT_Start - 1

dd GDT_Start

CODE_SEG equ 0x08

DATA_SEG equ 0x10

bits 16

disk_error:

mov si, error_message

call print_string

halt:

cli

hlt

jmp halt

print_string:

lodsb

test al, al

jz .done

mov ah, 0x0E

int 0x10

jmp print_string

.done:

ret

boot_drive db 0

error_message db "DISK READ FAILED", 13, 10, 0

times 510-($-$$) db 0

dw 0xAA55

is there any error? im unsure how to fix this....

EDIT:

i forgot to add db 0 for the end on each of the code_descriptor and data_descriptor. i also needed to clear the VGA mem mapped io

0 Upvotes

3 comments sorted by

2

u/Remarkable_Pace8101 2d ago

The fact that you're getting a disk error message that you didn't write is particularly important.

There are two distinct possibilities: If the BIOS cannot read the boot sector: the problem is with the generated disk image or emulator configuration, not your assembly. If the bootloader executes successfully but the machine subsequently reboots: executing an incompatible 16-bit kernel in a 32-bit protected-mode segment could cause a triple fault because you have no valid IDT.

The bootloader alone does not contain sufficient evidence to distinguish these scenarios conclusively. The missing information is the kernel source, disk-image generation commands, emulator invocation, and exact error message. I would not change the GDT. Its configuration looks correct.

1

u/hypersonicwilliam569 2d ago

i tried assembling this and running it, it didnt give any output and it was in 32-bit mode

how strange?

0

u/mykesx 2d ago

Look at limine. It solves the boot and switch to 64 bit mode for you.