• AVR Freaks

Hot!why the data and program memory usage are so high?

Author
irun2
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2015/10/27 18:21:27
  • Location: 0
  • Status: offline
2020/07/06 20:15:19 (permalink)
0

why the data and program memory usage are so high?

The project is just a very simple test code, attached below:

//#define USE_XC8
#ifdef USE_XC8
  #include "pic18f44k20.h" // for xc8
#else
  #include "p18f44k20.h" // for mcc18
#endif

void main(void) {

    //TRISA= 0;
    while(1){
    // PORTA= ~PORTA;
    }
}



After compilation(regardless of what optimization options I choose),
the program usage shows 231 bytes are used, and the data used are 268 bytes!

It seems something wrong inside or the infomation is misleading.

When I chose XC8, only 8 bytes program and 0 byte data are used...

Why MCC18 is so poor on code and data efficiency?
 
MCC18 version: v3.47

 
#1

18 Replies Related Threads

    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11930
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/06 20:29:35 (permalink)
    +2 (2)
    #ifdef USE_XC8
      #include "pic18f44k20.h" // for xc8
    #else
      #include "p18f44k20.h" // for mcc18
    #endif


    Both of these are wrong.  The correct files are xc.h and p18cxxx.h, respectively.
     
    Why MCC18 is so poor on code and data efficiency?

     
    You haven't shown what your optimization settings are, but C18 will never match the optimizations of XC8.  C18 has been obsolete for years, so why do you care?
    #2
    ric
    Super Member
    • Total Posts : 27977
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/06 20:32:56 (permalink)
    +2 (2)
    I believe the OP is under the misapprehension they may get better performance from C18, just because it supports PIC18 extended mode.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11930
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/06 20:47:07 (permalink)
    +2 (2)
    the program usage shows 231 bytes are used

     
    Your test is meaningless.  Since the body of main() is empty, your program will just be the startup code, which doesn't change.  That's another optimization that XC8 does; C18 uses a large static startup library while XC8 handles it intelligently.
    #4
    irun2
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2015/10/27 18:21:27
    • Location: 0
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/06 22:13:38 (permalink)
    +1 (1)
    jtemples
    the program usage shows 231 bytes are used

     
    Your test is meaningless.  Since the body of main() is empty, your program will just be the startup code, which doesn't change.  That's another optimization that XC8 does; C18 uses a large static startup library while XC8 handles it intelligently.




    The posted code may be meaningless, but I've tried some other projects, all compiled results show MCC18 is no match for XC8.
    #5
    NKurzman
    A Guy on the Net
    • Total Posts : 18852
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: why the data and program memory usage are so high? 2020/07/06 22:49:31 (permalink)
    +1 (1)
    So XC8 is better than C18?
    Maybe that’s why Microchip bought high-tech and discontinued C18.

    C18 also has a stack. That may be why the data usage is so high.
    XC8 uses a compiled stack. So it doesn’t have to reserve extra memory.
    #6
    irun2
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2015/10/27 18:21:27
    • Location: 0
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/06 22:55:01 (permalink)
    0
    jtemples
    #ifdef USE_XC8
    #include "pic18f44k20.h" // for xc8
    #else
    #include "p18f44k20.h" // for mcc18
    #endif


    Both of these are wrong.  The correct files are xc.h and p18cxxx.h, respectively.
     
    Why MCC18 is so poor on code and data efficiency?

     
    You haven't shown what your optimization settings are, but C18 will never match the optimizations of XC8.  C18 has been obsolete for years, so why do you care?




    Do you mean no matter what the target device is, still I have to include p18cxxx.h???
     
    Anyway, the optimization setting is captured as below.
     
    post edited by irun2 - 2020/07/08 01:45:35

    Attached Image(s)

    #7
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11930
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/06 23:02:38 (permalink)
    +2 (2)
    Do you mean no matter what the target device is, still I have to include p18cxxx.h???

     
    Yes, just like the manual says.
    #8
    ric
    Super Member
    • Total Posts : 27977
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/06 23:12:13 (permalink)
    +2 (2)
    irun2
    Do you mean no matter what the target device is, still I have to include p18cxxx.h???

    Yes, and NOT include the device specific file.
    The compiler does it for you if you do as instructed.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11930
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/06 23:19:19 (permalink)
    +2 (2)
    So XC8 is better than C18?

     
    Hi-tech was always better than C18 (not only in code generation, but in following the C standard), even before the OCG optimizations were added.  The only optimization C18 had that XC8 does not is procedural abstraction (detecting repeated code sequences and turning them into a function).  That optimization would not generally be useful for well-written code, but it really helped on certain Microchip libraries.
    #10
    ric
    Super Member
    • Total Posts : 27977
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/06 23:24:51 (permalink)
    +3 (3)
    jtemples
    That optimization would not generally be useful for well-written code, but it really helped on certain Microchip libraries.

    LOL!

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    Hen
    Senior Member
    • Total Posts : 83
    • Reward points : 0
    • Joined: 2018/10/24 04:01:44
    • Location: 0
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/07 00:40:29 (permalink)
    0
    irun2
    After compilation(regardless of what optimization options I choose),
    the program usage shows 231 bytes are used, and the data used are 268 bytes!

    It seems something wrong inside or the infomation is misleading.

    When I chose XC8, only 8 bytes program and 0 byte data are used...

    Why MCC18 is so poor on code and data efficiency?
     
    MCC18 version: v3.47

    By curiosity I tested your code with opts and procspec (nothing else) and got:
     
    * 4 bytes code and 0 bytes data with MCC18 3.46
    * 12 bytes code and 0 bytes data with XC8 2.20
     
    However I suggest you go the XC8 route if you can.
    #12
    crosland
    Super Member
    • Total Posts : 2014
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/07 01:54:10 (permalink)
    +1 (1)
    irun2
    Why MCC18 is so poor on code and data efficiency?
     

     
    It isn't. It's actually very good. XC8 is also very good. I fond them to be roughly on a par with my type of code.
     
    What does the C18 linker file contain? How much space did you allocate to the stack?
    #13
    mlp
    boots too small
    • Total Posts : 946
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/07 07:29:51 (permalink)
    +2 (2)
    jtemples
    The only optimization C18 had that XC8 does not is procedural abstraction

    Sorry John, but you're wrong here.
    Procedural Abstraction is part of OCG.
    It was in before I started at Microchip, and maybe even before I left HI-TECH.

    Mark (this opinion available for hire)
    #14
    irun2
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2015/10/27 18:21:27
    • Location: 0
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/07 22:11:54 (permalink)
    0
    Hen
    irun2
    After compilation(regardless of what optimization options I choose),
    the program usage shows 231 bytes are used, and the data used are 268 bytes!

    It seems something wrong inside or the infomation is misleading.

    When I chose XC8, only 8 bytes program and 0 byte data are used...

    Why MCC18 is so poor on code and data efficiency?
     
    MCC18 version: v3.47

    By curiosity I tested your code with opts and procspec (nothing else) and got:
     
    * 4 bytes code and 0 bytes data with MCC18 3.46
    * 12 bytes code and 0 bytes data with XC8 2.20
     
    However I suggest you go the XC8 route if you can.




    Thank you for your test and feedback. Can you show me your linker script?

    As I realized it may due to the stack size defined in xxx.lkr, I modified the SIZE to zero then recompiled,
    memory usage became: data 10 bytes, program 229 bytes.

    From that 229 bytes of program mem, I do think it's occupied by the startup code. Is there anyway to reduced that?
     
    #15
    NKurzman
    A Guy on the Net
    • Total Posts : 18852
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: why the data and program memory usage are so high? 2020/07/07 23:19:40 (permalink)
    +1 (1)
    Without a stack you can’t do any function calls.
    You could probably do a custom start up, but you have to be extremely careful otherwise certain C constructs may not work.
    Like clearing ram or initializing globals.
    #16
    irun2
    New Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2015/10/27 18:21:27
    • Location: 0
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/07 23:52:36 (permalink)
    0
    crosland
    irun2
    Why MCC18 is so poor on code and data efficiency?
     

     
    It isn't. It's actually very good. XC8 is also very good. I fond them to be roughly on a par with my type of code.
     
    What does the C18 linker file contain? How much space did you allocate to the stack?




    The original linker script defined:

     
    #IFDEF _CRUNTIME
      SECTION    NAME=CONFIG     ROM=config
      #IFDEF _DEBUGSTACK
        STACK SIZE=0x100 RAM=gpr1
      #ELSE
        STACK SIZE=0x100 RAM=gpr2
      #FI
    #FI
     

    #17
    Hen
    Senior Member
    • Total Posts : 83
    • Reward points : 0
    • Joined: 2018/10/24 04:01:44
    • Location: 0
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/08 00:55:49 (permalink)
    0
    irun2
     
    Thank you for your test and feedback. Can you show me your linker script?

    As I realized it may due to the stack size defined in xxx.lkr, I modified the SIZE to zero then recompiled,
    memory usage became: data 10 bytes, program 229 bytes.

    From that 229 bytes of program mem, I do think it's occupied by the startup code. Is there anyway to reduced that?
     

    I let the compiler+linker do everything, the only non out-of-the-box thing I did was enable all optimizations and specify the processor you hinted at.
     
    The MCC18 code ended like:
    0: NOP
    2: BRA 2
     
    and the XC8:
    0: GOTO X
    X: MOVLB 0
    X+2 GOTO Y
    Y: BRA Y
    #18
    crosland
    Super Member
    • Total Posts : 2014
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: why the data and program memory usage are so high? 2020/07/08 02:19:46 (permalink)
    +3 (3)
    irun2
     
    As I realized it may due to the stack size defined in xxx.lkr, I modified the SIZE to zero then recompiled,
    memory usage became: data 10 bytes, program 229 bytes.

     
    That's not a very sensible thing to do (I am using kind words here).

    From that 229 bytes of program mem, I do think it's occupied by the startup code. Is there anyway to reduced that?
     

     
    C18 and XC8 are very different beasts. You need to write some real code that actually does something useful and then compare the results. Your trivial example is meaningless.
    #19
    Jump to:
    © 2020 APG vNext Commercial Version 4.5