Compiling for pic16f877a

  • Rax

    Rax - 2008-09-07

    I'm new to SDCC. I want to compile this sample program:

    #include <pic/pic16f877a.h>
    typedef unsigned int word;
    word at 0x2007 __CONFIG = ( _WDT_OFF & _BODEN_OFF  & _HS_OSC &  _LVP_OFF );

    void main(void) {
        TRISA = 0x00;//Puerto A como salida
        PORTA = 0b10101010;
        while( 1 ){


    the assembler code gives me this:
    ; File Created by SDCC : free open source ANSI-C Compiler
    ; Version 2.8.3 #5229 (Sep  6 2008) (UNIX)
    ; This file was generated Sat Sep  6 22:35:55 2008
    ; PIC port for the 14-bit core
    ;    .file    "ProbandoSDCC.c"
        list    p=16f877a
        radix dec
        include ""
    ; config word
        __config 0x3f3a
    STARTUP    code
        pagesel __sdcc_gsinit_startup
        goto    __sdcc_gsinit_startup

    ;; Starting pCode block
    _main    ;Function start
    ; 2 exit points
    ;    .line    19; "ProbandoSDCC.c"    TRISA = 0x00;//Puerto A como salida
        BANKSEL    _TRISA
        CLRF    _TRISA
    ;    .line    20; "ProbandoSDCC.c"    PORTA = 0b10101010;
        MOVLW    0xaa
        BANKSEL    _PORTA
        MOVWF    _PORTA
    ;    .line    22; "ProbandoSDCC.c"    while( 1 ){
        GOTO    _00106_DS_

    What means BANKSEL?. The .hex file generated have a size of 1.2KB! too big!.
    I'm compiling with the command

    sdcc -mpic14 -p16f877a ProbandoSDCC.cç

    and --opt-code-size have almost no effect(2 o 3 instuction less). How this program should be compiled correctly?

    • William Gebers

      William Gebers - 2008-09-07

      BANKSEL_ is a macro that ensures the correct memory bank in the micro is selected.  The PIC architecture allows you to switch between memory banks to access the correct special function registers and this macro performs any changes that are required.

    • Rax

      Rax - 2008-09-07

      Thanks for your info about BANKSEL. But I'm still worry about the excesive code size. Is this just an startup routine? Is this overhead constant or will increase with my program size.
      In the same .asm file says this program is just 12bytes long, so why the overhead?

      • Oliver Sedlacek

        Oliver Sedlacek - 2008-09-08

        The startup code is a function that runs before the C environment is initialised. This is where you can configure hardware such as watchdogs and extended RAM that need to be set up before main() gets called.

        You really shouldn't worry too much about the code size of trivial programs. The important thing has to be to get your code to work correctly, and for non-trivial programs that is easier in C.

    • Rax

      Rax - 2008-09-07

      OK, the program works fine, but the init code... is this a bootloader? :S

      • William Gebers

        William Gebers - 2008-09-08

        Unless you specifically include a bootloader, no. 

        From my limited experience, the program size does not increase too rapidly as you add more functions.  Suggesting that the initial large size is the initialization code.

        I also suspect, although stand to be corrected, that if you are looking for highly optimized code you need to be writing in assembler directly.  Higher level languages such as C add significant overhead to the program code and even optimized compilers will still create larger bodies of code.

    • Rax

      Rax - 2008-09-08

      Thank you both!. Thanks to SDCC now I have a full PIC programming environment for Linux. No way I was complaining, I just wanted to know what was this startup code. Thak you!.


Log in to post a comment.