Evan Clark
Evan Clark

Reputation: 159

Having a compiler in a OS

Hello I have been following the Cambridge Computer Labs: Baking Pi guide where you can build your own simple OS for the ARM CPU for the raspberry pi is assembly. My question is that how would you be able to implement a simple C compiler so i could run basic C programs in the OS (and how would you be able to even compile without the necessary headers? Would you need to implement them your self)

Upvotes: 0

Views: 118

Answers (2)

m0skit0
m0skit0

Reputation: 25873

Well, that's a HUGE task...

  1. Write an ARM assembler. C programs are (usually) translated to assembly then assembled. You need this step anyway for the C compiler, so having a separate assembler is a good idea so you have another tool ready. Assemblers are relatively easy to write.
  2. Write a C compiler (here the fun begins). This should transform C code into assembly.
  3. Build a standard C library for your C program to use. This library has to interface with your OS. I suggest you that your OS follows some OS standard (like POSIX). This will make this part simpler.
  4. Write a linker. This will take object files generated by the assembler and link them with corresponding libraries (if any).
  5. ???
  6. Profit

Upvotes: 3

KrisSodroski
KrisSodroski

Reputation: 2842

You have to compile the c library into an assembly file for the ARM cpu on a different machine. Then you're left with a binary file that you load into memory on the ARM machine, and you set the pc register to the start of the compiler if you want to start running it. You'll have to know the semantics of where pointers are located that point to your source files and such. http://www.osdev.org/ has a lot of information about bootstrapping a c compiler.

Upvotes: 0

Related Questions