Page 1 of 1

Moving from real mode to protected mode

Posted: Thu Jan 09, 2014 4:57 am
by hometue
Hi guys, I am now programming my code and what I am doing now is I am going to switch to protected mode (yeah, I know, if you check my OS its been a lotta commits yet I am still on real mode #-o ...moving on :arrow: ). However, before I do so, I want to know how different it is coding for real mode and coding for protected mode (I heard data access is different but how is it different in C, also any other things I need to take note of? :?: ) Thanks :)

Re: Moving from real mode to protected mode

Posted: Thu Jan 09, 2014 8:35 am
by Love4Boobies
The point of using abstract machines, like C, is that they have their own rules which implementations map to the underlying environment (this mapping is called an ABI). In other words, your code should look the same regardless of which mode your CPU is in. It's just that your compiler will need to target whatever environment you wish to run your code under. However, any assembly code will obviously be different.

Re: Moving from real mode to protected mode

Posted: Thu Jan 09, 2014 9:03 am
by hometue
Ah...I see. Btw, to switch to protected mode is it fine to just put it in inline assembly(since I know C compiler will change it slightly so will this affect the result?). Also is there a way to check if the change went successfully (any address in the bios data area or any register to check). Thanks so much (since it seems to me that it is really important to switch to protected mode) :D .

Re: Moving from real mode to protected mode

Posted: Thu Jan 09, 2014 9:45 am
by Love4Boobies
hometue wrote:Ah...I see. Btw, to switch to protected mode is it fine to just put it in inline assembly(since I know C compiler will change it slightly so will this affect the result?).
I would recommend separate files for your assembly code for a couple of reasons. First, your source code will be easier to maintain if and when you decide to support multiple architectures. Secondly, it will give you the opportunity to use various C compilers (since the "asm" keyboard is a non-standard extension, it's often implemented in incompatible ways). Regarding the compiler modifying your assembly, some do others don't so be sure to check its documentation.
hometue wrote:Also is there a way to check if the change went successfully (any address in the bios data area or any register to check).
Why do you assume this is something that can fail, provided you write your code correctly? If it does fail, you'll probably be in enough trouble that you won't be able to check anything. And even if you won't, you'd need to have a pretty unusual setup in order for your protected mode code to have the same meaning as it would in real mode. You'd need to run the same code in both cases, right?

Also, the BIOS will probaby be unaware you changed to protected mode.