Chapter 1. Simple Example

This example is the most simple program possible. Upon reset, the initialization code generated by the compiler sets the stack pointer, and control jumps to the main() function. The compiler generates an infinite loop with no body for the while(1); statement.

The statement while(1) is the typical way of ensuring that main() never returns. Non-hosted applications can never return from main(); there's nowhere to return to! The behavior of the program (and possibly even of the processor itself) is undefined if main() attempts to return.

Figure 1-1. source code simple.c

/* The compiler generates a jump to main at the reset vector
 * then loops forever */

void main(void)
{
        while(1);
}

    

When you compile this program with the +l option, the compiler generates a listing file. The listing file contains the C source on the right, and the generated assembly on the left; the C source code is spaced to line up with the assembly code generated for each statement. The listing file is the most powerful diagnostic tool you have when trying to debug or analyse your program.

The first column on the left is frequently the ROM address of an assembly instruction, but has other meanings when the compiler is not actually generating code. Depending upon the context, the number is the value of a #define or the address of a declared variable. For assembly code, the next numbers are the opcodes placed in ROM. The middle column is the disassembly of the opcodes. The compiler does not generate an intermediate assembly file: these mnemonics are generated at the same time as the machine code.

Figure 1-2. compiler listing file output simple.lst

                                    /* The compiler generates a jump to main at the reset vector
                                    void main(void)
                                    {
0200 FF        JP     00200                 while(1);
                                    }


ROM USAGE MAP 
    0000 to 0004    00FF to 0100    0200 to 0200

    Total ROM used 0008

      

The ROM USAGE MAP lists which ROM locations the compiler has used. Locations from 0x0000 to 0x0004 are used to initalize the stack pointer and jump to main(). In this case, main() is located at 0x0200 and uses 1 byte of ROM. The ROM used at 0x00FF is the VIS instruction for vectoring to interrupts.