Page 1 of 1

Beginer BootStrap Question

Posted: Tue Feb 01, 2005 6:50 am
by gaiety
I have try to write a bootloader, but for now just try to print a 'A'(Maybe too hard for me as I not familiar with asm), after I copy the bootloader into floopy disk and restart the computer, the computer boot and then display a 'A'. However, the floppy disk LED keep lighting and won't stop. So, I switch off the cpu. After that my floopy disk just needed to format everytimes I use it. Here is my code, so what wrong with it.

System: Real mode
CPU: Pentium 400 mhz
Compiler: Nasm

Here is my code:

Code: Select all

[BITS 16]
[ORG 0x7C00]

jmp short begin

;****************************************************************
;* Floppy header record??????????????????*
;****************************************************************
OEMStr       ???db 'SEAZONE.'               
BytesPerSector  ???dw 512                      
SectorsPerCluster ???db 1                        
ReservedSector   ???dw 1                        
TotalFatCount    ???db 2                        
MaxRootEntries  ???dw 224                      
TotalSectorShort ???dw 2880                     
MediaDescriptor     db 0xF0                     
SectorsPerFat   ???dw 9                        
SectorsPerTrack ???dw 18                       
TotalHeader   ???dw 2                        
HiddenSectors ???dd 0                        
TotalSectorsLong  ???dd 0                        
BootDriver   ???db 0                        
Reserved    ???db 0                       
Signature  ???db 29h                      
VolumeID     ???dd 0                        
VolumeLabel   ???db 'BIG BIG COW'???    
FileSystemType    ???db 'FAT12   '               

begin:
???mov ah, 0x0e
???mov al, 'A'
???mov bx, 0x0007
???int 0x10
cli
hlt

WelcomeMessage db 'Welcome to Seazone Big Big Cow Operating System.', 13, 10, 0
times 512-($-$$)-2 db 0
BootSignature     db 0x55, 0xAA
So what was happen as the code is like other code that print a welcome message. So, what to do?

Thank you.

Re:My floppy just burn out

Posted: Tue Feb 01, 2005 7:22 am
by Pype.Clicker
There's a simple command you need to turn the floppy motor off:

Code: Select all

        mov dx,0x3f2            ; DigitalOutputRegister for Floppy
        mov al,0
        out dx,al               ; stops the motor ...

but that doesn't explain why you'd need "reformatting" ...

1. Does any other floppy Y!=X disk still work after you booted with X?
if not, things are very strange

2. Can you read the floppy X after you installed the bootsector on it but *before* you boot it ?
if not, could you be writing *too much* things on the floppy, erasing FATs and the like ?

3. Could it occur that the data in the BPB doesn't match what's on the floppy before you write your bootsector on it ?

Re:My floppy just burn out

Posted: Tue Feb 01, 2005 10:33 am
by ASHLEY4
gaiety wrote:
Here is my code:

Code: Select all

[BITS 16]
[ORG 0x7C00]

jmp short begin
nop                       ;***** This should be here*****
;****************************************************************
;* Floppy header record                  *
;****************************************************************
OEMStr          db 'SEAZONE.'               
BytesPerSector     dw 512                      
SectorsPerCluster    db 1                        
ReservedSector      dw 1                        
TotalFatCount       db 2                        
MaxRootEntries     dw 224                      
TotalSectorShort    dw 2880                     
MediaDescriptor     db 0xF0                     
SectorsPerFat      dw 9                        
SectorsPerTrack    dw 18                       
TotalHeader      dw 2                        
HiddenSectors    dd 0                        
TotalSectorsLong     dd 0                        
BootDriver      db 0                        
Reserved       db 0                       
Signature     db 29h                      
VolumeID        dd 0                        
VolumeLabel      db 'BIG BIG COW'       
FileSystemType       db 'FAT12   '               
Try it with the above mod eg: "nop"

\\\\||////
(@@)
ASHLEY4.

Batteries not included, Some assembly required.

Re:My floppy just burn out

Posted: Tue Feb 01, 2005 10:51 pm
by gaiety
I have change the code from

Code: Select all

cli
hlt
to

Code: Select all

hang: jmp hang
and it work fine. But I don't like the new code because it will burn out my cpu if it hang for long time. So, I will try to put the code you post. See if it can solve the problem.

I try for a few time and found that the floopy was unable to turn off while and after the hlt command excute. It make the floopy motor keep turning and burn my floopy disk.(not the floppy driver)

There is another few question. So, bother you again.

#1 I know IBM have give the 640K space memory for Dos(I try to create the same operating system of first version of dos). I know it is start from 0x500, But I don't know it is stop at where. Is it 0x9FFFF or 0x?????.

#2 Some computer boot at 0000:7c00 and some start at 07c0:0000. So, for my bootloader, if I have put the code

Code: Select all

[org 7c00h]
then everthing will be start at 7c00h. What I mean is my jump and everthing address will be start at base 7c00h. However, if the cpu boot at 07c0:0000, that mean all my jump and call will be incorect.

I am bit confuse about how jump occur, either jump just use the address of offset to jump to the target addess(Like the targer address is at 1000:7cFF, the jump just jump to 7cff offset and ignore the 1000 segment) or use of combination segment and offset to jump(If the address is 0100:7cff, the jump will use of 0100:7cff to jump). Same confuse with call function.

So, what to do? and which base will be nice to use for coding.

Code: Select all

[org 7c00]
or

Code: Select all

[org 0000]
Thank you for answering my question.

Re:My floppy just burn out

Posted: Wed Feb 02, 2005 12:20 am
by AR
You can do it either way depending on how you prefer to code it, for [org 7c00h] there is usually a bootstrap like this:

Code: Select all

[ORG 7C00h]
    jmp 0:ReloadCS   ;Reload CS so the the Segment starts at 0
    ReloadCS:
    mov ax, cs    ;Load DS to start at 0 as well
    mov ds, ax

    ; ....
The jump statement before the FAT data block should use relative addressing (jump to here+X bytes) so what ORG you have doesn't matter.

As for the hang loop, it doesn't matter, to burn the CPU out you'll need to have the computer overheat and leave it on for a long period of time, it is preferable however to code it like this:

Code: Select all

sti
InfLoop:
    hlt
jmp InfLoop
Basically the CPU will freeze when it encounters the halt until a hardware interrupt needs servicing by the BIOS then it'll hlt again.


Looks like the forum broke when Ashley closed the tags in the wrong order.

Re:My floppy just burn out

Posted: Wed Feb 02, 2005 7:45 am
by thomasantony
Hi,
You can also use jmp begin instead of jmp short begin and eliminate the nop.

Thomas Antony

Re:My floppy just burn out

Posted: Wed Feb 02, 2005 10:36 am
by dh
I notice that some people have the belief that their computers will "burn out" if they do something like jump to the same spot over and over. The fact is, it's much harder to do something like that (if the floppy motor stays on, it could die)

As a refrence to the idea of "burning out", some people use to (and some still do) have little programs that sit around and do nothing but load the system until it does fail (called something like a "burn test" maybe?) with various things like floating point operations. When the limit is reached, the system usually reboots or just halts.

Cheers, DH.

Re:My floppy just burn out

Posted: Wed Feb 02, 2005 11:11 pm
by gaiety
Sorry, still got some new question. I don't know I should put a new topic or continued use this post as it is just little thing. So, if I needed to open a new topic just inform me. This is ready dummy learn to write OS. I need to understand everything.

OK,
so far I have complete my boostrap with ablility to print a welcome message. Here come to another question.

My code was able to call a Print function to print a welcome message. To call a function, I need to setup stack. So, I come with the code:

Code: Select all

cli
mov ax, 0x9000
mov ss, ax
mov sp, 0xffff
sti
It is done, however, I don't understand why I need to deactive the interupt before setup the stack. For my understand, interupt will only function when we call the int function. However, we don't call the int, why should we deactive the interupt, is it something about the timer interupt that will cause the sp to change all what.(May be I get a terrible wrong understand again)

Don't know I will end up with how much question before complete writting bootstrap. So, I think I may open a bootstrap beginner and ask all the question in that post.

Again, Thank you very much for answering my question.

Re:My floppy just burn out

Posted: Thu Feb 03, 2005 1:12 am
by AR
That sort of protection is there to prevent the hardware interrupts from occuring and preventing the stack change from processing correctly (Since the interrupt will transition into the BIOS which may make use of the stack), primarily for the Timer interrupt but the user could also be banging on the keyboard or something.

Re:My floppy just burn out

Posted: Thu Feb 03, 2005 10:42 am
by dh
;P. Don't be sorry, almost 50+% people believe that!

Re:My floppy just burn out

Posted: Thu Feb 03, 2005 11:06 am
by beyondsociety
This is what most people think
The cli/sti pair are being used because they are modifying the Stack Segment (SS) and Stack Pointer (SP) registers. If an interrupt occurs mid-stack segment/pointer change, it's possible that the system will crash. Any Interrupt Service Routines (ISRs), will use your stack (either in full or just briefly) while servicing the IRQ. So it's really important that a valid stack is accessible while external interrupts are enabled.

The safe bet to just to disable external int's, until you have your stack setup.
this instruction: mov ss,ax
disables interrupts until the end of next instruction
to make possible to load ss and sp in two consecutive instructions without disabling interrupts.

Code: Select all

so, this would work for a 386+ processor
mov ss, ax
mov sp, 0xfffff
Actually, CLI/STI on modifying stack is only necessary for 8086/8088 CPUs. All newer Intel CPUs AFAIK do that CLI/STI thing automatically.
From Fasm board

Re:My floppy just burn out

Posted: Fri Feb 04, 2005 4:28 am
by Pype.Clicker
sounds surrealistic, but actually confirmed by the Instruction set reference (p 3-404, order number 243192) ...
Loading the SS register with a MOV instruction inhibits all interrupts until after the execution of the next instruction. This operation allows a stack pointer to be loaded into the ESP register with the next instruction (MOV ESP, stack-pointer value) before an interrupt occurs(1). The LSS instruction offers a more efficient method of loading the SS and ESP registers.

1. Note that in a sequence of instructions that individually delay interrupts past the following instruction, only the first instruction in the sequence is guaranteed to delay the interrupt, but subsequent interrupt-delaying instructions may not delay the interrupt. Thus, in the following instruction sequence: STI MOV SS, EAX MOV ESP, EBP interrupts may be recognized before MOV ESP, EBP executes, because STI also delays interrupts for one instruction.

Re:Beginer BootStrap Question

Posted: Wed Feb 09, 2005 12:49 pm
by gaiety
OK, I have change the title to the suite one. Perviously it is 'My floppy just burn out'

I will put all my question about bootstrap and now it is available to print a welcome message. I am now come to load kernel from floppy. The file system is FAT 12. Here is my question.

#1 How to debug the sector I load. For example, I have load the number 36 sector with bios interupt. How do I check if I load the correct sector. How to check it or debug it. Any idea. This is the first time I use bios interupt to load sector. I always use c++ to load file before.

Thank you for answering my question.

Re:My floppy just burn out

Posted: Wed Feb 09, 2005 7:06 pm
by B.E
Some computer boot at 0000:7c00 and some start at 07c0:0000. So, for my bootloader, if I have put the code

Code: Select all

[org 7c00h]
then everthing will be start at 7c00h. What I mean is my jump and everthing address will be start at base 7c00h. However, if the cpu boot at 07c0:0000, that mean all my jump and call will be incorect.

I am bit confuse about how jump occur, either jump just use the address of offset to jump to the target addess(Like the targer address is at 1000:7cFF, the jump just jump to 7cff offset and ignore the 1000 segment) or use of combination segment and offset to jump(If the address is 0100:7cff, the jump will use of 0100:7cff to jump). Same confuse with call function.

So, what to do? and which base will be nice to use for coding.

Code: Select all

[org 7c00]
or

Code: Select all

[org 0000]
0x0000:7c00 is the same physical address as 0x07c0:0000 because to convert the segment to an offset you would have to multply 0x07c0 by 0x10. which whould give you 7c00.