Save some bytes in program memory


Results 1 to 12 of 12

Threaded View

  1. #5
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default Re: Save some bytes in program memory

    Quote Originally Posted by pedja089 View Post
    You can save some code space if you use multiple If condition instead one with multiple AND.
    Example:
    Code:
    IF portb.0 AND portb.1 AND portb.2 AND portb.3 THEN
    ENDIF '46words
    But if you break that in multiple if's
    Code:
    IF portb.0 THEN
     IF portb.1 THEN
      IF portb.2 THEN
       IF portb.3 then
       ENDIF
      ENDIF
     ENDIF
    ENDIF
    Code is just 13 words.
    Wow! Big difference. Here's your first example (46 words) in BoostC -> 16 words;

    Code:
        if(portb.0 & portb.1 & portb.2 & portb.3)
          nop();
    Code:
         if(portb.0 & portb.1 & portb.2 & portb.3)
    00ED  01FD      CLRF CompTempVar20
    00EE  1C86      BTFSS gbl_portb,1
    00EF  28F3      GOTO    label14
    00F0  1C06      BTFSS gbl_portb,0
    00F1  28F3      GOTO    label14
    00F2  0AFD      INCF CompTempVar20, F
    00F3        label14
    00F3  01FE      CLRF CompTempVar21
    00F4  1C7D      BTFSS CompTempVar20,0
    00F5  28F9      GOTO    label15
    00F6  1D06      BTFSS gbl_portb,2
    00F7  28F9      GOTO    label15
    00F8  0AFE      INCF CompTempVar21, F
    00F9        label15
    00F9  1D86      BTFSS gbl_portb,3
    00FA  28FE      GOTO    label16
    00FB  1C7E      BTFSS CompTempVar21,0
    00FC  28FE      GOTO    label16
    
    
           nop();         
    00FD  0000      NOP
    00FE        label16
    Your second example (13 words) in BoostC -> 8 words;
    Code:
         if(portb.0)
           if(portb.1)
             if(portb.2)
               if(portb.3)
                 nop();
    Code:
         if(portb.0)
    00DD  1C06      BTFSS gbl_portb,0
    00DE  28E6      GOTO    label12
            if(portb.1)
    00DF  1C86      BTFSS gbl_portb,1
    00E0  28E6      GOTO    label12
              if(portb.2)
    00E1  1D06      BTFSS gbl_portb,2
    00E2  28E6      GOTO    label12
                if(portb.3)
    00E3  1D86      BTFSS gbl_portb,3
    00E4  28E6      GOTO    label12
    
                  nop();
    00E5  0000      NOP
    00E6        label12
    This variation only uses four words, which is pretty much how you might code it in assembler;
    Code:
         if(portb ^ 15)
           nop();
    Code:
         if(portb ^ 15)
    00E6  300F      MOVLW 0x0F
    00E7  0606      XORWF gbl_portb, W
    00E8  1903      BTFSC STATUS,Z
    00E9  28EB      GOTO    label13
    
    
           nop();
    00EA  0000      NOP
    00EB        label13
    It looks like the moral is to try and avoid multiple operations within a single instruction statement (line).
    Last edited by Mike, K8LH; - 8th February 2012 at 15:09.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts