Hi,
You tell the assembler that all offsets will begin at 0x7C00, here:
Code:
org 0x7c00 ; BIOS boot origin
However, here, you place an unknown value into ds, es, and ss.
Code:
main:
cli ;Clear interrupts
;Setup stack segments
mov ax,cs
mov ds,ax
mov es,ax
mov ss,ax
sti ;Enable interrupts
;Print the first characters
mov si, Message
call Println
At this point, cs can be anything. Since you told the assembler all offsets were at 0x7C00, you need the segment portion to be 0x0000.
For example, let's say that on entry to your code, the BIOS has placed the value of 0x07C0 in to CS -- A very common value for a BIOS. With this in mind, the processor actually takes:
Code:
mov si, DS:Message ; DS: is implied (actually, implied later in the PrintLn routine)
call Println
and coverts the physical address to:
Code:
mov [ds:si],(0x07C0 << 4) + (0x7C00 + offset of Message)
Hence, the physical address used is actually:
Code:
0xF800 + offset of Message
Same thing happens on the call instruction. Even though the call is a relative call, the assembler thinks that the offset of "PrintLn" is somewhere past 0x7C00. If CS is 0x07C0, the physical call could easily be within the BIOS somewhere.
Even though many tutorials and other web sites say that you need to set CS and/or set DS (and ES) to CS, they are wrong. You have no idea what CS will be. It is completely up to the BIOS POST code to set CS as long as the CS:IP pair points to physical address 0x07C00.
The best thing to do is to set DS (and ES, SS, etc) to 0x07C0 and tell the assembler to use an offset of 0x0000.
Code:
org 0x0000 ; BIOS boot origin
xor ax,ax
mov ds,ax
mov es,ax
mov ss,ax
mov sp,0x7C00
Don't even touch CS. Don't "extract" the value from it, and definitely don't set it to something. As long as you don't use a CS: override or use other instructions that use the CS segment register, you don't even care what CS:IP is. Besides, in a boot sector (the first stage code), you would never use any instructions like this anyway.
Ben
-
http://www.fysnet.net/osdesign_book_series.htm