PDA

View Full Version : How do they compile code for an OS ?



Nutshell
03-24-2002, 05:51 AM
Hi,

HOw do you write an OS using C? OR actually how do u write os for practically ANY language? I am not asking for the details. But i was just wondering what compilers people use to compile code for an os. I thought compilers were platform-dependent, and if you're writing an OS you're creating a new platform...

thnx

-KEN-
03-24-2002, 08:59 AM
Well, don't tell anyone, but from one os-dev'r to another:

First, print out yor code. Doesn't matter if it's 500 pages long, print it all. Then seperate each module into its own stack, and you can optionally surround it with a securing device (rubber band). Then pick up the magical shaft of OS-Dev, and dance around the fire singing ceremonial os-dev chants to the os-dev gods. If they smile upon you, great flames should shoot up from your files, and the ashes shall form your kernel. Recompiling is a .........., though...


Or you could go to osdev.org, but that's for sissy newbies. Now excuse me while I compile the keyboard interface ::dons ceremonial mask::

Oh, and go to Flashdaddee's OS-dev forums (http://flashdaddee.com/forums/forumdisplay.php?s=&forumid=20) is you run into any problems. We'll help you out there.

incognito
03-24-2002, 09:04 AM
Originally posted by -KEN-
Well, don't tell anyone, but from one os-dev'r to another:

First, print out yor code. Doesn't matter if it's 500 pages long, print it all. Then seperate each module into its own stack, and you can optionally surround it with a securing device (rubber band). Then pick up the magical shaft of OS-Dev, and dance around the fire singing ceremonial os-dev chants to the os-dev gods. If they smile upon you, great flames should shoot up from your files, and the ashes shall form your kernel. Recompiling is a .........., though...


Or you could go to osdev.org, but that's for sissy newbies. Now excuse me while I compile the keyboard interface ::dons ceremonial mask::

Oh, and go to Flashdaddee's OS-dev forums (http://flashdaddee.com/forums/forumdisplay.php?s=&forumid=20) is you run into any problems. We'll help you out there.


LMAO if this is not true.........it's funny, if not it's kinda scary.

Shiro
03-24-2002, 10:18 AM
>i was just wondering what compilers people use to compile code
>for an os

A good one I've used is the enviroment of Keil. Used that for writing an MicrOS for M68000 and for 8051 programming.

For PC's I think GCC will be a good one. Though note that an OS can't be completely written in C. You'll need some assembly too. I think that NASM is a good one.

Unregistered
03-24-2002, 01:32 PM
Linus Torvalds used the gcc to compile his OS (Linux).
Of course assembly is nescessary, but the gcc also compiles assembly-code. That means that you donīt need any "NASM".

klausi

klausi
03-24-2002, 01:35 PM
I forgot to log in.

incognito
03-24-2002, 02:02 PM
Originally posted by klausi
I forgot to log in.


LMAO I thought that was just funny, specially the thumbs up thing...........hey did you know that back in Roman Time the thumbs up actually meant to kill the opponent after the match was over? Yup, now you know.......well something like that, I read that online and saw it on the History Channel.....but then again you probably dont' care.

klausi
03-24-2002, 03:05 PM
Thatīs what I think about you!

:D

...was only a joke;
nice to know...

klausi

-KEN-
03-24-2002, 06:31 PM
>>Linus Torvalds used the gcc to compile his OS (Linux).

I thought GCC originated on Linux? Or did he write GCC to compile his os?


And hey! The OS-dev gods want more mention from you pleebs! Now bow!


;) :p

Hillbillie
03-24-2002, 07:34 PM
>Recompiling is a *****, though...<

LMFAO...

DavidP
03-24-2002, 07:57 PM
Well, don't tell anyone, but from one os-dev'r to another:

First, print out yor code. Doesn't matter if it's 500 pages long, print it all. Then seperate each module into its own stack, and you can optionally surround it with a securing device (rubber band). Then pick up the magical shaft of OS-Dev, and dance around the fire singing ceremonial os-dev chants to the os-dev gods. If they smile upon you, great flames should shoot up from your files, and the ashes shall form your kernel. Recompiling is a *****, though...


Or you could go to osdev.org, but that's for sissy newbies. Now excuse me while I compile the keyboard interface ::dons ceremonial mask::


So the stories about Microsoft are true!

Imperito
03-24-2002, 10:53 PM
An OS can only be compiled by a compiler written on that OS. Conversely, a compiler can only be written on an OS compiled by that compiler.

blight2c
03-24-2002, 11:26 PM
Originally posted by Imperito
An OS can only be compiled by a compiler written on that OS. Conversely, a compiler can only be written on an OS compiled by that compiler.

lol . . . the reason for your unreasonable treatment of my reason so infuriates my reason that i have reason to doubt your reason

i think cervantes wrote that one

Hillbillie
03-24-2002, 11:36 PM
>An OS can only be compiled by a compiler written on that OS. Conversely, a compiler can only be written on an OS compiled by that compiler.<

Uh, no? :rolleyes:

Prelude
03-24-2002, 11:42 PM
>the thumbs up actually meant to kill the opponent after the match was over
Actually, there were two ways and I believe the Romans used the second. The first was thumbs up and down where down mean kill and up meant mercy. The second was a simple palm up and palm down where down meant kill and up meant mercy. I could very well be wrong about the method, but I do know that down means to kill the opponent.


-Prelude

Nutshell
03-25-2002, 12:50 AM
Originally posted by Imperito
An OS can only be compiled by a compiler written on that OS. Conversely, a compiler can only be written on an OS compiled by that compiler.


It does'nt make sense, if it's like what u said, how is the os compiled in the first place?

shtarker
03-25-2002, 12:54 AM
>>It does'nt make sense, if it's like what u said, how is the os compiled in the first place?

simple:


First, print out yor code. Doesn't matter if it's 500 pages . . . . .

novacain
03-25-2002, 01:20 AM
Its the chicken and the egg all over again.

Which came first, the OS or the PC?

shtarker
03-25-2002, 01:45 AM
Egg.
Its basic evolution.
Birds decended from reptiles.
Reptiles laied eggs.
Therefore egg before chicken.

novacain
03-25-2002, 02:02 AM
How about monotremes(?)?

shtarker
03-25-2002, 02:17 AM
Well working with the assumption that all tetrapods came from Devonian lobe-finned fish and the lack of any contradicting theories or evidence (except creationism), I think egg wins again.

novacain
03-25-2002, 02:33 AM
What about the easter egg and the Easter bunny?

Nutshell
03-25-2002, 02:35 AM
How do u compile once you printed out?

Seriously, pls help, i really want to know the theory.

thnx

shtarker
03-25-2002, 02:42 AM
Well the egg is a symbol of rebirth at easter so has its origins way back with jesus. The easter bunny is some marketing gimic that i doubt goes back much further than the 50's. Egg wins again.

And i do remember someone on this board saying that microsoft have thier own special compiler for windows. (that information is atleast second or third hand by now so don't flame me when you come up with conclusive evidance proving otherwise)

Nutshell
03-25-2002, 03:32 AM
I am talking about as in general. Say if i wrote an OS using C and assembly. Now, what do i do to compile it and actually use it? I believe thats a straight question when it's asked in general terms.

shtarker
03-25-2002, 04:49 AM
http://www.osdev.org/developers/guide01/index.html

There's a guide to mixing C and Assembly to compile an operating system. They suggest using gcc.
It mainly just includes compiling them both into object code and linking them as a final step.
In theory, just about any c++ compiler could be used, provided you specify for it not to play with your code.

Nutshell
03-25-2002, 05:08 AM
Hi thnx,

SO basically i'll write the code in any os and then compiling it using maybe gcc. What will be the output? Will it have any special file extensions? DO i thne copy the whole thing on disk or burn on cd so it'll load up when i reboot the comp or somethin?

thnx

Govtcheez
03-25-2002, 07:26 AM
> The first was thumbs up and down where down mean kill and up meant mercy.

I don't know about your second method, but I believe the reason thumbs down meant to kill was that thrusting the thumb in a downward motion was an imitation of thrusting the sword into the prone foe. Or so I've heard.

Robert
03-25-2002, 05:44 PM
Originally posted by Nutshell


It does'nt make sense, if it's like what u said, how is the os compiled in the first place?

Well, I think they were handcoding. You know, hex things and DEBUG (not MS's, an handcoded DEBUG) which they used for the first HexEditor, which produced the first Interpreter which saved on tape the first 10 commands... But hey, this is ancient history (don't remember if greek of roman). Compiling is a very modern thing, you know, must be mid 80's, or closer.

BTW, they should us computer history! Which one computed the length of a circle?

There was a first programmer which came with the ideea (was not even the programmer-he invented the job), and did a lot of work, like God, did it from nothing!

I might be wrong about hex. First computer was an 8 bit data bus?

OS vs compiler. Tonight, on...

Hillbillie
03-25-2002, 08:30 PM
>What will be the output?<

A file. The format is what you decide on, though the most common are flat binary, COFF and ELF.

>Will it have any special file extensions?<

The file can be of any extension, as in all reality file extensions don't mean anything.

>DO i thne copy the whole thing on disk or burn on cd so it'll load up when i reboot the comp or somethin?<

Depends how your bootloader is set up. If it can read from a FAT disk, you could just copy it over. This all really depends on your bootloader though.

Nutshell
03-26-2002, 02:16 AM
Hi,

Ok now i got more understanding of it.

But i really want to know who created the first OS? And how? How did he write the code? How did he compile it?

And also i asked this question some time ago in this board, and just wondering if any of u guys know it:

How were the C standard libraries written? Who wrote it? Using what language?

Without the C libraries you can almost do nothing in c programming, thats why i wanna know.

thnxq

shtarker
03-26-2002, 02:24 AM
>>But i really want to know who created the first OS? And how? How did he write the code? How did he compile it?

It was probbably written in native code or something similar so he didn't have to compile it.

>>How were the C standard libraries written? Who wrote it? Using what language?

Mixture of c and assembly.
I'd suggest you go out and take a look at some basic assembly, it'll clear all this stuff up for you. . . . and keep you entertained till like 2005.
http://webster.cs.ucr.edu/Page_asm/ArtOfAsm.html

Hillbillie
03-26-2002, 12:34 PM
>But i really want to know who created the first OS? And how? How did he write the code? How did he compile it?<

I can't answer that, and I doubt anyone can for that matter. There's too many platforms out there, along with too many OS's to have a record of the first one. The first one was probably a very, very simple program written in binary. From there, they probably programmed a more sophisticated one. It most likely evolved into an OS.

But, there may be a record of the first true OS. Let me know if anyone finds out. :)

>How were the C standard libraries written?<

Using a compiler or assembler just like any other program.

>Who wrote it?<

Which one? For example Stoned_Coder is writing his own; so am I. There's more than one LibC out there. :)

>Using what language?<

I can only speak for myself. I'm using C and inline assembly. My compiler is DJGPP (DOS port of GCC). Most others are heading this direction as well...

This might interest you: http://www.execpc.com/~geezer/osd/libc/index.htm

Unregistered
03-26-2002, 11:23 PM
Okay guys,

I read all your posts. But why everybody is ending at GCC or some port of GCC.

Let me put something more beyond to this :

compiling means(including linking) first checking for the syntax and then linking with its libraries, this will produce the OBJ code and the obj code is then linked with runtime libraries and is been converted into machine dependent executable. Actually a compiler is targetted towards a target system i.e. how the systems deals with I/O,memory etc.

First thing to create an O/S actually involves in creating a compiler for that O/S. This is something called self compilation. So you write a compiler which compiles it self and produces the compiler. Using that compiler you can start coding your o/s.

For getting started with this method, you need to code some basic logic in assembly of that particular platform. This assembly code will run your first compiler which compiles it self and produces a compiler and creates the runtime libraries which are platform specific.

And hence you got the first chicken which lays eggs;-).

But you dont need work that hard like K&R to produce your first compiler. You will get some environments which will run on one system but can produce the executables targetted to run on another platform. (i think they call them Emulators....i am not sure).

If you need more info search for terms like Yacc, Lex etc.
Oh, ya... do you know what is the full form of Yacc.

--cheers.

Hillbillie
03-26-2002, 11:32 PM
>Actually a compiler is targetted towards a target system i.e. how the systems deals with I/O,memory etc.<

You _can_ (on some compilers) compile as a freestanding module, hence targeting no OS and using no libraries.

>First thing to create an O/S actually involves in creating a compiler for that O/S.<

I'm sorry, unregistered, but I've got an angry posse (hehe, been wanting to use that word.) of fellow OS dev'rs that beg to differ. :)

xds4lx
03-27-2002, 12:37 AM
Im in an os class right now and the way weve been going about it is to use pure asm to achieve a level where we can have basic user interaction, i.e. typing in commands at a command prompt, right now we are learning about implementing a high-level-language compiler into it to allow us to write os code.

Nutshell
03-27-2002, 03:27 AM
ARe there any books out there which talks about inline assembly in C?

shtarker
03-27-2002, 03:30 AM
Yes, but they are all books on assembly. . . . .

Nutshell
03-27-2002, 04:10 AM
ok so i suppose they only put a chapter or two in assembly books on stuff like inline asm in C...

shtarker
03-27-2002, 04:14 AM
No, inline asm is still asm. You need to know how to programme in assembly in order to use it, or just blatantly copy code.

Nutshell
03-27-2002, 04:44 AM
Ok, so inline asm is the same as asm, but there are special braces or brackets or keywords around the code to indicate they are asm code, and is to be compiled in another way.

okok

klausi
03-27-2002, 11:44 AM
@KEN:
>I thought GCC originated on Linux? Or did he write GCC to compile his os?

Linus Torvalds first used Minix (which was an UNIX-like OS for x86-computers developed by Andrew Tannenbaum) on his 386-PC to develop his terminal-emulation-program which became Linux then. He compiled it with the GNU GCC-compiler. The GNU-tools for UNIX existed at this time yet and stood under the GPL.
After having crashed Minix one day, he portated the GCC to Linux and used from that day on only his own OS.

klausi

Sebastiani
03-27-2002, 01:10 PM
Since we're on the subject, so in theory, you could compile a program "simply" by generating a pattern of bits which in turn are the actual representation of machine binaries? And so what really does it mean to "run" the program? That is, there must be a program that the OS runs to "run" executables-how does it work, differentiating from "opening" or "reading" or "writing" a file, what then does "running it" actually mean, to the OS, that is? And what is the name of that program on a windows machine? I would love to look it up...

Right now I am working on the parse-end of a compiler. I am dreading the next step, asm. Nonetheless, this is the most interesting program I've worked on yet! Anyone know the BEST book to read on ASM that is readable and interesting?

Shiro
03-27-2002, 01:29 PM
>I read all your posts. But why everybody is ending at GCC or some port of GCC.

It's a great compiler, free, open source and there are ports for many OS'es.

>compiling means(including linking)

Hope you don't mean that compiling includes linking. Linking is a different process. A compiler produces object code, this can be a compiled C-file. To fullfill the process, there is need to link the object code with libraries so externals will be solved. If an external can't be found, the linker will tell you about unresolved externals.

>the obj code is then linked with runtime libraries

Hmmm. Isn't a runtime library linked at runtime? Or were that the dynamic link libraries?

>First thing to create an O/S actually involves in creating a compiler for that O/S.
>This is something called self compilation. So you write a compiler which compiles it self

How does a compiler compile itself?

I've written a few simple OS'es for microcontrollers, used crosscompilers for that.

shtarker
03-27-2002, 03:02 PM
Originally posted by Sebastiani
Since we're on the subject, so in theory, you could compile a program "simply" by generating a pattern of bits which in turn are the actual representation of machine binaries? And so what really does it mean to "run" the program? That is, there must be a program that the OS runs to "run" executables-how does it work, differentiating from "opening" or "reading" or "writing" a file, what then does "running it" actually mean, to the OS, that is? And what is the name of that program on a windows machine? I would love to look it up...

Right now I am working on the parse-end of a compiler. I am dreading the next step, asm. Nonetheless, this is the most interesting program I've worked on yet! Anyone know the BEST book to read on ASM that is readable and interesting?

I think it involves calling the main function.
Although thats a very simplistic view of the process, there's probbably a lot more in the form of threading and whatnot.

Govtcheez
03-27-2002, 03:16 PM
> I think it involves calling the main function.

What main function? At the bit level, there's no main function.

Hillbillie
03-27-2002, 03:50 PM
>And what is the name of that program on a windows machine? I would love to look it up...<

What exactly do you mean by "name"? Filename? Internal application name?

>And so what really does it mean to "run" the program? That is, there must be a program that the OS runs to "run" executables-how does it work<

The OS transfers control of the PC to the program. For how long? Depending on how the tasking model of the OS is set up. Maybe a few milliseconds, or perhaps even until the program finishes executing.

>Right now I am working on the parse-end of a compiler.<

Interesting. Good luck! :)

>I think it involves calling the main function.<

Generally speaking, it actually calls a function named _start() which in turn calls main(). Of course, like shtarker said, this is a broad description of what happens.

>What main function? At the bit level, there's no main function.<

Govt, you're a C/C++ coder right? I'm sure you're familar with pointers then. As you should know, a pointer is simply a variable that holds a memory address. Function names in C/C++ are similar - they are an identifier for a section of code. main() doesn't get called (because as you said in the bit level, there is no main), but the code that was previously known as main() does (with a jmp instruction).

Nutshell
03-27-2002, 11:30 PM
Shiro, did u mean a compiler can't compile itself? Then how'd u compile a compiler so that compiler can compile other programs?

shtarker
03-27-2002, 11:37 PM
>>Shiro, did u mean a compiler can't compile itself? Then how'd u compile a compiler so that compiler can compile other programs?

ummm. . . . .with a compiler?

Nutshell
03-28-2002, 12:16 AM
um..............okok