
How to run a program without an operating system? - Cieplak
https://stackoverflow.com/questions/22054578/how-to-run-a-program-without-an-operating-system/32483545#32483545
======
emilfihlman
>Technically, a program that runs without an OS, is an OS.

It is not. A program that runs without an OS just doesn't need the OS provided
services but instead manages everything itself.

A perfectly valid program might be:

    
    
        #include <avr/io.h>
        
        int main(void)
        {
            DDRB=255;
            while(1)
            {
                PORTB++;
            }
        }

~~~
hartator
Your valid program is still a system that operates your hardware directly. So,
still an OS?

~~~
lostgame
An OS, by general definition, usually contains the ability to launch
applications developed for it, using the high-level API’s the OS provides to
greatly simplify low-level hardware functionality, such as playing sound or
displaying images.

A program, by definition, is a running system which executes specific
instruction sets, whether this is from a low-level, no-OS setup, such as an
NES, or a MacOS program which makes extensive use of Core Image, Core
Animation, Core Audio, et cetera.

These are both programs. But only one of them actually runs in an OS.

------
CoolGuySteve
If you ever programmed a ROM for a Gameboy or something this should look
familiar. The BIOS of those old systems just jumps right into a certain
address and all you have to do is make sure your linker is setup properly to
put crt0.s there.

The Gameboy Advance in particular is pretty fun and simple, I recommend it.

~~~
shshhdhs
Where should one go to get started with GBA dev work these days?

~~~
raptorfactor
[http://www.gbadev.org/](http://www.gbadev.org/)

------
Communitivity
For any "How do I...X?" question involving connecting two pieces of software
together in a different way, the first thing I try to do is look at the API(s)
that connects them. In the case of a program and the OS, it's the system
calls, device drivers, and standard libraries.

There have been efforts to provide the capability of running a program without
an OS before, but any such effort is going to need to provide the system calls
and standard libraries used by the program, and the infrastructure to support
it (device drivers, management, etc.). That that point it becomes a mini-OS.

An example is Erlang on Xen. Xen is more often run with a guest OS running
inside it, and then the program runs within that guest os. The
[http://erlangonxen.org/](http://erlangonxen.org/) folks made Ling
([https://github.com/cloudozer/ling](https://github.com/cloudozer/ling)),
software that enables an Erlang BEAM VM to be run directly on Xen, and thereby
run a single Erlang program on Ling.

------
beached_whale
Saw a talk on Include OS that is a C++ library that allows one to do this
[http://www.includeos.org](http://www.includeos.org)

~~~
cirosantilli
A similar effect can be achieved with Newlib, which is mentioned on the post,
and exemplified at: [https://github.com/cirosantilli/linux-kernel-module-
cheat/tr...](https://github.com/cirosantilli/linux-kernel-module-
cheat/tree/54e15e04338c0fecc0be139a0da2d0d972c21419#baremetal-setup-getting-
started) You might need to implement some syscalls though. Or similarly you
can also use an open source embedded system like FreeRTOS or Zephyr which
essentially implement the syscalls for you.

~~~
perbu
IncludeOS is modeled much like FreeRTOS or Zephyr. The difference is mainly
that it addresses CPUs and not microcontrollers as the primary platform. With
paging, you have better security, typically.

~~~
beached_whale
Along those lines, a bit, I have always wanted to play with targeting linux
but making ones program the init process. All the resources but the benefit of
Linux's ability to run on a lot of platforms. Not micro controller though,
well arm, but that is pretty large.

~~~
cirosantilli
This is trivial with Buildroot btw, see e.g.:
[https://github.com/cirosantilli/linux-kernel-module-
cheat/tr...](https://github.com/cirosantilli/linux-kernel-module-
cheat/tree/54e15e04338c0fecc0be139a0da2d0d972c21419#init)

And a minimal hello world at:
[https://unix.stackexchange.com/questions/122717/how-to-
creat...](https://unix.stackexchange.com/questions/122717/how-to-create-a-
custom-linux-distro-that-runs-just-one-program-and-nothing-else/238579#238579)

------
sureaboutthis
People forget that an operating system is, itself, just a program.

~~~
JdeBP
It is usually more than one program.

* [https://superuser.com/a/329479/38062](https://superuser.com/a/329479/38062)

------
Arainach
For another take on the subject, see "This OS is a bootloader" in PoC||GTFO
0x4:

[https://www.alchemistowl.org/pocorgtfo/](https://www.alchemistowl.org/pocorgtfo/)

There's a sequel in 0x5 that explains how to add a basic form of
multiprocessing support.

~~~
cirosantilli
I have also covered baremetal multicore at:
[https://stackoverflow.com/questions/980999/what-does-
multico...](https://stackoverflow.com/questions/980999/what-does-multicore-
assembly-language-look-like/33651438#33651438) The exemple is present in the
same repo: [https://github.com/cirosantilli/x86-bare-metal-
examples/blob...](https://github.com/cirosantilli/x86-bare-metal-
examples/blob/3c35cabb8934d0dd92974f5826c8815572831bea/smp.S)

------
justincormack
The Osdev wiki is a great resource on this type of thing
[https://wiki.osdev.org/Expanded_Main_Page](https://wiki.osdev.org/Expanded_Main_Page)

~~~
cirosantilli
I only wish they would create a repo with all their examples like I did :-)

------
babuskov
Not directly related to the topic, but I wrote a simple puzzle game that
emulates a computer which executes programs directly in the memory and you can
modify memory to rewrite running programs. Here's a web-based demo if anyone
wants to check it out:

[https://roguebit.bigosaur.com/](https://roguebit.bigosaur.com/)

------
lostgame
A lot of people here seem to be questioning the usefulness of this, why
someone would want to do this, et cetera.

Embedded systems are one of the most extremely relevant uses of this today.

However, historically, I think my first case of programming without an OS
would be ASM development for the Sega Genesis?

Another addition is that I saw a comment ‘a program that runs an OS, is an
OS.’

This is absolutely not true. An Operating System is a multi-faceted, complex
piece of software, usually with the capacity to run other pieces of software
within it, as well as usually offering high-level API’s to improve ease-of-use
for developers to access common functionality, such as playing sound, or
displaying images.

A ROM for the NES, while a program, is certainly not an OS, and to be very
clear, the NES does not have an OS, like I believe the PS4 and XB1 have.

It contains a certain number of hardware features that can be accessed and
manipulated by low-level software functions.

------
amelius
Now try to do it on a smartphone.

~~~
0xTJ
I really wish it were possible to do this kind of thing on smartphones.
Unfortunately, odds are that anything you try will quickly brick it, and best-
case scenario you can restore it using something like the low-level Broadcom
interface

------
nmstoker
Would be interesting to hear discussion on why one might consider doing this.

My take is that it's most likely just being asked for the theoretical details,
and few people would practically benefit, but does anyone think this would be
practically useful?

Presumably you'd potentially cut overhead from anything you didn't explicitly
want happening on the host, but as soon as I start to think about most of the
uses typically have, the advantages quickly sink below the disadvantages of
having to actually manage everything. It's reminiscent of that person who set
out to build a toaster from scratch.

~~~
Isamu
>does anyone think this would be practically useful?

Most embedded software projects I worked on did not have an operating system.
You'd just have C or C++ program and some ASM to initialize everything.

I would expect really small IoT devices would be the same. With your average
single chip device I wouldn't bother trying to get an operating system on
there.

I have only just started to use a Raspberry Pi and it does have an operating
system - the advantages there are you can pull in IP networking and run
services and do much more complex things than on the bare metal.

------
jancsika
> printf '\364%509s\125\252' > main.img

It appears that bash `printf` wraps coreutils printf "with ARGUMENTs converted
to proper type first," according to the manual. I guess that's how the "%509s"
specifier is able to work without any args.

So how does bash `printf` actually determine the proper arg types? Is
coreutils printf implemented with hooks that let wrappers fetch the proper arg
types without generating an output string? Or does bash `printf` implement its
own separate parser for the format specifier?

~~~
kevinoid
> It appears that bash `printf` wraps coreutils printf "with ARGUMENTs
> converted to proper type first," according to the manual. I guess that's how
> the "%509s" specifier is able to work without any args.

Actually, coreutils printf follows POSIX/SUS which states that "Any extra b,
c, or s conversion specifiers shall be evaluated as if a null string argument
were supplied".[1] So that trick will work for many printf(1) implementations
(including both bash and coreutils).

Bash printf(1) doesn't have any hooks or wrappers, but it does parse the
format string to determine how to convert arguments before calling libc
vsnprintf(3). If you are interested in the details, check out how %d/%i is
handled in bash[2] and coreutils[3].

1\.
[http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pr...](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html)
(item 9 under Extended Description)

2\.
[https://git.savannah.gnu.org/cgit/bash.git/tree/builtins/pri...](https://git.savannah.gnu.org/cgit/bash.git/tree/builtins/printf.def?h=bash-4.4#n598)

3\.
[https://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob;...](https://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob;f=src/printf.c;hb=v8.30#l374)

~~~
jancsika
Thanks for the links, those are really helpful!

Why didn't libc include a printf function that expands a format specifier with
a pointer to a byte array (and/or possibly an array of c strings)? That way
there would at least be a standard way of handling arbitrary input and
erroring out with less risk of buffer overflows and stuff.

------
finchisko
Very nice post. However I thought it would be about writing programs that
could be executed from OS, but OS would be completely unaware that such a
program is running. Not sure if that is even possible.

I know it’s possible to not use libc or similar, but os is aware of such a
program.

~~~
davrosthedalek
In some way, this is what TSR (terminate stay resident) programs did under
DOS. They were started by the OS, exited, but some of the code was still
around as part of IRQ handlers and such. DOS didn't really know about that.

~~~
finchisko
Yes, I had exactly this on my mind. DOS era.

------
andrewstuart
Stack Overflow is for very specific questions with very specific answers.

You'll get better results for more general questions from a reddit forum.

~~~
sureaboutthis
There are some fairly decent subs on reddit but, generally, I find it mostly
populated by 15-year old kids and others who have never had a job in the
industry so, no, I would not refer anyone to reddit for information.

~~~
softawre
How can you tell their age?

I've always though if a forum like this had a little age indicator (or
experience indicator, or something like 'this person is smart') it would be a
lot more useful.

~~~
sureaboutthis
It's strange that I can tell by the way they write. I'm not always correct but
many times I'd find someone writing as if they were an expert but you could
just tell they were only regurgitating something they read. They couldn't make
a proper connection between things they were saying. I'd trick them in a way
to reveal their age and way too often it was 15 to 17 years old. Sometimes 18
or 19.

You can also read them saying they wanted to take CS when they graduated from
school so you can glean from that they are probably in high school.

Or I just flat out ask them (or accuse them).

~~~
beaconstudios
> many times I'd find someone writing as if they were an expert but you could
> just tell they were only regurgitating something they read.

that's 90% of HN too though.

------
vectorEQ
i always feel these questions are answered to best effort of a person just
starting out in the field of os / bare metal programming. there is a whole
plethora of information missing in this post crucial to x86/64 early
development which will get new programmers of this type into trouble if they
don't become aware of it.

i'd be careful following such advices. for example this person isn't aware you
really need to build a cross toolchain for your OS to compile to your target.
on 64-bit there's some more things to take into account about compilers (red-
zone?). even if you go all assembly your assembler will probably be an
optimising one and shoot you in the back on more than one occasion! Thats just
the tool-chain surface issues (deeper ones ignored... like file formats..?
binary is fun, but good luck scaling that up to a complex project... bios
can't load you up that high in memory and limited amounts...)

Next you get to things omitted which on baremetal would cause problems, like
no mention of power management and such topics. or even assumptions that your
code is loaded at 0x0000:0x7c00 while it could be loaded at that address but
via a different CS value, completely screwing you over if you don't flush it
immediately on your mbr...

Take an example of opengl tutorials. people tell you to write your first
polygon because they just managed to do so. They forget to mention to throttle
the loop, so you burn out your VGA card rendering 10000000000fps of 1
triangle.... good job!

this tutorials are kind of like that. fun to put into qemu which is sort of
'safe' learning and get acquainted but really doesn't do what it says on the
box.

I'm not against these type of tutorials, i like that more people become
interested in this, but really take some time and state the scope of the
offered information properly. like this is a bit of clickbait / misinformation
which is a shame

------
everyone
Prompted a fascinating answer. 'closed as too broad' \-- modern Stack overflow
in a nutshell.

~~~
yesenadam
Yeah, I hate that. I've asked a few questions on those sites and came back
later, excited to read what answers I got and...it had been closed
immediately, before anyone could answer it. GRRRR!

Maybe there could/should be a 'broad' SE site alongside every 'normal' one?
The 'too broad' questions include most of the interesting ones to read. The
ones that are 'intellectually interesting'.[0] It's a shame. I love how you
can ask anything on HN without being shut down like that. It's so unfriendly,
hurtful even. (At worst on here, no-one answers - no harm done.) Why they
can't just let 'too broad' questions go for a day or two to see what responses
come in, and delete the page if it sux, I don't know. The mods get points for
closing things, I guess. Often they don't understand the question, it seems.

[0] e.g. [https://mathoverflow.net/questions/43690/whats-a-
mathematici...](https://mathoverflow.net/questions/43690/whats-a-
mathematician-to-do)

Well, bizarrely the close notice on that says "This question is unlikely to
help any future visitors; it is only relevant to a small geographic area, a
specific moment in time, or an extraordinarily narrow situation that is not
generally applicable to the worldwide audience of the internet." Which seems
absurd. (Although sure, the _question_ doesn't help people, the _answers_ do
though.)

~~~
Arnt
Some neat questions are too broad.

But the too-broad questions aren't usually very interesting. Most of them are
just pure crap. If you have enough SO karma, try
[https://stackoverflow.com/review/close/?filter-
closereason=t...](https://stackoverflow.com/review/close/?filter-
closereason=toobroad) — what I see just now is someone who wants help with his
java/spring thing and isn't very detailed about it, someone who's "been trying
to learn how delegation" works, someone who "would like to know how can i mock
diffrent user request behavior pattern" and those aren't unusual. The
intellectually interesting questions are unusual in the "too broad" morass.

If people are to moderate SO, the rules have to be simple and quick to apply.
Of course people don't have to do that, but I fear that without moderation,
the interesting questions would get no answers because they would be
completely flooded by questions like those three. So the open-ended,
intellectially interesting questions lose either way.

~~~
yesenadam
Well, yeah, I assume most non-broad questions are pure crap too. But they
don't close all of the non-broad questions. (Or even a lot of the awful ones -
I have tried to answer a few horrifyingly badly written Qs on math SE, that
no-one else dared to go near, which weren't closed for months, for some
reason) So I can't see how that was so relevant to talk about that.

So the answer is, There Is No Alternative? There's no possible way things
could be different in that respect?

~~~
Arnt
The close reasons ("too broad" and the others) are good descriptions of the
typical bad questions. Anything which doesn't fit into any of the dozen-odd
close reasons is not typically a bad question.

This summer I waited on a lot of six- and even twelve-minute batch jobs, and I
spent some of those on the SO moderation queue (I'm one of the 3000-karma
users, not an elected moderator). It didn't take much more than six minutes to
learn that picking the interesting questions out of the flood of manure is a
terrible chore.

It would be nice if the intellectually interesting questions could be left
alive. But I don't think it's humanly achievable. People _will_ post their job
interview quizzes and first-year CS exercises and say, oh, mentally
stimulating and please do leave open.

~~~
wild_preference
Also, what made this interesting wasn't the question but that the question won
the lottery of someone contributing substantial time to craft a great answer.

I don't think you could've done a better job than linking that moderation
queue of "toobroad" to make your point. It was a never-ending channel of
straight shit. Ranging from "here's a copy and paste of all my unformatted
code that won't work, whatever that means" to "how do i use python on
windows".

Everything looks easy to someone that has no stake in the solution. We get so
used to being end-users that we take everything for granted, as if our
experience is the natural ordering of the universe, rather than the result of
all the work that goes on behind the scenes.

Read enough posts from HNers condemning Stack Overflow and you'd think they
believe Reddit/HN's "new" queue is the ultimate browsing experience.

------
stevefan1999
This is wrong, you'd still have a operating system: BIOS

~~~
klez
Maybe it's just my limited knowledge on the subject, but it seems to me it's a
bit too much to consider the BIOS an operating system. At that point, what
isn't an operating system? You're still writing code against a microcode
interpreter even once you remove the BIOS. Is that an OS too?

~~~
da_chicken
I would agree. The BIOS/UEFI POSTs the system, and then passes execution to
another program (usually a bootloader). At that point, the BIOS is no longer
performing the tasks that an OS does. It's not scheduling tasks for the
processor or handling I/O at all. Instead, it switches to what's basically an
ACPI interface with functions that can be called by the OS. It's like an old
TSR program.

~~~
vectorEQ
the BIOS sets up SMM handlers which do run. also there is ACPI which runs
partially outside of the os's control....

