5

It's a simple question. If to compile the C compiler is needed a C compiler... Maybe directly with assembly code? Perhaps the kernel provides a basic tool for converting C to assembler and create an escalating infrastructure? It's a stupid question also but I'm really interested in how to design an operating system (not me) from 0 to interact with the CPU and memory.

user1629569
  • 661
  • 1
  • 4
  • 17

2 Answers2

9

Bootstrapping

This comes from the phrase, "pulling yourself up by your bootstraps", if you want to look it up. Bootstrapping is a straightforward process, but it is a lot of work.

  1. Write a C compiler in another language.

  2. Write a C compiler in C.

  3. Use the compiler from step #1 to compile the compiler from step #2.

Some other compilers have to be bootstrapped, not just those for C. For example, GHC must be used to compile itself.

Note that bootstrapping is only necessary if both of the following are true:

  1. You are inventing a new language, so there are no existing compilers.

  2. You want to write the compiler in the language you are compiling.

This has nothing to do with operating systems. If you are designing an operating system, you have enough work ahead of you already. If your new operating system does not have a C compiler yet, you can cross-compile the compiler from a different operating system. This is much less work — maybe a few hours or days, instead of months or years.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
3

It sounds like this is a task for tcc (http://bellard.org/tcc/), the Tiny C Compiler.

It's the smallest C compiler I know of, it can compile a bigger one once you've ported it, and there's numerous guides on how to port it to custom systems.

salezica
  • 74,081
  • 25
  • 105
  • 166
  • Also tcc is simple enough to "compile" it into x86 assembly/opcodes by hand. It's one of the crazy projects I'd like to do someday: Getting an old punch card reader and punch cards, punching them with a manually "compiled and linked" tcc and hooking up that to the LPC interface of a modern computer with a adaptor made from discrete components, just for the sake of it. – datenwolf Feb 05 '13 at 02:25
  • 1
    You have a strange sense of fun, my friend :P – salezica Feb 05 '13 at 04:56