Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to create a standalone C program?

Reply
Thread Tools

How to create a standalone C program?

 
 
dspfun
Guest
Posts: n/a
 
      07-31-2007
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!

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      07-31-2007
dspfun wrote:

> 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.

 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      07-31-2007

"dspfun" <> wrote in message
news: oups.com...
> 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.


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-31-2007
santosh <> writes:
[...]
> 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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
AsifH
Guest
Posts: n/a
 
      08-01-2007
On Jul 31, 12:08 pm, dspfun <dsp...@hotmail.com> wrote:
> 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

 
Reply With Quote
 
Ark Khasin
Guest
Posts: n/a
 
      08-01-2007
dspfun wrote:
> 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
 
Reply With Quote
 
AsifH
Guest
Posts: n/a
 
      08-01-2007
On Jul 31, 12:08 pm, dspfun <dsp...@hotmail.com> wrote:
> 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...M_PC_bootstrap)

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-01-2007
Ark Khasin wrote:
>

.... 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.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
dspfun
Guest
Posts: n/a
 
      08-13-2007
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/wg...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)



 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      08-13-2007
On Mon, 13 Aug 2007 07:38:54 -0700, in comp.lang.c , dspfun
<> wrote:

>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
simplest way to create simple standalone wsgi server without importwsgi_lib.server Gelonida Python 4 02-01-2011 11:30 PM
Create standalone Windows program with simple graphics? Poster28 Python 9 04-20-2009 11:04 AM
Re: How to create Standalone PYC File Chris Rebert Python 0 03-04-2009 07:43 PM
MN-730 standalone driver & WPA ofelas Wireless Networking 0 12-26-2004 07:28 PM



Advertisments