Initial state of x86 registers after BIOS initialization
Initial state of x86 registers after BIOS initialization
I'm writing a bootloader and found a difference between bochs and actual hardware (Pentium 133). On bochs all segment registers are zeroed, so I can put some code in memory using mov commands, and then run it by simply jumping. However on an actual PC the ds is not 0 and must be zeroed manually.
Is the initial state of registers (both data and segment) somehow standarized (like 0x7c00 initial address), or does it vary from BIOS to BIOS?
Is the initial state of registers (both data and segment) somehow standarized (like 0x7c00 initial address), or does it vary from BIOS to BIOS?
- MichaelFarthing
- Member
- Posts: 167
- Joined: Thu Mar 10, 2016 7:35 am
- Location: Lancaster, England, Disunited Kingdom
Re: Initial state of x86 registers after BIOS initialization
Everything is unreliable with the possible exception of dl. Even cs:ip might vary between 0:7c00 and 7c0:0
-
- Member
- Posts: 501
- Joined: Wed Jun 17, 2015 9:40 am
- Libera.chat IRC: glauxosdever
- Location: Athens, Greece
Re: Initial state of x86 registers after BIOS initialization
Hi,
As for the 0x7C00 initial address, it can be 0x0000:0x7C00, 0x07C0:0x0000, 0x0700:0x0C00, 0x00C0:0x7000, or any other combination of segment:offset with the same effective address.
Hope this helps.
Regards,
glauxosdever
You should almost never assume anything about the initial state of registers. The only known to me exception is the "dl" register, which holds the drive number you should use when invoking "int 0x13".mrrobot wrote:Is the initial state of registers (both data and segment) somehow standarized (like 0x7c00 initial address), or does it vary from BIOS to BIOS?
As for the 0x7C00 initial address, it can be 0x0000:0x7C00, 0x07C0:0x0000, 0x0700:0x0C00, 0x00C0:0x7000, or any other combination of segment:offset with the same effective address.
Hope this helps.
Regards,
glauxosdever
Re: Initial state of x86 registers after BIOS initialization
Thanks! I didn't think about multiple possible encodings of 0x7c00, I guess I wasn't so unlucky then.
- BrightLight
- Member
- Posts: 901
- Joined: Sat Dec 27, 2014 9:11 am
- Location: Maadi, Cairo, Egypt
- Contact:
Re: Initial state of x86 registers after BIOS initialization
Your boot sector code should always start by disabling hardware interrupts (CLI instruction), a far jump to fix the CS:IP combination whatever they may be, setting up the DS, ES, FS, and GS registers, and SS:SP as well. Then, it should store the BIOS boot drive number from DL to a memory location and re-enable hardware interrupts (STI instruction.)
You know your OS is advanced when you stop using the Intel programming guide as a reference.
Re: Initial state of x86 registers after BIOS initialization
In my opinion, there is no need to "fix" the cs:ip register pair. As long as you do not use the cs: data override and always use relative jumps, there is no need to "fix" the cs:ip register pair, ever. Your boot code could really not care any less what the cs:ip register pair is. It knows that it is at absolute address 0x07C00, so as long as the other segment registers are set accordingly, there is no reason to adjust the cs:ip registers.omarrx024 wrote:Your boot sector code should always start by disabling hardware interrupts (CLI instruction), a far jump to fix the CS:IP combination whatever they may be...
Just my opinion,
Ben
http://www.fysnet.net/osdesign_book_series.htm
-
- Member
- Posts: 5587
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Initial state of x86 registers after BIOS initialization
There's also no need to disable interrupts. The CPU automatically prevents interrupts for one instruction following any instruction that sets SS, so you can set SS and SP without disabling interrupts. None of the other steps listed there require interrupts to be disabled at all.
For example:You can use a code sequence like this even with interrupts enabled and there will be no problem.
For example:
Code: Select all
mov ss, ax
mov sp, 0x7c00
- BrightLight
- Member
- Posts: 901
- Joined: Sat Dec 27, 2014 9:11 am
- Location: Maadi, Cairo, Egypt
- Contact:
Re: Initial state of x86 registers after BIOS initialization
OK, let's say for example BIOS loaded me with CS:IP 0x07C0:0x0000 and I have (ORG 0x7C00) in the beginning of my assembly file. When I call a function from within my code (e.g. CALL 0x7C60) the execution will go to 0x07C0:0x7C60, and most likely will execute garbage until the CPU triple faults. The relative jump can only target 127+/128- bytes -- the boot sector can be up to 512 bytes (2048 for CD) and thus fits more than this.BenLunt wrote:In my opinion, there is no need to "fix" the cs:ip register pair. As long as you do not use the cs: data override and always use relative jumps, there is no need to "fix" the cs:ip register pair, ever. Your boot code could really not care any less what the cs:ip register pair is. It knows that it is at absolute address 0x07C00, so as long as the other segment registers are set accordingly, there is no reason to adjust the cs:ip registers.
Conclusion: I stick to my first statement; setting CS:IP is important in the beginning of a boot sector program.
You know your OS is advanced when you stop using the Intel programming guide as a reference.
-
- Member
- Posts: 5587
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Initial state of x86 registers after BIOS initialization
That CALL instruction is encoded with a relative offset, so the destination is 0x07C0:0x0060. There is no problem.omarrx024 wrote:OK, let's say for example BIOS loaded me with CS:IP 0x07C0:0x0000 and I have (ORG 0x7C00) in the beginning of my assembly file. When I call a function from within my code (e.g. CALL 0x7C60) the execution will go to 0x07C0:0x7C60,
JMP is available with 8, 16, and 32-bit relative offsets in real mode (even though a 32-bit offset is useless). There is no problem.omarrx024 wrote:The relative jump can only target 127+/128- bytes
- BrightLight
- Member
- Posts: 901
- Joined: Sat Dec 27, 2014 9:11 am
- Location: Maadi, Cairo, Egypt
- Contact:
Re: Initial state of x86 registers after BIOS initialization
Oops, my bad.
You know your OS is advanced when you stop using the Intel programming guide as a reference.