Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =====[stage1.asm]==================================================
- [BITS 16] ;16-bit real mode
- [ORG 0x7C00] ;program origin
- JMP start
- msg_halt DB "HALT0",0x00
- msg_ok DB "OK",0x0D,0x0A,0x00
- msg_fail DB "FAIL",0x0D,0x0A,0x00
- msg_reset DB "Resetting Disk...",0x00
- msg_read DB "Reading Sector...",0x00
- msg_st1 DB "Welcome to stage 1",0x0D,0x0A,0x00
- msg_exit DB "Now leaving stage 1",0x0D,0x0A,0x0D,0x0A,0x00
- currentDrive DB 0x00 ;Drive number
- ;---------------
- ;Halt the system
- ;---------------
- fn_halt:
- MOV SI,msg_halt
- CALL fn_print
- _halt:
- CLI
- HLT
- JMP _halt
- ;---------------------------------------------------------
- ;Print null-terminated string with address in SI to screen
- ;---------------------------------------------------------
- fn_print:
- PUSH AX
- PUSH BX
- _print:
- LODSB
- OR AL,AL
- JZ _printDone
- MOV AH,0x0E
- MOV BH,0x00
- MOV BL,0x0F
- INT 0x10
- JMP _print
- _printDone:
- POP BX
- POP AX
- RET
- ;------------------------------
- ;Reset hdd back to first sector
- ;------------------------------
- fn_reset_disk:
- PUSH AX
- PUSH DX
- MOV SI,msg_reset
- CALL fn_print
- _reset_disk:
- MOV AH,0x00 ;Reset drive
- MOV DL,BYTE[currentDrive] ;Drive number
- INT 0x13 ;Video BIOS interrupt
- JC _reset_disk ;Retry on fail
- MOV SI,msg_ok
- CALL fn_print
- POP DX
- POP AX
- RET
- ;--------------------------------------------
- ;Read a sector from the disk to 0x0500:0x0000
- ;--------------------------------------------
- fn_read_sector:
- MOV DI,0x05
- MOV AX,0x0500 ;Location of stage 2
- MOV ES,AX ;Populate buffer secment
- XOR BX,BX ;Clear buffer offset
- MOV SI,msg_read
- CALL fn_print
- _try_read:
- MOV AH,0x02 ;Read from drive
- MOV AL,1 ;How many sectors to read
- MOV CH,0 ;Track/Cylinder number
- MOV CL,2 ;Sector to read
- MOV DH,0 ;Head number
- MOV DL,BYTE[currentDrive] ;Drive number
- INT 0x13
- JNC _read_done
- DEC DI
- CMP DI,0
- JZ _read_error
- JMP _try_read
- _read_error:
- MOV SI,msg_fail
- CALL fn_print
- CALL fn_halt
- CLI
- HLT
- _read_done:
- MOV SI,msg_ok
- CALL fn_print
- RET
- start:
- MOV [currentDrive],dl ;Save drive number
- CLI ;Disable interrupts while setting up the stack
- XOR AX,AX ;Real mode flat memory model
- MOV DS,AX
- MOV ES,AX
- MOV FS,AX
- MOV GS,AX
- MOV SS,AX
- MOV SP,0xFFFF
- STI ;Enable interrupts
- MOV AH,0x00 ;Set video mode
- MOV AL,0x12 ;640x480x8 @ 80x30
- INT 0x10 ;Video BIOS interrupt
- MOV SI,msg_st1
- CALL fn_print
- CALL fn_reset_disk
- CALL fn_read_sector
- MOV SI,msg_exit
- CALL fn_print
- JMP 0x0500:0x0000
- CALL fn_halt
- CLI
- HLT
- TIMES 510-($-$$) HLT ;Padding bytes
- DB 0x55, 0xAA ;Mark bootable
- =====[stage2.asm]==================================================
- [BITS 16] ;16-bit real mode
- [ORG 0x5000] ;program origin
- JMP main
- %include "gdt.inc" ;Global Descriptor Table definition
- msg_halt DB "HALT1",0x00
- msg_ok DB "OK",0x0D,0x0A,0x00
- msg_st2 DB "Welcome to Stage 2",0x0D,0x0A,0x00
- msg_gdt DB "Setting up GDT...",0x00
- msg_a20 DB "Enabling A20 Gate...",0x00
- msg_pm DB "Entering Protected Mode...",0x00
- ;---------------
- ;Halt the system
- ;---------------
- fn_halt:
- MOV SI,msg_halt
- CALL fn_print
- _halt:
- CLI
- HLT
- JMP _halt
- ;---------------------------------------------------------
- ;Print null-terminated string with address in SI to screen
- ;---------------------------------------------------------
- fn_print:
- PUSH AX
- PUSH BX
- _print:
- LODSB
- OR AL,AL
- JZ _printDone
- MOV AH,0x0E
- MOV BH,0x00
- MOV BL,0x0F
- INT 0x10
- JMP _print
- _printDone:
- POP BX
- POP AX
- RET
- fn_enable_A20:
- MOV AX,0x2401
- INT 0x15
- RET
- main:
- CLI ;Disable interrupts while setting up the stack
- XOR AX,AX ;Real mode flat memory model
- MOV DS,AX
- MOV ES,AX
- MOV FS,AX
- MOV GS,AX
- MOV SS,AX
- MOV SP,0xFFFF
- STI ;Enable interrupts
- MOV SI,msg_st2
- CALL fn_print
- MOV SI,msg_gdt
- CALL fn_print
- CALL fn_load_gdt
- MOV SI,msg_ok
- CALL fn_print
- MOV SI,msg_a20
- CALL fn_print
- CALL fn_enable_A20
- MOV SI,msg_ok
- CALL fn_print
- MOV SI,msg_pm
- CALL fn_print
- MOV EAX,CR0
- OR EAX,1
- MOV CR0,EAX
- MOV SI,msg_ok
- CALL fn_print
- CALL fn_halt
- TIMES 510-($-$$) HLT ;Padding bytes
- =====[gdt.inc]==================================================
- fn_load_gdt:
- cli ;disable interrupts
- pusha ;save registers
- lgdt [gdt_ptr] ;load the gdt
- sti ;enable interrupts
- popa ;restore registers
- ret ;return
- descriptor_table:
- ;null descriptor
- db 0 ;fill 8 bytes with zeros
- db 0
- ;code descriptor
- dw 0FFFFH ;limit low
- dw 0 ;base low
- db 0 ;base middle
- db 10011010b ;access
- db 11001111b ;granularity
- db 0 ;base high
- ;data descriptor
- dw 0FFFFH ;limit low
- dw 0 ;base low
- db 0 ;base middle
- db 10010010b ;access
- db 11001111b ;granularity
- db 0 ;base high
- end_table:
- gdt_ptr:
- dw end_table - descriptor_table - 1 ;the sise of the descriptor table - 1
- dd descriptor_table ;base of the GDT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement