my latest os code, but its almost basically taken 95% from gemini. (i was on the right path, but i think my bad fat info ruined it)

 ; floppy_boot.asm
; A simple legacy boot sector example to read a sector from floppy and jump to it.
;
; To assemble with NASM: nasm -f bin floppy_boot.asm -o floppy_boot.bin
; To write to a floppy image (e.g., in a VM): dd if=floppy_boot.bin of=floppy.img bs=512 count=1 conv=notrunc

org 0x7C00               ; Boot sectors are loaded at 0x7C00

BITS 16                  ; We are in 16-bit real mode

start:
    jmp short setup_segments

; --- Data ---
; These are placeholders for a typical boot sector.
; In a real scenario, you'd have more data here, including a Partition Table.
OEM_ID                  db "MSWIN4.1"   ; OEM ID
BytesPerSector          dw 512          ; Bytes per sector
SectorsPerCluster       db 1            ; Sectors per cluster
ReservedSectors         dw 1            ; Reserved sectors
NumberOfFATs            db 2            ; Number of File Allocation Tables
RootEntries             dw 224          ; Max root directory entries
TotalSectorsShort       dw 2880         ; Total sectors (if < 65536)
MediaType               db 0xF0         ; Media type (floppy)
SectorsPerFAT           dw 9            ; Sectors per FAT
SectorsPerTrack         dw 18           ; Sectors per track
NumberOfHeads           dw 2            ; Number of heads
HiddenSectors           dd 0            ; Hidden sectors
TotalSectorsLong        dd 0            ; Total sectors (if > 65536)

DriveNumber             db 0            ; Drive number (0 for floppy A:)
Reserved1               db 0
BootSignature           db 0x29         ; Boot signature
VolumeID                dd 0x12345678   ; Volume ID
VolumeLabel             db "MY BOOTDISK" ; Volume label
FileSystemType          db "FAT12   "    ; File system type (padded with spaces)


; --- Code ---

setup_segments:
    xor ax, ax               ; AX = 0
    mov ds, ax               ; DS = 0 (Data Segment)
    mov es, ax               ; ES = 0 (Extra Segment)
    mov ss, ax               ; SS = 0 (Stack Segment)
    mov sp, 0x7C00           ; SP points to the start of the boot sector (stack grows downwards)

    ; We're loading into 0x7E00 (just after the boot sector itself)
    ; This is a common practice to avoid overwriting the boot sector while it's running.
    mov bx, 0x7E00           ; BX will hold the memory address for the loaded sector

    ; --- Read Sector using INT 13h ---
    ; AH = 0x02 (Read Sectors)
    ; AL = Number of sectors to read (1 in this case)
    ; CH = Cylinder number (0 for floppy)
    ; CL = Sector number (2, because boot sector is sector 1, we want the next one)
    ; DH = Head number (0 for floppy)
    ; DL = Drive number (0 for floppy A:, 1 for B:)
    ; ES:BX = Pointer to buffer where data will be loaded

read_sector:
    mov ah, 0x02             ; BIOS Function: Readhttps://nicks-os-blog.blogspot.com/ Sectors
    mov al, 0x01             ; Number of sectors to read (1 sector)
    mov ch, 0x00             ; Cylinder number (0)
    mov cl, 0x02             ; Sector number (Start from sector 2, as sector 1 is this boot sector)
    mov dh, 0x00             ; Head number (0)
    mov dl, [DriveNumber]    ; Drive number (0 = A:)
    mov bx, 0x7E00           ; ES:BX = Destination buffer address (0x0000:0x7E00)
    int 0x13                 ; Call BIOS Disk Services

    jc disk_error            ; Jump if carry flag is set (error occurred)

    ; --- Jump to the loaded sector ---
    jmp 0x0000:0x7E00        ; Far jump to the loaded code at 0x7E00

disk_error:
    ; Simple error handling: halt the system
    mov si, err_msg
    call print_string
    hlt                      ; Halt the CPU

print_string:
    mov ah, 0x0E             ; Teletype output (BIOS INT 10h, AH=0x0E)
.loop:
    lodsb                    ; Load byte from [DS:SI] into AL, increment SI
    or al, al                ; Check if AL is null (end of string)
    jz .done                 ; If AL is 0, we're done
    int 0x10                 ; Call BIOS to print character
    jmp .loop
.done:
    ret

err_msg         db "Disk Read Error!", 0

; --- Boot Signature ---
; Must be at the end of the boot sector for the BIOS to recognize it.
times 510 - ($ - $$) db 0   ; Pad with zeros until 510 bytes
dw 0xAA55                   ; Boot signature (0x55AA little-endian)

sector_2:
times 4096 - ($ -$$) db 0

Comments

Popular posts from this blog

woo hoo, here is an easy way to make UEFI boot code for YOUR os :D