# A simple C program

## About this recipe

This recipe gives you a simple exercise in using the ARM Software Development Toolkit (the toolkit) to write a program in C. By following it, you will learn how to:

- use the ARM C compiler armcc to create a runnable program;
- use the ARM source level debugger armsd to run your program on a (simulated) ARM system;
- use armcc to compile a C program to an object file;
- use the ARM linker armlink to create a runnable program from an object file and the ARM C library.

## Prerequisites

Before you can try this recipe, the toolkit must be properly installed on your computer. Instructions for installation are given in the installation notes distributed with every toolkit. If you experience any difficulties, please refer to these notes.

## Making a simple runnable program

The "Hello World" program shown below, is included in the on-line examples as file hellow.c in the directory examples:

``

    #include <stdio.h>

    int main( int argc, char **argv )
    {
        printf("Hello World\n");
          return 0;
    }

If you set your working directory to be the examples directory you can compile this program to runnable form in a single step using:

``

    armcc hellow.c -li -apcs 3/32bit

### Explanation

The argument -li says that the target is little endian and -apcs 3/32bit says that the 32 bit ARM procedure call standard should be used. If the compiler has been configured to use these options by default then these arguments need not be given. The executable program is left in a file called hellow.

## Running the program

You can run the program (technically an AIF Image) using armsd. You should follow the sample dialog below:

``

    host-prompt> armsd -li hellow
    A.R.M. Source-level Debugger, version 4.10 (A.R.M.) [Aug 26 1992]
    ARMulator V1.20, 512 Kb RAM, MMU present, Demon 1.01, FPE, Little
    endian.
    Object program file hellow
    armsd: go
    Hello world
    Program terminated normally at PC = 0x000082a0
          0x000082a0: 0xef000011 .... : >  swi     0x11
    armsd: quit
    Quitting
    host-prompt>

### Explanation

The -li argument to armsd tells it to emulate a little endian arm. If armsd has been configured to be little endian by default then -li can be omitted .

When armsd comes up with its "armsd:" prompt and waits for your command, you should type "goCR". At the next prompt type "quitCR" to exit armsd.

## Separate compiling

You can invoke the compiler and the linker separately. You can use:

``

    armcc -c hellow.c -li -apcs 3/32bit

to make an object file (in this example called hellow.o, by default).

### Explanation

The -c flag tells the compiler to make an object file but not to link it with the C library.

## Separate linking

When you have finished compiling, you can link your object file with the C library to make a runnable program using:

``

    armlink -o hellow hellow.o somewhere/armlib.321

Where we have written somewhere, above, you must type the name of the directory containing the ARM C libraries.

### Notes

You now have to be very explicit; you must specify:

- the name of the file which will contain the runnable program (here, hellow);
- the name of the object file (here, hellow.o);
- the location and name of the C library you wish to use.

In simple cases, armcc can reduce the need to be so explicit.

## Related topics

Please refer to the index to find topics of particular interest.