
stage1 c0de
April 27, 2009here is the code, which I have written for stage1 boot loader
; Copyright (c) 2009 K.K.Senthil Velan
; Permission to use, copy, modify, and distribute this software for any
; purpose with or without fee is hereby granted, provided that the
; above copyright notice and this permission notice appear in all
; copies.
; THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS
; ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING
; ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
; DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
; WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
; TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
; USE OR PERFORMANCE OF THIS SOFTWARE.
; 0.8
; ———————————————————-
; the stage1 boot loader
; ———————————————————-
[bits 16]
[org 0x07C00]
jmp start1
; ———————————————————-
; code section STARTS
; ———————————————————-
start1:
mov ax, 0×0000
mov ds, ax ; set data segment to 0×0000
mov ss, ax ; set stack segment to 0×0000
mov si, rloaderString ; print the welcome string
call PrintString
mov si, Stage1String
call PrintString
call ResetFloppy
; set the segment, where the sector will be loaded into this mem location
; this is the location , where stage2 will be loaded and executed
mov ax, 0×1000
mov es, ax
mov bx, 0×0000
mov ah, 0×02
mov al, 0×02 ; load two sectors, as size of stage2 is > 512
mov ch, 0×00
mov cl, 0×02
mov dh, 0×00
mov dl, 0×00
int 0×13
jc ResetFloppy
jmp 0×1000:0000 ; since stage2 starts at 0×10000, jump there
; ———————————————————-
; code section ENDS
; ———————————————————-
; ———————————————————-
; core functions STARTS
; ———————————————————-
; resets the floppy drive controller
ResetFloppy:
mov ah, 0×00 ; service for resetting
mov dl, 0×00 ; floppy #
int 0×13
jc ResetFloppy ; jmp on error
ret
PrintString:
NextPrint:
lodsb ; load from si register
or al, al ; check for zero
jz ExitPrint ; check if the final char has reached
call PrintChar ; print the char
jmp NextPrint
ExitPrint:
ret
PrintChar:
mov ah, 0×0E ; for service number
mov bh, 0×00 ; the page number
mov bl, 0×07 ; for colour
int 0×10 ; call video interrupt
ret
NewLine:
mov al, 0×0A ; 10 is for line feed
call PrintChar
mov al, 0×0D ; 13 is for carriage return
call PrintChar
ret
FlushRegs:
; xor is faster than mov
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
ret
; —————————————————————-
; core functions ENDS
; —————————————————————-
; —————————————————————-
; data section STARTS
; —————————————————————-
rloaderString db “rloader version 0.6. “, 13, 10, 0
Stage1String db “stage1 boot loader (0.8) loaded. loading stage2 … “, 13, 10, 0
; —————————————————————-
; data section ENDS
; —————————————————————-
TIMES 510 – ($ – $$) db 0
DW 0×55AA