How to create a standalone C program?

Discussion in 'C Programming' started by dspfun, Jul 31, 2007.

  1. dspfun

    dspfun Guest

    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!
     
    dspfun, Jul 31, 2007
    #1
    1. Advertising

  2. dspfun

    santosh Guest

    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.
     
    santosh, Jul 31, 2007
    #2
    1. Advertising

  3. "dspfun" <> wrote in message
    news:...
    > 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.
     
    Malcolm McLean, Jul 31, 2007
    #3
  4. 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) <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"
     
    Keith Thompson, Jul 31, 2007
    #4
  5. dspfun

    AsifH Guest

    On Jul 31, 12:08 pm, 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!


    This might be of interest to you (although in assembly):
    http://www.omninerd.com/2005/11/05/articles/40
     
    AsifH, Aug 1, 2007
    #5
  6. dspfun

    Ark Khasin Guest

    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
     
    Ark Khasin, Aug 1, 2007
    #6
  7. dspfun

    AsifH Guest

    On Jul 31, 12:08 pm, 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!


    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)
     
    AsifH, Aug 1, 2007
    #7
  8. dspfun

    CBFalconer Guest

    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
     
    CBFalconer, Aug 1, 2007
    #8
  9. dspfun

    dspfun Guest

    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)
     
    dspfun, Aug 13, 2007
    #9
  10. 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
     
    Mark McIntyre, Aug 13, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Hayden

    Undefined reference to memcpy in standalone program

    Richard Hayden, Dec 24, 2003, in forum: C Programming
    Replies:
    7
    Views:
    1,720
    lambda
    Jan 19, 2004
  2. jeuxal_com
    Replies:
    2
    Views:
    443
    Daniel Dittmar
    Mar 2, 2004
  3. Chris Rebert

    Re: How to create Standalone PYC File

    Chris Rebert, Mar 4, 2009, in forum: Python
    Replies:
    0
    Views:
    488
    Chris Rebert
    Mar 4, 2009
  4. Poster28
    Replies:
    9
    Views:
    330
    Poster28
    Apr 20, 2009
  5. Gelonida
    Replies:
    4
    Views:
    573
    Gelonida
    Feb 1, 2011
Loading...

Share This Page