Home arrow Forums
OSDEV Forums  


apple
User

Junior Osdever
Posts: 8
graphgraph
Karma: -1  
MOV problem - 2004/10/12 11:03 I'm developing a boot sector for my future experimental OS. I try to set the segments to 0x7C00 but it doesn't work.

<div class='indent'>
mov cs, 0x7C00
mov ds, 0x7C00
mov es, 0x7C00
mov ss, 0x8000
</div>

How to fix?
  | | The administrator has disabled public write access.
OSDEV
Community
Advertisement
   
root
User

Platinum Osdever
Posts: 121
graphgraph
Karma: 1  
Re: MOV problem - 2004/10/12 11:11 Well, you are not allowed to set directly the segment registers. You may likt to do it using an extra regular register, for example: ax.


mov ax, 0x7C00
mov cs, ax
mov ds, ax
mov es, ax

... and so on.

OK?
  | | The administrator has disabled public write access.
anonymous
Visitor

Fresh Osdever
Posts: 0
graphgraph
Karma: 0  
Re: MOV problem - 2005/01/09 15:07 Several mistakes:

1.) Don't MOV anythung to CS!
That's not working.
Do a far JMP to set CS.

2.) The segment of the bootsector loaded by the BIOS is
0x7C0, not 0x7C00.

So You could do for example:

--------------------
org 0 ;default value, but for clarity

jmp 0x7C0etCS
setCS: mov ax, 0x7C0
mov ds, ax
(etc.)
--------------------

3.) You should set SP immediatly after setting SS, because that's
the onlay crash-safe way. (See intel manuals if You care for details.)
  | | The administrator has disabled public write access.
DRF
User

Platinum Osdever
Posts: 123
graphgraph
Karma: 1  
Re: MOV problem - 2005/01/10 10:04 CS shouldn't be directly modified as it will as the previous poster said mess up the program (CS standing for Code Segment and hence what you would do is force a jump to a static point in memory)

The location that the bootsector is writen to in main memory is:
Segment 0
Offset 7C00
Hence using the location 0x7C00 will work (and is used in the ORG code in many cases)
[ORG 0x7C00] will work though it's not uncommon for people to use ORG 0 and then immediatly do a jump so making the ORG value be correct. (Which is fine as long as you don't add any compiler labels before the jump that corrects the memory registers)

A fail safe way to set your data segment registers in a real mode flat mode program is to use: (Should also work in a bootloader)
mov ax,cs
mov ds,ax
mov es,ax

As your data is in the same location as the code you copy the code segments value and use it for the data segment (this obviously won't apply for the stack segment)

The code I use is:
Code:

; Set up the DS segment register
mov ax, 0x0000 ; Load AX register with current segment
mov ds, ax ; Load DS using the value just put into AX (remember DS doesn't take immediate data)

;;; The stack set up code

; We need to setup SS and SP registers at this point (the stack)
cli ; Clear interrupts while we setup a stack
mov ax,0x9000 ; this seems to be the typical place for a stack
mov ss,ax ; Remember the segment registers can't handle immediate data
mov sp,0xffff ; Use the whole segment.
sti ; Turn the interrupts back on

In my bootsectors I've allways left the code segment register as it was for the code and set the data register as 0x0000 and Stack Segment as 0x9000 and Stack pointer 0xffff. (My bootsectors have been run on a 486 computer a 166mhz intel, a 233mhz intel, a 800mhz AMD and Athlon XP not to mention the other people who have used code from my OS over the years)

The confusion often is in that each location in memory can be addressed in multiple ways as the segments offsets overlap. If anyones interested I could put some examples up here.

Daniel

(Memory addressing is never an easy thing so don't want to go too indepth as I'll have to check things up in a book to be sure I'm not making mistakes, it being a while since I did much bootsector stuff)
  | | The administrator has disabled public write access.
anonymous
Visitor

Fresh Osdever
Posts: 0
graphgraph
Karma: 0  
Re: MOV problem - 2005/12/06 07:44 A safer way than above is to do as previously stated and set ax, 0x7c0... some bioses have CS == 0, but EIP = 0x7c00. In this case, if you had org = 0, your program would not work. I normally set my org to 0x7c00, and all my segment register to 0. Never depend on the bios to set cs = 0x7c0, because it won't always happen.

Just wanted to point out that the 'fail safe way' of doing it above isn't actually fail safe, and is prone to buggy bios'.
  | | The administrator has disabled public write access.
apple
User

Junior Osdever
Posts: 8
graphgraph
Karma: -1  
Re: MOV problem - 2005/12/18 09:36 Thanks everyone for clearing things out.

Tho it has been over an year since I asked is good to know why I messed up my code.

I have some spare time now and I will get back to osdev
  | | The administrator has disabled public write access.
DRF
User

Platinum Osdever
Posts: 123
graphgraph
Karma: 1  
Re: MOV problem - 2005/12/30 21:47 I know how you feel, it's allways hard to start OS dev stuff after a break I find. (the problem is trying to keep so many important figures in your head at once, I always forget what I was doing and need to try and figure out what I need to do next).

Hope this didn't mess up your code too much. If you are having problems grasping the assembly language I recommend you get a book (eg. 'Assembly Step by Step by Jeff Dauntemann' is the one I have, at least I think thats the books title)

Daniel
  | | The administrator has disabled public write access.

A WebArticles site. Sponsored by Evoleto. Motorola V525 / Business Directory / Delaware Incorporation / Home Made Bazaar