OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 5:16 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Bootloader - How to choose the segment
PostPosted: Wed Aug 09, 2017 7:58 pm 
Offline

Joined: Wed Aug 09, 2017 7:41 pm
Posts: 3
Hello,

I have started to read courses / articles about OS devlopment and I do not understand a choice in bootloader dev.

The bootloader must be placed at address 0x7C00, ok. And in severals articles they do :

Code:
[BITS 16]
[ORG 0x0]

    mov ax, 0x07C0
    mov ds, ax
    mov es, ax


There is a reason to choose ds and es registers ? Or is it possible to use the cs register for instance ?

I am not an asm expert, I have the basics, but I try to get the right way.

Really thank you :)


Top
 Profile  
 
 Post subject: Re: Bootloader - How to choose the segment
PostPosted: Wed Aug 09, 2017 10:42 pm 
Offline
Member
Member
User avatar

Joined: Sat Dec 27, 2014 9:11 am
Posts: 901
Location: Maadi, Cairo, Egypt
I'll assume you know how segmented memory addressing is done in real mode. The x86 has 6 segment registers: CS, DS, ES, FS, GS and SS.

CS is the code segment register. It is used to read instructions that the CPU executes, by the pointer CS:IP. DS is the data segment register. It is the default register used to read data in string operations (lodsb, movsb, etc) as well as reading/writing data to memory, with instructions such as:
Code:
mov ax, [0x8000]

This instruction reads a WORD from DS:0x8000 into AX. This can be overridden, if you like.
Code:
mov ax, [fs:0x8000]

This instruction reads a WORD from FS:0x8000 into AX.

ES is the extra segment register. It is the default register used to write data in string operations (stosb, movsb, etc) as well as reading/writing to memory with the DI register, unless specified otherwise.
Code:
mov ax, [di]

This instruction reads a WORD from ES:DI into AX.

FS and GS are unused by for real mode and protected mode, but have some uses in long mode.

SS is the stack segment register. It is used by the CPU to point to the current position in the stack, by the pointer SS:SP.

Back to the boot sector, the BIOS loads the boot sector into linear address 0x7C00. There's probably thousands of segment:offset combinations that point to 0x7C00, and while most BIOSes set CS:IP to 0x0000:0x7C00, you can never be too safe. So, you can reload CS if you like. Any data you have within the boot sector will also be located within this address range, and you don't know what the BIOS has in the segment registers. As such, you set them up to values that suit you, as well as set up a stack by changing SS:SP, to allow BIOS and hardware interrupts to occur.

_________________
You know your OS is advanced when you stop using the Intel programming guide as a reference.


Top
 Profile  
 
 Post subject: Re: Bootloader - How to choose the segment
PostPosted: Thu Aug 10, 2017 5:01 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
wammder wrote:
I have started to read courses / articles about OS devlopment

Beware! Most example bootloaders have bugs. Often, they are difficult to debug because they work in emulators but fail on real hardware.

omarrx024 wrote:
This instruction reads a WORD from ES:DI into AX.

No, the default segment does not change when you use DI. That instruction reads a word from DS:DI into AX.

The default segment changes from DS to SS when you use BP (and sometimes EBP).
Code:
mov ax, [bp]

This instruction reads a word from SS:BP into AX.


Top
 Profile  
 
 Post subject: Re: Bootloader - How to choose the segment
PostPosted: Thu Aug 10, 2017 5:22 am 
Offline

Joined: Wed Aug 09, 2017 7:41 pm
Posts: 3
Thank you, I really appreciate it !
I found a link to go deeper in your explications, because I do not have all the details about real mode

Thank you !


Top
 Profile  
 
 Post subject: Re: Bootloader - How to choose the segment
PostPosted: Thu Aug 10, 2017 5:27 am 
Offline

Joined: Wed Aug 09, 2017 7:41 pm
Posts: 3
Excuse me Octocontrabass but what did you mean by : Most example bootloaders have bugs ?

Currently I do not read articles about OS dev to dev my own OS but just to try to understand how it really works


Top
 Profile  
 
 Post subject: Re: Bootloader - How to choose the segment
PostPosted: Thu Aug 10, 2017 5:41 am 
Offline
Member
Member

Joined: Thu Aug 13, 2015 4:57 pm
Posts: 384
wammder wrote:
Thank you, I really appreciate it !
I found a link to go deeper in your explications, because I do not have all the details about real mode

Thank you !

Intel gives you manuals about x86 for free. If you are interested only in real mode then you go and get the 386 manual, it is shorter and simpler than the later manuals because the later manuals have more details about newer features.


Top
 Profile  
 
 Post subject: Re: Bootloader - How to choose the segment
PostPosted: Thu Aug 10, 2017 7:17 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
wammder wrote:
Excuse me Octocontrabass but what did you mean by : Most example bootloaders have bugs ?

The initial environment is inconsistent between PCs, so it's easy to write code that relies on something behaving a particular way, when in reality it's not guaranteed to behave that way on every PC. For example, a common mistake is to use string instructions without clearing the direction flag first. While it may appear to be a safe assumption that the direction flag will be clear when the PC boots, it's actually unspecified, so any bootloader that relies on that assumption will fail on some PCs.


Top
 Profile  
 
 Post subject: Re: Bootloader - How to choose the segment
PostPosted: Mon Aug 14, 2017 6:01 am 
Offline
Member
Member

Joined: Wed Aug 09, 2017 7:37 am
Posts: 80
wammder wrote:
Hello,

I have started to read courses / articles about OS devlopment and I do not understand a choice in bootloader dev.

The bootloader must be placed at address 0x7C00, ok. And in severals articles they do :

Code:
[BITS 16]
[ORG 0x0]

    mov ax, 0x07C0
    mov ds, ax
    mov es, ax


There is a reason to choose ds and es registers ? Or is it possible to use the cs register for instance ?

I am not an asm expert, I have the basics, but I try to get the right way.

Really thank you :)


You should learn x86 assembly language before even stepping into OS development. Learn user space first. Write some programs in your operating system (Windows or Linux for example).

You might also want to learn C.

The link below is the Intel Manual and should help you get started. The 10 volume set manuals would be good. Start reading a little bit of basic architecture. If you need help on instructions, use the instruction set reference and find the instruction (Control+F or Command+F will help).

You should be familiar with memory addresses. Little endian means starting from the least significant byte.
For example, 0xAABBCCDD (a made up memory address) is big endian and 0xDDCCBBAA is little endian. It's reversing the bytes around.

The most significant byte would be 0xAA and the least would be 0xDD.

Little endian is used in memory.

When you read Basic Architecture, the most important sections are around 3.1 and 3.4.
1.3 is also very important (apart from 1.3.5, that might confuse beginners).

https://software.intel.com/en-us/articles/intel-sdm

If you want to learn about BIOS interrupts, learn DOS Assembly. Please be warned that 16-bit mode (Real Mode) has many limitations. BIOS interrupts are deprecated. Learn with it, then get away from it.

You may also want to check out Ralf Brown's Interrupt List.

Learn about registers, segments, instructions and the environment of x86.

You don't need to understand everything but here
Instructions to look at would be

MOV (essential, the most used instruction)
ADD (adding numbers. can be useful)
INC (short for using ADD to add 1 to a register, increment by 1)
DEC (decrement by 1)
SUB
DIV
STOSB (very useful, stores byte from AL to SI).
LODSB (very useful as well, reads byte from SI to AL)
INT (interrupt)

You might like to read Section 6 of Basic Architecture as well.


Top
 Profile  
 
 Post subject: Re: Bootloader - How to choose the segment
PostPosted: Mon Aug 14, 2017 6:22 am 
Offline
Member
Member
User avatar

Joined: Fri Jan 27, 2017 12:15 pm
Posts: 149
Location: Belgium
,,learn assembly'' says the guy who is not able to read two sectors

_________________
AQUA OS: https://obiwac.wordpress.com/aqua-os/


Top
 Profile  
 
 Post subject: Re: Bootloader - How to choose the segment
PostPosted: Mon Aug 14, 2017 6:32 am 
Offline
Member
Member

Joined: Wed Aug 09, 2017 7:37 am
Posts: 80
obiwac wrote:
,,learn assembly'' says the guy who is not able to read two sectors


I'm moving away from Real Mode slowly. I already know about 0xb800 and PS/2 keyboards. INT 10h and INT 16h gone ;)
INT 13h... well... these things prove difficult.

By the way, your other post. It's Geri's writing and the fact he has misleading information. You can access 32 bit registers in 16 bit.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: 8infy, Bing [Bot], DotBot [Bot], Majestic-12 [Bot] and 63 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group