Page 1 of 1

Stupid Boot Code!!!

Posted: Sat Apr 10, 2004 8:31 am
by XStream
Hi,

I know that most of you are probably using GRUB or something to boot your OS, but I am going to be writing my own, I want to learn everything. :)

Ok, So here is the "simple" bit of code that just wont work. The boot sector just loads a few extra sectors from the disk and then I jump to it. I know that this code is working because if all I do is print a single character to the screen the it appears. So here is the extra code:

Code: Select all

;loaded after the boot code
[ORG 0x7E00]
[BITS 16]

; reset video
MOV???AX, 0x0003
INT???0x10

; load text screen layout
CLD
MOV???CX, 2000
MOV???SI, __SCRN_DATA
MOV???AX, 0xB800
MOV???ES, AX
XOR???DI, DI
REP???MOVSW

; halt
JMP???$

__SCRN_DATA   dw 0x07C9
         TIMES 78   dw 0x07CD
      dw 0x07BB, 0x07BA
         etc...
__SCRN_DATA is just a heap of data that when written to video memory puts a border around the screen and displays a title.

So can anyone see my problem??

Re:Stupid Boot Code!!!

Posted: Sat Apr 10, 2004 8:44 am
by BI lazy
where do you jmp to at the end of your precious code?

Do you send your little preciousss cpu down to the hot flows of Orodruin?

Re:Stupid Boot Code!!!

Posted: Sat Apr 10, 2004 8:50 am
by XStream
My precious code

JMP $

is the same as this precious code

_end:
JMP _end

in NASM.

Re:Stupid Boot Code!!!

Posted: Sat Apr 10, 2004 8:52 am
by Pype.Clicker
*grin* that's BI's way to welcome you to the fellowship of the code, trying to save the DPL0 ring ...

more seriousssly, you may want to check if your bootloader can handle as much sectors as required. Make sure you read all the information about INT 13, like the fact it only handle a track at once, etc.

Oh, and btw, if you could describe a bit more *what* you expected the code to do and *what* it actually does, and attach your bootsector code, maybe we could provide more helpful hintz :)

Re:Stupid Boot Code!!!

Posted: Sat Apr 10, 2004 9:03 am
by XStream
No worries pype,

The reason I never posted the boot code was because I know it is working, because I can get the loaded code to print a character to the screen if thats all I do. I think the problem is with the location of __SCRN_DATA.

Code: Select all

[ORG 0x7C00]
[BITS 16]

; setup the stack
     CLI
   MOV            AX, 0x2000
   MOV            SS, AX
   MOV            SP, 0xFFFF
     STI
; reset the boot drive, DL = boot drive
   MOV            AX, 0x7E00
   MOV            ES, AX
   XOR            BX, BX
_RESET_DISK:
   CMP            BYTE [__DISK_TRY_COUNT], 0x06
   JE            READ_DISK_ERROR
   INC            BYTE [__DISK_TRY_COUNT]
   XOR            AH, AH
   INT            0x13
   JC            _RESET_DISK
; try to read sectors 2-16
   MOV            AX, 0x020F
   MOV            CX, 0x0002
   MOV            DH, 0x00
   INT            0x13
   JC            _RESET_DISK
; jump to the full boot loader code
   JMP            0x7E00:0x0000
; come here for disk error
READ_DISK_ERROR:
   JMP            $


__DISK_TRY_COUNT   db 0x00
TIMES 510-($-$$)   db 0
               dw 0xAA55
The other code is as posted above.

Re:Stupid Boot Code!!!

Posted: Sat Apr 10, 2004 9:26 am
by DennisCGc

Code: Select all

JMP            0x7E00:0x0000
Should it not be JMP 0x0:0x7E00 ::) ?

Code: Select all

MOV   AX, 0xB800
MOV   ES, AX
Where do I see DS ?
You should set it to 0x0, because you may not assume DS is 0.

Re:Stupid Boot Code!!!

Posted: Sat Apr 10, 2004 10:01 am
by Candy
DennisCGc wrote:

Code: Select all

JMP            0x7E00:0x0000
Should it not be JMP 0x0:0x7E00 ::) ?
Or jmp 0x7E0:0000, but your point holds.

Code: Select all

MOV   AX, 0xB800
MOV   ES, AX
Where do I see DS ?
You should set it to 0x0, because you may not assume DS is 0.
Very true. Your problem is that DS points to something awkward, it points to either 0 or 0x7C0, of which only the first one is valid for the second sector. So, if you load it to 0x7E00 and then execute it and expect it to work, set DS to 0x0.

Re:Stupid Boot Code!!!

Posted: Sat Apr 10, 2004 10:03 am
by Pype.Clicker
DennisCGc wrote:

Code: Select all

JMP            0x7E00:0x0000
Should it not be JMP 0x0:0x7E00 ::) ?
There's indeed something weird with the way you're loading things. The 'kernel' is loaded at 0x7E00*0x10 + 0 (es=7e00, bx=0) and then you jump at the same location. However, it's not quite correct to have "org 7e00" in the kernel code in that way, as it is at the start (offset 0) of its own segment.

You should either load at 0:7E00 (or 7e0:0, that's the same) and jump at 0:7e00 to some code that had org 7e00 at its start or
load at 7e00:0 (but that's almost 512K far away from your bootsector!) and jump at 7e00:0 with org 0.

Code: Select all

MOV   AX, 0xB800
MOV   ES, AX
Where do I see DS ?
You should set it to 0x0, because you may not assume DS is 0.
I second that. The BIOS doesn't guarantee you DS=0 when calling your bootloader, so you might be using the wrong dataset when writing to video memory.

Make sure your file has been completely written to the floppy, too and that it fits 15 sectors (which should be around 7680 bytes -- or 48 lines of text mode :)

Re:Stupid Boot Code!!!

Posted: Sat Apr 10, 2004 10:10 am
by DennisCGc
Make sure your file has been completely written to the floppy, too and that it fits 15 sectors
Yes, true, because if you load more than 15 sectors at one time the computer crashes, that's what my experience is.

Re:Stupid Boot Code!!!

Posted: Sat Apr 10, 2004 10:33 am
by Pype.Clicker
actually, it is perfectly possible to load more than 15 sectors but you cannot go higher than one track at a time (18 sectors iirc) and cannot request sectors that will span over multiple tracks (thus if you start at sector #2 in the track, only 16 sectors remain readable in one single shot ...

Re:Stupid Boot Code!!!

Posted: Sat Apr 10, 2004 10:48 am
by DennisCGc
oh yeah ::)
but anyway, it doesn't work.
(I just hate int 0x13, but on the other hand, it's handy ;) )

Re:Stupid Boot Code!!!

Posted: Sat Apr 10, 2004 4:55 pm
by XStream
Wow, Information Overload.

Ok, I accept big mistake not to set DS but this was not my problem in boot sector because my BIOS did set it to correct value. By very valid point. :)

When it comes to the loading of extra sectors and jumping to it, if I write a byte to 7E00:0000 this is really physical address 7E000, so
DennisCGc and Candy,
Should it not be JMP 0x0:0x7E00 ?
If I jumped here, I would be going to physical address 07E00, I did try this and it did fail as I thought it would. My extra boot loader is at 7E000.

Pype.Clicker solved the problem, my [org 0x7e00] should have been [org 0] which did work and solve the problem.
Pype.Clicker,
You should either load at 0:7E00 (or 7e0:0, that's the same) and jump at 0:7e00 to some code that had org 7e00 at its start or load at 7e00:0 (but that's almost 512K far away from your bootsector!) and jump at 7e00:0 with org 0. You should either load at 0:7E00 (or 7e0:0, that's the same) and jump at 0:7e00 to some code that had org 7e00 at its start or
load at 7e00:0 (...) and jump at 7e00:0 with org 0.
This was the correct way to describe what I think everyone else was trying to say.
Pype.Clicker,
(but that's almost 512K far away from your bootsector!)
Ahh... My stuff up, It's all becoming clearer now. :)

Also, I have

Code: Select all

TIMES 7680-($-$$)   db 0
at the bottom of my extended boot loader code. This fills it out.
(thus if you start at sector #2 in the track, only 16 sectors remain readable in one single shot)
This would of course be if #0 was the number of the first sector. However I though sectors started at #1?? Which would mean starting at #2 (and including it) would allow you to read 17? Is this correct.

Thanks for all the help guys, you really came through. :) :) :)

Re:Stupid Boot Code!!!

Posted: Sun Apr 11, 2004 3:15 am
by Pype.Clicker
actually, sector #1 is the first on the track, but that's your bootsector too :)