A simple C program, and how to compile it


Here is a simple C program for the HC12

#define COUNT 5

unsigned int i;

main()
{
    i = COUNT;
}    
To compile the program, make a linker file simple.lkf:
#    link command file for test program
#
+seg .text -b 0x0800 -n .text    # program start address
+seg .const -a .text             # constants follow code
+seg .data -b 0x0900             # data start address
crts.o                           # startup routine
simple.o                         # application program
+seg .const -b 0xffce            # vectors start address
+def __memory=@.bss              # symbol used by library
+def __stack=0x0a00              # stack pointer initial value
You need a C startup file crts.s
;   C STARTUP FOR MC68HC12
;   
;
    xdef    _exit, __stext
    xref    _main, __memory, __stack
;
    switch  .bss
__sbss:
    switch  .text
__stext:
    lds #__stack    ; initialize stack pointer
    jsr _main       ; execute main
_exit:
    swi             ; Return to D-Bug 12
;
    end
You can use the following batch file cc.bat to compile the program, create a simple.h12 file to load into the Zap simulator, and a simple.s19 file to load into the HC12:
cx6812 -vl -p -xx -a -xx crts.s %1.c
echo Linking ...
clnk -o %1.h12 %1.lkf
echo Converting ...
chex -o %1.s19 %1.h12
echo Generating absolute listing ...
clabs %1.h12
Here is the machine code generated by the compiler:

   1                     ; C Compiler for MC68HC12 [COSMIC Software]
   2                     ; Version V4.2g - 13 Oct 1998
  29                     ; 5 main()
  29                     ; 6 {
  30                         switch    .text
  31  0815               _main:
  35                     ; 7     i = COUNT;
  37  0815 cc0005            ldd    #5
  38  0818 7c0000            std    _i
  39                     ; 8 }    
  42  081b 3d                rts    
  65                         xdef    _main
  66                         switch    .bss
  67  0000               _i:
  68  0000 0000              ds.b    2
  69                         xdef    _i
  70                         end