Commit 10f6f815 by triazo

Merge branch 'multi-stage' into 'master'

Added multi-stage bootloader that loads real bootloader from disk

Multi-stage bootloader that should load the main code into memory

See merge request !3
parents 3e917a93 96c3a990
Showing with 65 additions and 30 deletions
;--------------- PRELIMINARY SETUP ---------------; [bits 16] ; 16 bit real mode
[BITS 16] ; 16 bit real mode [org 0x7C00] ; MBR is loaded at 0x7C00
[ORG 0x7C00] ; MBR is loaded at 0x7C00
jmp 0x0000:st ;cs=0x0000, for consistency
st:
xor ax, ax ;clearing registers
mov ds, ax
mov es, ax
mov dl, 0x80 ; HDD1
xor dh, dh ; force 0
mov bx, 0x7e00 ; read to 7e00, after bootloader
mov al, 1 ; num of sectors to read
mov cx, 0x0002 ; floppy cylinder
call read_sectors_16 ; read with above args
jnc 0x7e00 ; jump to actual code
halt:
cli ; halt if failed, loop
hlt
jmp halt
read_sectors_16:
pusha
mov si, 0x02 ; maximum attempts - 1
.top:
mov ah, 0x02 ; read sectors into memory (int 0x13, ah = 0x02)
int 0x13
jnc .end ; exit if read succeeded
dec si ; decrement remaining attempts
jc .end ; exit if maximum attempts exceeded
xor ah, ah ; reset disk system (int 0x13, ah = 0x00)
int 0x13
jnc .top ; retry if reset succeeded, otherwise exit
.end:
popa
retn
read_fail db 'Failure to read from disk.', 13, 10, 0
; $ is current line, $$ is first line, db 0 is a 00000000 byte
; So, pad the code with 0s until you reach 510 bytes
times 510 - ($ - $$) db 0
; Fill last two bytes (a word) with the MBR signature 0xAA55
dw 0xaa55
;---------------- BOOTLOADER CODE ----------------; ;---------------- BOOTLOADER CODE ----------------;
ls: MOV SI, str1 ; pointer to string in SI ls: mov si, str1 ; pointer to string in si
CALL PrintStr ; print string call PrintStr ; print string
;MOV ebx, 1000 ;start at 10000 jmp Main ;
;SUB ebx, 1 ;down 1 each time hlt
;NOP ;waste time
;JZ 6 ;for loop
;JMP ls ;
HLT
;---------------- SCREEN FUNCTIONS ---------------; ;---------------- SCREEN FUNCTIONS ---------------;
PrintStr: ; print string at SI PrintStr: ; print string at SI
nextchar: nextChar:
MOV AL, [SI] ; Grab next char mov al, [si] ; grab next char
OR AL, AL ; if char is null term or al, al ; if char is null term
JZ exit ; then return jz exit ; then return
CALL PrintChar ; else print char call PrintChar ; else print char
INC SI ; inc pointer to next char inc si ; inc pointer to next char
JMP nextchar ; loop jmp nextChar ; loop
exit: exit:
RET ret
PrintChar: ; print char at AL PrintChar: ; print char at AL
MOV AH, 0x0E ; Teletype Mode mov ah, 0x0e ; teletype mode
MOV BH, 0x00 ; Page zero mov bh, 0x00 ; page zero
MOV BL, 0x07 ; Light Gray mov bl, 0x07 ; light gray
INT 0x10 ; Print Character int 0x10 ; print character
RET ret
;------------------ DATA BLOCK ------------------; ;------------------ DATA BLOCK ------------------;
str1 db 'Hello World', 0 ;stuff str1 db 'Hello World', 0 ;stuff
;-------------- PADDING / SIGNATURE -------------; str2 db 'WHY IS THIS', 0
; $ is current line, $$ is first line, db 0 is a 00000000 byte
; So, pad the code with 0s until you reach 510 bytes
TIMES 510 - ($ - $$) DB 0
; Fill last two bytes (a word) with the MBR signature 0xAA55
DW 0xAA55
Main: ;main section Main: ;main section
nop
nop
mov si, str2
call PrintStr
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment