Email: Password: Remember Me | Create Account (Free)
Using SDCC on the AT89C52


SDCC on the AT89C52






Overview


I have been using SDCC with the 8052 micrcontroller for a while now, and I have found it to be a very good compiler. Being the tinkering kind of guy I am, I almost always take a peek at the code that it has produced so I can compare it with what I would do.

99% of the time it comes up with pretty good code, it uses jump tables for switch statements, and can optimize away quite alot of redundant code.

I does however, have a few flaws, these are mainly to do with optimisations. This is understandable, because it cannot account for all types of processor that it supports. A prime example is its use of the DPTR register.

SDCC does this to load a 16 bit pointer:


    mov dpl,_pData
    mov dph,(_pData + 1)

When it should do this

    mov dptr,#_pData


Little faults like this can be fixed by using the peephole optimizer.






Peephole what?



The peephole optimizer is part of SDCC , it was originally a standalone package, but is now incorporated into the compiler. What it does it look at a section of code, and see if it can do it any quicker. The Peephole Optimizer uses a set of rules to determine what it can do If it finds a matching rule for a section of code, then it replaces the original code with the new optimized code.


You can specify rules to use with SDCC on the command line by using the --peep-file command line argument. The rules you create must be stored in a file, and the file specified on the command line like this:


    C:> sdcc --peep-file peep.rules myfile.c


This instructs the compiler to use this file to read your custom rules from.

Peephole Rules


The format of a rule file is like this :-


    replace
    {
       mov dpl,%1
       mov dph,(%1 + 1)
    }
    by
    {
       ; peephole custom rule, use DPTR as a single register.
       mov dptr,#%1
    }

This tells the optimizer to change all the inefficient mov's to the single mov. It also places the comment in the file so you know why it is there. There are various other options you can put in the file, which are all fully covered in the manual. Be warned You can easily wreck things by using custom rules which cause bugs in your program. The compiler doesn't check you have done something stupid. so...

BE VERY CAREFUL







Files SDCC Creates

SDCC generates alot of files, here's a quick rundown of each one.

     <sourcefile> .asm - Assembler source created by the compiler
     <sourcefile> .lst - Assembler listing created by the Assembler
     <sourcefile> .rst - Assembler listing updated with linkedit information, created by linkage editor
     <sourcefile> .sym - symbol listing for the source, created by the assembler
     <sourcefile> .rel - Object created by the assembler, input to Linkage editor
     <sourcefile> .map - The memory map for the load module, created by the Linker
     <sourcefile> .ihx - The load module in Intel hex format
     <sourcefile> .cdb - An optional file (with --debug) containing debug information







SDCC Utilities

PACKIHX

The IHX file produced by SDCC is the one 99% of us are interested in, this contains the Intel Hex Format data.

Some programmers have trouble with messy hex files, which can be a problem, SDCC comes with a utility called packihx which packs the hex file into a better format for your programmer to handle

You use packihx like this :-

    c:\> packihx < <sourcefile> .ihx > <sourcefile> .hex
e.g.

    c:\> packihx < test.ihx > test.hex
    packihx: read 129 lines, wrote 65: OK.



USEDMEM

One thing I found it difficult to see is how big your program is unless you're good at adding all the hex digits together in the file. So I wrote myself a little utility to tell me how big the file is.

It's a pretty simple program, all it does is add up the record size of each record entry and give you a total in bytes an Kilobytes, like this:-

    c:\> usedmem < <sourcefile> .ihx > <sourcefile> .hex

e.g.

    c:\> usedmem < test.hex
    704 Bytes (0.69K) Used.


Here is usedmem, feel free to use it yourself.


My Make File

This is the make file I use to build my projects, it can only handle one file, but I have never needed more than that as my program are pretty small anyway. Note that I am piping the code into the programs in a different way to normal, but that is because I am 'spawning' a process from an IDE to do the work for me. This will work on the command line without any changes though.


    @echo off
    PATH=%PATH%;c:\sdcc\bin
    sdcc --peep-file c:\sdcc\bin\peep.rules %1.c
    type %1.ihx | packihx >%1.hex
    type %1.hex | usedmem
    echo.


That's all for SDCC , I have found it to be very good, given that it is a free compiler.



Home