This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

question about assembly code optimization

Hi,

    I get a question about asm code optimization. I am now using cl430 to assemble/link my asm code. it works ok. my main code is something like :

    .include "global_support.asm"
.sect "balabal"
main
....................

     there are may routines in global_support.asm ,eg: routineA, routineB..... however, in my main code, only routineA is called.

      my question is : is there any option of cl430 that can optimize routineB... which is not called so that I can save main program area , and later I can change to devices with smaller main program area. now, I think macro may take effect, but I have to change main code...... is there any other method?

      many thanks for your help and suggestions. 

Felix

  • user1372058 said:
    my question is : is there any option of cl430 that can optimize routineB... which is not called so that I can save main program area

    The way the C compiler does this is to place each function in it's own subsection, and the linker only links the subsections for the functions which are called.

    The subsections are called <section_name>:<function_name>. E.g. main is placed in a section called .text:main

    To use subsections in the assembler you need to place double-quotes around the section name. E.g.:

    	.sect ".text:__duvul"
    	.align 2
    
    __divul: .asmfunc stack_usage(STACK_USED)
    

  • Hi Gillon,

        Interesting!  Thank you for you explaining of C-compiler working mechanism. However, even if I allocate subsections for each functions, in my option. these subsection memory space may not be used by main code...... which will still waste memory space if the corresponding function is not used.

        I thought this problem again, and I think it is really hard for assembler to determine which function is not used and can be optimized in asm file. anyway, I will try to use workaround in my design.

        Still many thanks for your help. :)

    Felix

  • Everything that is in the same section may reference each other directly, without using a relocatable symbol. The linker cannot know, so it has to link each section completely or not at all. By putting each function into its own sub-section, they cannot directly reference each other and need to rely on the linker to connect them. So the linker knows which functions are referenced and which aren’t and when there is no reference into one section, it can be discarded.
    In case of assembly code, there is no difference in the resulting code, regardless whether the functions directly call each other or indirectly, suing linker.-resolved symbols. However, when using a compiler, it might generate different assembly code if functions are not forced to be in separate sections. It may share the trailing code of multiple functions, inline function code etc. But this means, the whole code block needs to be linked as one.
    As usual, it is a tradeoff.

**Attention** This is a public forum