LOADOFF	   =	0x7C00
BOOTSEG    =	0x1000
BOOTOFF	   =	0x0030
BUFFER	   =	0x0600
DSKBASE    =	  0x1E
DSKPARSIZE =	    11
SECTORS	   =	     4
LOWSEC     =	     8
device	   =	     0
lowsec	   =	     2
secpcyl	   =	     6
.define begtext, begdata, begbss, endtext, enddata, endbss
.data
begdata: 
.bss
begbss: 
.text
begtext: 
boot: 
xor	ax, ax
mov	ds, ax
cli
mov	ss, ax
mov	sp, #LOADOFF
sti
push	ax
push	ax
push	dx
mov	bp, sp
push	es
push	si
mov	di, #LOADOFF+parameters
testb	dl, dl
jge	floppy
winchester: 
seg es
les	ax, LOWSEC(si)
mov	lowsec+0(bp), ax
mov	lowsec+2(bp), es
movb	ah, #0x08
int	0x13
andb	cl, #0x3F
movb	SECTORS(di), cl
incb	dh
j loadboot
next: add	di, #DSKPARSIZE
floppy: mov	DSKBASE*4+0, di
mov	DSKBASE*4+2, ds
xorb	ah, ah
int	0x13
movb	cl, SECTORS(di)
cmp	di, #LOADOFF+dsdd3
jz	success
mov	es, lowsec(bp)
mov	bx, #BUFFER
mov	ax, #0x0201
xorb	ch, ch
xorb	dh, dh
int	0x13
jb	next
success: movb	dh, #2
loadboot: 
movb	al, cl
mulb	dh
mov	secpcyl(bp), ax
mov	ax, #BOOTSEG
mov	es, ax
xor	bx, bx
mov	si, #LOADOFF+addresses
load: 
mov	ax, 1(si)
movb	dl, 3(si)
xorb	dh, dh
add	ax, lowsec+0(bp)
adc	dx, lowsec+2(bp)
div	secpcyl(bp)
xchg	ax, dx
movb	ch, dl
divb	SECTORS(di)
xorb	dl, dl
shr	dx, #1
shr	dx, #1
orb	dl, ah
movb	cl, dl
incb	cl
movb	dh, al
movb	dl, device(bp)
movb	al, SECTORS(di)
subb	al, ah
cmpb	al, (si)
jbe	read
movb	al, (si)
read: push	ax
movb	ah, #2
int	0x13
pop	cx
jb	error
movb	al, cl
addb	bh, al
addb	bh, al
add	1(si), ax
adcb	3(si), ah
subb	(si), al
jnz	load
add	si, #4
cmpb	ah, (si)
jnz	load
done: 
pop	si
pop	es
jmpi BOOTOFF, BOOTSEG
error: 
mov	si, #LOADOFF+rderr
print: lodb
movb	ah, #14
mov	bx, #0x0001
int	0x10
cmp	si, #LOADOFF+errend
jb	print
hang: j hang
.data
rderr: .ascii	"Read error "
errend: 
parameters: 
dshd3: .byte 	0xAF, 0x02, 25, 2, 18, 0x1B, 0xFF, 0x54, 0xF6, 15, 8
dshd5: .byte 	0xDF, 0x02, 25, 2, 15, 0x1B, 0xFF, 0x54, 0xF6, 15, 8
dsdd3: .byte 	0xDF, 0x02, 25, 2,  9, 0x2A, 0xFF, 0x50, 0xF6, 15, 8
.text
endtext: 
.data
enddata: 
addresses: 
.bss
endbss: 
