how to shrink code


Closed Thread
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2005
    Posts
    67

    Smile how to shrink code

    a minor oversight !
    The demo PIC had 8k,
    final PIC ONLY 4k !
    Suggestions to shrink code, please !
    I have already used ASM/ENDASM where possible,
    have used minimal GoSubs,
    I started with 7996 and am down to 5100, only 1100 bytes to go !
    any other suggestions ?

    kindest regards
    Pete

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default

    have used minimal GoSubs,
    Try re-using code fragments by making them subroutines. Use either Gosub or call depending on whether you call from PBP or ASM section of your code

    JF

  3. #3
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Peter1960 View Post
    a minor oversight !
    The demo PIC had 8k,
    final PIC ONLY 4k !
    Suggestions to shrink code, please !
    I have already used ASM/ENDASM where possible,
    have used minimal GoSubs,
    I started with 7996 and am down to 5100, only 1100 bytes to go !
    any other suggestions ?
    kindest regards
    Which PIC?
    If it's a 16F series, you can probably mess with the variables a bit.
    Figure out which variable you 'access' the most and ensure it's forced into BANK 0.

    For that matter, let's see the code.
    You'll probably get a 1000 ideas on how to shrink the code.
    Last edited by skimask; - 23rd January 2008 at 16:47.

  4. #4
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Also,
    1. If you have small subroutines and/or small loops , keep them at the beginning.

    Also,
    2. if you are comparing a byte variable to a word variable, make sure, you compare the byte variable to the lowbyte of the word variable.

    Ex:
    A var byte
    B var word

    Instead of
    IF A = B THEN Home

    use,

    IF A = B.LowByte THEN Home

    Also,
    3. If you have HIGH and LOW commands, make them direct access to PORT register.

    Ex:
    Instead of
    HIGH PinA

    Use,
    PinA = 1


    =================
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  5. #5
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Default

    See Cliff Griffin's hints here
    http://www.rcfaq.com/PIC/optimize.htm
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  6. #6
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    If minimal code size is the goal, then PBP is a LOT of work.

    Reducing gosubs is not always the best rule, unless you are getting rid of the subs that were only added to make the code more "readable". If you can insert a few subroutines to eliminate duplicate code, they are very helpful.

    One thing to do is eliminate as many built-in basic commands as possible. For instance, hserout is very inefficient. You can manually load your values into the TXreg and save a lot of space. The ADCIN command is probably even worse.

  7. #7
    Join Date
    Feb 2005
    Posts
    67


    Did you find this post helpful? Yes | No

    Smile

    Hi and thanks everyone.

    Using a combination of Lookup and assembler

    the code is now 3768 !!!! beauty !

    I noticed that lookup can reduce code but can also
    increase code, so be carefull !

    thanks

    kind regards

    Peter
    Pete

  8. #8
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Peter1960 View Post
    ...

    I noticed that lookup can reduce code but can also
    increase code, so be carefull !..

    Peter

    Peter, it depends on the location of the LookUp statement in your program.

    Just do a little experiment by changing its location in the program.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  9. #9
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer View Post
    Peter, it depends on the location of the LookUp statement in your program.

    Just do a little experiment by changing its location in the program.
    A lot also has to do with how many elements you are looking up. It does not pay off until you are looking up more than just a few... 2-3, and it is a waste; a dozen and it saves a lot.

  10. #10
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I tend to use internal EEPROM for that when at all possible.

    If you want how nasty we could shrink it... challenge us a little bit and post it here

    Who take he challenge? Darrel? Bruce? Melanie? Alain? Skimask? Anyone?
    Last edited by mister_e; - 24th January 2008 at 20:44.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  11. #11
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e View Post
    I tend to use internal EEPROM for that when at all possible.
    If you want how nasty we could shrink it... challenge us a little bit and post it here
    Who take he challenge? Darrel? Bruce? Melanie? Alain? Skimask? Anyone?
    I'm up...but I'm afraid all I'll get is spanked...

  12. #12
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e from Montreal, Canada
    I tend to use internal EEPROM for that when at all possible.

    If you want how nasty we could shrink it... challenge us a little bit and post it here

    Who take he challenge? Darrel? Bruce? Melanie? Alain? Skimask? Anyone?
    Steve,

    Do you mean a scan routine that will get an input variable, then scan EEPROM and provide an output variable?

    ________________________________
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  13. #13
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    depending which part of the message you refer. But yeah, when possible i replace LOOKUP by a READ ... depending what also on-board i may use an external EEPROM. I don't have any conventions. Since awhile all my projects are different. I just try to find the best compromise to reduce cost, pcb space etc etc.

    Code shrinking is a kind of funny challenge... Peter says he already shrink it and maybe he's still working on, i don't know. What i suggest to him, is to post his whole code here to see how much we could reduce it.

    Not a must, be as i said, that's fun. I know Darrel like that kind of thing
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  5. Re-Writing IF-THEN-AND-ENDIF code?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th August 2006, 17:23

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