13

I just wonder, can we execute a program on a machine without an operating system?

Besides, I heard that the Linux kernel is written in C language and a kernel is run during booting, so I just wonder how a computer understand the language without first compiling?

6
  • 1
    There's nothing bare about a machine that can boot Linux or Windows. It needs a BIOS. Jan 20, 2010 at 10:17
  • 1
    @nobugz: What makes you think that the BIOS isn't "software"? I mean, most BIOS chips are flashable, so I can rewrite it if I really want to... Jan 20, 2010 at 17:45
  • That was very much my point. The opposite of "bare". Jan 20, 2010 at 17:56
  • 1
    @nobugz: but the BIOS is software and it runs on the bare metal. Every computer always has something running on bare metal. Every single one. Every single time. Jan 20, 2010 at 18:26
  • 1
    @caramel1991, they say "flash" because the BIOS is stored in a kind of non-volatile memory device called flash memory. Before that, it was stored in a non-volatile device called EPROM, which could be erased and reprogrammed, but required a bright UV light to do the erasing, and a separate programmer device to write it. FLASH is quite the improvement since it can be erased and reprogrammed in-system.
    – RBerteig
    Sep 1, 2010 at 7:18

8 Answers 8

12

From Wikipedia:

When a computer is first powered on, it does not have an operating system in ROM or RAM. The computer must initially execute a small program stored in ROM along with the bare minimum of data needed to access the nonvolatile devices from which the operating system programs and data are loaded into RAM. The small program that starts this sequence of loading into RAM, is known as a bootstrap loader, bootstrap or boot loader. This small boot loader program's only job is to load other data and programs which are then executed from RAM.

The computer can understand the Linux kernel because it has already been compiled and stored (usually) on disk. The bootloader gives the computer enough functionality to load the precompiled kernel into memory.

You wouldn't need to load a whole operating system to run a program on a computer, you could write a bootloader to kick off a program you had compiled. You would not have access to any of the operating system calls that make life easier for programmers.

1
11

In short, yes.

Linux is still a software program, in machine code, that runs on a bare metal machine. While you can execute a software program without an operating system, your program will need to implement ALL the code that is used to talk to various pieces of hardware in a computer to various degrees - e.g. outputting data to a display, interpreting input from a keyboard / mouse / network card etc. (Some of the very low level stuff are implemented by the firmware in computer components, but the rest your program will have to implement). This makes it very time-consuming and difficult for you to write something that runs entirely without an operating system.

8

Yes, and it is done today for small microcontrollers with a few KB of memory.

The program is typically written in C and compiled on some other computer (that is called cross-compiling) and then loaded as binary data into the flash memory of the controller.

8

the linux kernel might be written in C. It is still compiled to machinecode. And it is this machine code which is executed during boot

You can also write software which is run during a boot. This way you can make your own custom OS, or make your own custom software which can run without an OS directly. Beware though, that an OS gives you a lot of functionality which you'll have to make yourself. Things like driver support, disk IO routines, networkstacks, multitasking and memory management you'll have to do yourself.

Finally: I don't think people don't like it that much if they have to reboot their machine in able to run your software. So I'd go with writing for an OS... it makes live easier on you and the user.

8
  • Users could use virtualization (and indeed many DRM proponents are seduced by the very idea of not relying on the host OS -- the "trusted computing" initiative). Jan 20, 2010 at 8:39
  • But of course, you can't totally trust the hardware either... DRM just isn't possible. Jan 20, 2010 at 9:42
  • 1
    So that's mean every operating system like windows is a software??Then how bout the day before operating system was created??And does a bootloader need to be written in assembly low level language?? Jan 20, 2010 at 9:54
  • yes, an OS is a piece of software too. It is a piece of software which allows other pieces of software to run together and use it's functionality.
    – Toad
    Jan 20, 2010 at 10:01
  • As for the days before operating systems were created: you manually loaded a program into memory somehow and told the computer to start executing from location 0. (which is still what it does today, but the thing it starts executing is the boot strapper)
    – Pod
    Jan 20, 2010 at 10:25
4

What is an operating system if not software running on a "bare" machine? Voodoo? XD

1
  • 5
    Don't know about voodoo, but Windows definitely does some necromancy somewhere along the line. Aug 29, 2010 at 21:39
2

1st: Sure. You don't really need an operating system just to burn some cycles.

You might need some kind of OS support if you want to load or store files or data, manage input or output, but this can also be done calling BIOS functions directly: read key from keyboard, write to some screen or LED or serial interface. Only when you want to run multiple programs, or deal with interrupts from outside, conflicting ressources or such, then you will desperately need an OS.

2nd: The kernel is compiled to machine code, which is executed during boot. There's no C involved when running a kernel. C only helps writing a kernel or any program which should run, if in the kernel or "bare metal".

1

Just look at any games console prior to the 32 bit ones. Almost all of them lacked any boot code at all, and simply booted directly from the inserted cartridge.

1
  • Slight correction: while some consoles lacked any sort of internal ROM, including the most brilliant game console of all time (the Atari 2600), it's not accurate to say they "almost all" did. The Intellivision, Odyssey2, Bally Astrocade, and Atari 7800 all preceded the 32-bit era, and had an internal boot ROM (all start executing code from internal ROM, even though some jump to cartridge space before confirming that a cartridge exists)
    – supercat
    Jan 31, 2011 at 23:30
0

theoretically, you can build bootloader by using hex editor on another machine.

Not the answer you're looking for? Browse other questions tagged or ask your own question.