How to create a standalone C program?

D

dspfun

How do you create a standalone C program? With standalone C program I
mean it should run "freestanding" on a CPU without an OS or other
supporting software/libraries linked to the C program. After the bios/
setup has done its initializations and "stuff" I would like to be able
to continue execution with my own C program. The C program will be
really simple to start with, for example just write an integer value
to a memory address.

Thanks!
 
S

santosh

dspfun said:
How do you create a standalone C program? With standalone C program I
mean it should run "freestanding" on a CPU without an OS or other
supporting software/libraries linked to the C program. After the bios/
setup has done its initializations and "stuff" I would like to be able
to continue execution with my own C program. The C program will be
really simple to start with, for example just write an integer value
to a memory address.

Thanks!

You should probably ask this question in a group for your system. Generally,
some amount of native assembler code will be required to set up the
environment that C expects. Also you'll have to find some way to make your
system's firmware load your program into memory and pass control to it. The
details are completely system specific and off-topic for this group. Also
you need to take special steps to compile your program as a freestanding
one. In the process you'll lose access to most functions of the Standard
library, and might have to implement your own replacements.

I suggest the following groups:

alt.lang.asm, comp.lang.asm.x86 and alt.os.developement.

Though your question is not exactly topical for any of the above groups,
they might be able to answer your question better, since they can discuss
system specific topics.
 
M

Malcolm McLean

dspfun said:
How do you create a standalone C program? With standalone C program I
mean it should run "freestanding" on a CPU without an OS or other
supporting software/libraries linked to the C program. After the bios/
setup has done its initializations and "stuff" I would like to be able
to continue execution with my own C program. The C program will be
really simple to start with, for example just write an integer value
to a memory address.
Don't include any libraries.
The snag is that you've then go no IO. You need to write to hardcoded memory
address, possible in C, or generate interrupts, for which you need a spot of
assembly.
Then somehow you've got to get a compiler that can be persuaded not to
include normal start-up code before main(). This is probably possible with
most comercial compilers, but it will need a certain amount of delving into
unexplored depths of documentation to understand how to do it. On non-hosted
implementations, which is effectively what you are using the PC as,
normally entry is vai a special function called "boot" or similar.
 
K

Keith Thompson

santosh said:
I suggest the following groups:

alt.lang.asm, comp.lang.asm.x86 and alt.os.developement.

Though your question is not exactly topical for any of the above groups,
they might be able to answer your question better, since they can discuss
system specific topics.

or perhaps comp.arch.embedded, but check its archives and FAQ first.
 
A

AsifH

How do you create a standalone C program? With standalone C program I
mean it should run "freestanding" on a CPU without an OS or other
supporting software/libraries linked to the C program. After the bios/
setup has done its initializations and "stuff" I would like to be able
to continue execution with my own C program. The C program will be
really simple to start with, for example just write an integer value
to a memory address.

Thanks!

This might be of interest to you (although in assembly):
http://www.omninerd.com/2005/11/05/articles/40
 
A

Ark Khasin

dspfun said:
How do you create a standalone C program? With standalone C program I
mean it should run "freestanding" on a CPU without an OS or other
supporting software/libraries linked to the C program. After the bios/
setup has done its initializations and "stuff" I would like to be able
to continue execution with my own C program. The C program will be
really simple to start with, for example just write an integer value
to a memory address.

Thanks!
A freestanding *C program* is not really different from a hosted C
program, except that the standard guarantees the existence of only a
small subset of the library functions; your compiler may but doesn't
have to provide more. (There are details like main() but that's beside
the point.)

A true difference comes from how the program is built as an executable.
1. There is magic that starts your program
2. There is magic that creates the C runtime environment once the
program starts
3. There is an implementation of the (available) library functions,
notably I/O and malloc if available.

In many cases, those things come with a compiler capable of or dedicated
to making freestanding applications /of some sort/.

If e.g. you are writing code for a dishwasher, the #1 magic must start
from the CPU reset (which starts your program), which also means your
program must be linked at an absolute address.

If you are writing PC BIOS extension, you can rely on your program being
started by BIOS scan, by which time some basic BIOS services may become
available.

If you convince your OS loader that your program is the default command
shell, you probably have everything the OS provides, except system().

So scenarios vary depending on the level of abstraction from the hardware

-- Ark
 
A

AsifH

How do you create a standalone C program? With standalone C program I
mean it should run "freestanding" on a CPU without an OS or other
supporting software/libraries linked to the C program. After the bios/
setup has done its initializations and "stuff" I would like to be able
to continue execution with my own C program. The C program will be
really simple to start with, for example just write an integer value
to a memory address.

Thanks!

These might be of interest to you:
http://www.omninerd.com/2005/11/05/articles/40
http://en.literateprograms.org/Hello_World_(IBM_PC_bootstrap)
 
C

CBFalconer

Ark said:
.... snip ...

If you convince your OS loader that your program is the default command
shell, you probably have everything the OS provides, except system().

Including system(), which just returns a failure indicator, as
specified in the standard.
 
D

dspfun

What I meant with standalone is the same as "freestanding" mentioned
in the C standard.
From the c standard document
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

6
The two forms of conforming implementation are hosted and
freestanding. A conforming
hosted implementation shall accept any strictly conforming program. A
conforming
freestanding implementation shall accept any strictly conforming
program that does not
use complex types and in which the use of the features specified in
the library clause
(clause 7) is confined to the contents of the standard headers
<float.h>,
<iso646.h>, <limits.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, and
<stdint.h>. A conforming implementation may have extensions (including
additional
library functions), provided they do not alter the behavior of any
strictly conforming
program.3)
 
M

Mark McIntyre

What I meant with standalone is the same as "freestanding" mentioned
in the C standard.

A freestanding implementation is normally one that runs on embedded
hardware such as the chip inside a car engine management system or
mobile phone. You would need specific cross-compilation tools for that
hardware3.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top