Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

PIC14 Return Values

2007-02-17
2013-03-12
  • This code:

    unsigned char c;

    signed char do_return(void)
    {
            if(c)
                    return(-1);
            else
                    return(0);
    }

    Generates this ASM:

    _do_return      ;Function start
    ; 2 exit points
    ;       .line   8; "return.c"   if(c)
            MOVLW   0x00
            BANKSEL _c
            IORWF   _c,W
            BTFSC   STATUS,2
            GOTO    _00106_DS_
    ;       .line   9; "return.c"   return(-1);
            MOVLW   0xff
            GOTO    _00108_DS_
    _00106_DS_
    ;       .line   11; "return.c"  return(0);
            MOVLW   0x00
    _00108_DS_
            RETURN
    ; exit point of _do_return

    Now, I'm no expert, but I'm fairly sure PICs do have a SINGLE INSTRUCTION designed for returning an 8-bit literal.  It reduces that mess to this:

    _do_return      ;Function start
            MOVLW   0x00
            BANKSEL _c
            IORWF   _c,W
            BTFSC   STATUS,2
            RETLW   0x00
            RETLW   0xff

    Any particular reason the optimizer misses this?

     
    • AP2k
      AP2k
      2007-05-14

      Quick question: does the PIC you are compiling for support the RETLW instruction? I know the 16F877 has it.