Page 1 of 1

trying to start from scratch

Posted: Wed Jan 05, 2011 10:41 pm
by konrad236
Hi all, I spent much time reading, playing with code snippets, and building on other codes from tutorials.I have wrote small 32 bit kernel in nasm, pretty useless still but I am proud.

After taking a break to do some work in java on a friends project, I come back, and want to write my own stage 1 bootloader.

So I used bios interrupts to try and load files. Now I have compared to some code I pulled from a tutorial months ago, and mine is VERY bare comparatively, but I do not see any part of their code which looks necessary to actually loading sector to memory and executing.

Please note before answering: I am not looking for code to fix my problem, I would like to make it myself, but I need some direction as to what I am doing wrong(because when I look at reference manual from Intel or Art of Assembler, I see my code looks right to me)

Anyway here is the code I have wrote, i dd both files to raw floppy image.

boot1.s

Code: Select all

BITS	16
ORG	0x7C00
start:	jmp load

Puts16:
	mov	ah, 0x0E
  .loop:
	lodsb
	cmp	al, 0
	jz	.done
	int	10h
	jmp	.loop
	
  .done:
	ret

ResetFloppy:
	mov	si, rstfloppy
	call	Puts16
	xor	ah, ah
	mov	dl, ah
	int	13h
	jc	ResetFloppy
	ret

ReadSectors:	; read sectors to ax:0 (es:bx)
	call	ResetFloppy
	mov	es, ax
	xor	bx, bx

	mov	ah,	02h
	mov	al, 	1	; sectors to load: adjust for stage2 size
	mov	ch,	1	; track #(should make function to translate linear address to chs)
	mov	cl,	2	; sector number(sector to begin reading at)
	xor	dh,	dh	; head 0 (first side of the floppy
	mov	dl,	dh	; drive 0 = floppy
	int	13h		; call bios disk service interupt
	jc	ReadSectors	; epic fail, do that **** again

	ret

load:

	xor	ax, ax
	mov	ds, ax	; do i need this line?
	mov	es, ax	; or this one?

	mov	si, bootmsg
	call	Puts16

	mov	ax, 0x0200
	call	ReadSectors

	jmp 0x0200:0x0	;far jump to execute loaded sector!


bootmsg db 'Stage 1 boot initiated...', 13, 10, 0
rstfloppy db 'Resetting floppy drive.', 13, 10, 0

	times 510-($-$$) db 0
	db 0x55
	db 0xAA
and stage2 stub:

Code: Select all

BITS	16

ORG	0x0200

start: jmp stg2

Puts16:
	mov	ah, 0x0E
  .loop:
	lodsb
	cmp	al, 0
	jz	.done
	int	10h
	jmp	.loop
	
  .done:
	ret

stg2:
	mov	si, msg
	call	Puts16
	cli
	jmp $

msg	db 'now in stage 2!', 13, 10, 0

Re: trying to start from scratch

Posted: Thu Jan 06, 2011 1:21 am
by Tosi
What happens when you try to run it, and what emulator/hardware are you using?

Re: trying to start from scratch

Posted: Thu Jan 06, 2011 1:50 am
by M2004
Are you sure that your second file destination actually equals to
the location you jump right after the loading is completed?

You could try to change the destination (temporalily) to linear adress of 0x7e00
and do a jump there.

regards
Mac2004

Re: trying to start from scratch

Posted: Thu Jan 06, 2011 1:01 pm
by bluemoon
1. You intended to load the 2nd stage sector to 0:0x200 (linear 0x0200) but jump to 0x200:0 (linear 0x02000).
2. You accidentally forget to set the destination (instead you mov ax, 0x0200, which get trashed later)
3. Write to 0x0200 which may be reserved by BIOS is not good idea. A better place maybe 0x7C00+512 = 0x7E00.
4. If you have a really bad floopy drive or disc, the loader may stuck in inf loop, try introducing a chance count - say, 3 tries and INT18.