Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [BITS 16]
- [ORG 0x7C00]
- start:
- ; Set up segments and stack
- xor ax, ax
- mov ds, ax
- mov es, ax
- mov ss, ax
- mov sp, 0x7000 ; Move stack below bootloader
- ; Store boot drive number
- mov [bootDrive], dl
- ; Load Stage 2 in chunks (3 sectors at a time, total 32 sectors)
- mov cx, 0x0002 ; Start at sector 2 (CH=0, CL=2)
- mov bx, 0x7E00 ; Start loading at memory address 0x8000
- mov dl, [bootDrive] ; Load the boot drive number
- load_loop:
- mov ah, 0x02 ; BIOS read sector function
- mov al, 3 ; Read 3 sectors at a time
- mov ch, 0 ; Cylinder number
- mov dh, 0 ; Head number
- int 0x13 ; BIOS interrupt to read disk
- jc disk_error ; Jump if error
- ; Advance to next set of sectors
- add cx, 3 ; Increment sector number
- add bx, 1536 ; Move memory address (3 * 512 = 1536 bytes)
- cmp cx, 34 ; Stop when sector >= 34 (32 sectors total from 2)
- jl load_loop ; Continue loading until all sectors are read
- ; Print success message
- mov si, successMsg
- call print_string
- ; Jump to stage 2 at 0x8000
- jmp 0x0000:0x7E00
- disk_error:
- ; Display error and halt
- mov si, diskErrorMsg
- call print_string
- mov al, ah ; Error code in AH
- call print_hex
- cli ; Disable interrupts
- .halt:
- hlt ; Halt CPU
- jmp .halt ; Loop forever
- print_string:
- lodsb
- or al, al
- jz done
- mov ah, 0x0E
- int 0x10
- jmp print_string
- done:
- ret
- print_hex:
- push ax
- push bx
- mov bx, ax
- shr al, 4
- call print_hex_digit
- mov al, bl
- and al, 0x0F
- call print_hex_digit
- pop bx
- pop ax
- ret
- print_hex_digit:
- and al, 0x0F
- add al, '0'
- cmp al, '9'
- jle .print
- add al, 7
- .print:
- mov ah, 0x0E
- int 0x10
- ret
- diskErrorMsg db 'Disk Error! Code: ', 0
- successMsg db 'Stage 2 loaded successfully!', 13, 10, 0
- bootDrive db 0
- times 510 - ($ - $$) db 0
- dw 0xAA55 ; Boot signature
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement