Executing Sector By Sector

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator:Moderators

Post Reply
atif
Posts:4
Joined:Mon May 05, 2008 6:58 pm
Executing Sector By Sector

Post by atif » Mon May 05, 2008 7:11 pm

Hi all
Can i execute my code sector by sector?
Let me explain
Mike "Crypter" showed how to find and load second stage bootloader("Operating Systems Development" till part 6).
Rather than having any file system on the floppy drive i would have my code in a series of instructions starting from Sector 0 to last sector (ofcourse 1st one require boot signature).
BIOS will load first sector and start execution. The first sector will be responsible for doing its part of job and load 2nd sector, 2nd sector will do its job and load 3rd sector and so on...
Definitely it is not a good idea :)
Regards

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Post by Andyhhp » Tue May 06, 2008 11:13 am

Hi,

There is certainly nothing preventing you doing that.

However, If each sector is going to load the next, most of each sector will be only loading the next, leaving you very little space to have any other code.

If you really want to execute code without having a filesystem (I still don't understand this and would strongly recommend that you have a filesystem), you could make a file similar to stage2.bin which is a flat, multi-sector file that you could copy to sector 2 of the disk. Then your bootloader can load the correct number of sectors and just jump to the beginning. This would waste far less space than using individual sectors.

Andrew
Image

atif
Posts:4
Joined:Mon May 05, 2008 6:58 pm

Post by atif » Tue May 06, 2008 11:25 am

I already tried it out but failed
Following is the code i used from BonaFide to load second sector

; Code to load the second sector on the disk into memory location 0x2000:0x0000
mov bx, 0x2000 ; Segment location to read into (remember can't load direct to segment register)
mov es, bx
mov bx, 0 ; Offset to read into
mov ah, 02 ; BIOS read sector function
mov al, 01 ; read one sector
mov ch, 01 ; Track to read
mov cl, 02 ; Sector to read
mov dh, 01 ; Head to read
mov dl, 00 ; Drive to read
int 0x13 ; Make the BIOS call (int 13h contains mainly BIOS drive functions)

and after calling this code i simply used
jmp 0x2000:0x0

but the code was not jumping to this location, it simply halted

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Post by Andyhhp » Tue May 06, 2008 11:35 am

Hi,

That is because the Cylinder and Head numbers start from 0.

i.e.
First sector is at CHS value 0:0:1
Second sector is at CHS value 0:0:2

therefore, your code should be:

Code: Select all

mov ch,0
mov dh,0
Currently, you are probably just loading 0's off the disk and triple-faulting.

Andrew
Image

atif
Posts:4
Joined:Mon May 05, 2008 6:58 pm

Post by atif » Tue May 06, 2008 11:44 am

ok, i will try it, i don't have the code right now with me
Thanks

atif
Posts:4
Joined:Mon May 05, 2008 6:58 pm

Post by atif » Wed May 07, 2008 6:12 pm

No dear, it is not working :(
I have copied the code, hope you will find the blunder :)
I used NASM to assemble these as

nasmw -f bin boot.asm -o boot.bin
nasmw -f bin boot2.asm -o boot2.bin

and then i used partcopy to copy boot.bin on sector 1 and boot2.bin on sector 2 of a virtual floppy drive

partcopy boot.bin 0 200 -f0
partcopy boot2.bin 0 200 -f0 201

;##################################################################
;Copied from BrokenThorn.com
;Stage 1
org 0x7c00
bits 16
Start:
jmp loader

msg1 db "Welcome to My Operating System!", 0
msg2 db "Loading Stage 2.", 0
msg3 db "Stage 2 Loaded.", 0

;=========================================================================
Print:
lodsb
or al, al
jz PrintDone
mov ah, 0eh
int 10h
jmp Print
PrintDone:
ret
;=========================================================================

;=========================================================================
; Code to load the second sector on the disk into memory location 0x2000:0x0000
;Copied from BonaFide.com
LoadSec:
mov si, msg2
call Print
mov bx, 0x2000 ; Segment location to read into (remember can't load direct to segment register)
mov es, bx
mov bx, 0 ; Offset to read into
mov ah, 02 ; BIOS read sector function
mov al, 01 ; read one sector
mov ch, 00 ; Track to read
mov cl, 02 ; Sector to read
mov dh, 00 ; Head to read
mov dl, 00 ; Drive to read
int 0x13 ; Make the BIOS call (int 13h contains mainly BIOS drive functions)
ret
;=========================================================================
loader:
xor ax, ax
mov ds, ax
mov es, ax

mov si, msg1
call Print

call LoadSec

mov si, msg3
call Print
jmp 0x2000:0x0

cli
hlt

times 510 - ($-$$) db 0
dw 0xAA55
;##################################################################
;##################################################################
;Stage 2
org 0x2000
bits 16
start: jmp loader

msg db "Welcome to Stage 2", 0

;==================================================================
Print:
lodsb
or al, al
jz PrintDone
mov ah, 0eh
int 10h
jmp Print
PrintDone:
ret
;==================================================================

loader:
xor ax, ax
mov ds, ax
mov es, ax

mov si, msg
call Print

cli
hlt
times 512 - ($-$$) db 0
;##################################################################

Andyhhp
Moderator
Posts:387
Joined:Tue Oct 23, 2007 10:05 am
Location:127.0.0.1
Contact:

Post by Andyhhp » Thu May 08, 2008 11:36 am

Hi,

Firstly iirc, your partcopy instructions should be:

Code: Select all

partcopy boot.bin 0 200 -f0
partcopy boot2.bin 0 200 -f0 200
else you might get 1-byte misalignment on the second sector.

Can you be more specific about how broken it is.

Even which messages it prints out is a bonus because you can track the (possibly only first) bug to between the bit of code that prints the last error message and the bit of code that prints the first missing error message.

Due to the lack of debugging support on a bootloader, this is almost always by far the easiest way.

Andrew
Image

Post Reply