[BITS 16]
[ORG 0x7C00]

jmp start

;-------------------------------------;
;     	  Start main function         ;
;-------------------------------------;
start:
	mov [bootdrive], dl  		 ; The bios keep the drive that we booted from
						 		 ; in the register al, therefore we save it here
	
	mov si, notification 
	call print_str
	
	call read_drive
	
	call 1000h:0000
	
   	jmp $				 		;Hang
;-------------------------------------;


; Reset the drive saved before in bootdrive using
; the bios interrupt 0x13 function 0
reset_drive:
	xor byte ah, ah		 ;Set AH to 0
	mov dl, [bootdrive]
	int 0x13
	ret

; Read a specified number of sectors (512 bytes) from the booted drive
; Bios interrupt 0x13
; ah=0x2				al=Sectors to read_drive
; cx=Track/Cylinder		dh=head
; dl=drive				es:bx buffer address pointer
;
; Returns
; cf set if error
; ah return code
; al actual sectors read count

; Note cx contains cylinder number and sector number
; cx = 		---ch---  ---cl---
; Cylinder : 76543210 98           
; Sector   :             543210

read_drive:
	call reset_drive
	
	mov ax, 1000h
	mov es, ax
	xor bx, bx

	mov ah, 2			;
	mov al, 5			;Read 5 sectors
	mov ch, 0			;cylinder 0
	mov cl, 2			;sector 2 ( sector 1 is the boot sector )
	mov dh, 0           ;head 0
	mov dl, [bootdrive] ;disk drive
	int 0x13
	
	jc  read_drive		;Try again if there is error
		
	ret

; Write string pointed to by %si
; Use Bios interrupt 0x10 function E for character writing
; Function params
; ah = 0xe 		al = character
; bh = page     bl = foreground color

print_c:
	mov word bx, 0x0001
	mov byte ah, 0xE
	int 0x10
print_str:
	lodsb
	cmp byte al,0
	jne print_c
	ret	
	
;-------------------------------------;
;       	Data Section              ;
;-------------------------------------;
bootdrive    db  0
notification db "Loading..."

times 510-($-$$) db 0
	dw 0xAA55
