Save some bytes in program memory


Closed Thread
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    Sep 2009
    Posts
    755


    Did you find this post helpful? Yes | No

    Default Re: Save some bytes in program memory

    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.

  2. #2
    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.

  3. #3
    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

    I was too quick on the trigger... That last example should be;

    Code:
        if((portb ^ 15) == 0)
          nop();
    Code:
         if((portb ^ 15) == 0)
    00E8  300F      MOVLW 0x0F
    00E9  0606      XORWF gbl_portb, W
    00EA  1D03      BTFSS STATUS,Z
    00EB  28ED      GOTO    label13
    
    
           nop();
    00EC  0000      NOP
    00ED        label13

  4. #4
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Save some bytes in program memory

    This variation only uses four words, which is pretty much how you might code it in assembler;
    You could load portb into the temp variable, rotate it four times left, and check the value is b11110000,
    and it wouldn't matter what was happening on the high bits of portb.

  5. #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 Art View Post
    This variation only uses four words, which is pretty much how you might code it in assembler;
    You could load portb into the temp variable, rotate it four times left, and check the value is b11110000, and it wouldn't matter what was happening on the high bits of portb.
    Is there some advantage doing it that way, Art?

  6. #6
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Save some bytes in program memory

    Only if there was something else going on with the 4 high bits of portb,
    Otherwise any input on the high bits breaks the condition of portb ^ 15 = 0. or am I mistaken there?

  7. #7
    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

    You're correct. Sorry I misunderstood, Art...

  8. #8
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    699


    Did you find this post helpful? Yes | No

    Default Re: Save some bytes in program memory

    Quote Originally Posted by Mike, K8LH View Post
    It looks like the moral is to try and avoid multiple operations within a single instruction statement (line).
    Hmmm, I've never looked at it that way, but you might be right .
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

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