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.

How can I use the C stack in linear assembly?

If I define an array in the C function,it will be placed in the stack,but I would like to rewrite my code in linear assembly(C callable),and I don't kow how to reserve space in the stack for an array in assembly.Is there anyone knows how to do this or any other solutions?

  • Which DSP are you using?

    Recommendations:

    1. Stay in C; do not use Linear Assembly. There are many optimization compiler switches (-o3) and pragmas (MUST_ITERATE) and keywords (restrict) that will significantly improve your C performance.

    2. Mix C and Linear Assembly. Write your Linear Assembly function within a slightly narrower scope. Allow a C wrapper function to allocate the array on its stack, and then have the C wrapper call the Linear Assembly function passing the address of the array as one of the arguments. The Linear Assembly function can be concerned with only the math operations, for example, while the C wrapper function will handle the stack allocation.

    3. Write it in C and look at the assembly output. You will find how the C compiler generates assembly to do the function you want to write in Linear Assembly. You will find the assignment used to define SP (stack pointer) and the operations that manipulate SP. You will learn some good assembly programming ideas from the compiler.

    4. Study the C Compiler User's Guide in detail. In particular, you will need to understand chapter 7 "Run-Time Environment" and section 7.5 "Interfacing C and C++ With Assembly Language". There is a lot of detail and many calling/usage conventions which you must follow to make sure your Linear Assembly program will function correctly within the C environment.

    5. SP is usually B15. Space is reserved on the stack by adding a value to SP. You decide what part of that space is your array and what part is used for other local variables.

  • hi,RandyP

    Thank you for  your recommendations.

    I have try recommendation 1 ,except the "restrict" keyword because another pointer  will refference the same block of memory,but it didn't  work,software pipeline schedule was still not found.

    The C Compiler User's Guide  mentions that manipulating SP in code between.cproc and .endproc is not allowed,does it mean I should not use the .cproc and .endproc directives if I manipulate the SP?

  • That was not a complete list of tools for optimizing C. Please do not give up on C until you have studied the User's Guide a lot and looked for suggestions in our Wiki pages.

    yinghui li said:
    The C Compiler User's Guide  mentions that manipulating SP in code between.cproc and .endproc is not allowed

    Where did it say this? I would think you have to manipulate SP to allocate space on the stack. Is that what you want to do, or something more uncommon?

  • Yes,I want to allocate some space for an array in the stack.

    I found the following words on  < TMS320C6000 Optimizing Compiler v 7.0 user' guide > 4.4.1 Instructions That Are Not Allowed in Procedures,page 116

    "The stack pointer (register B15) can be read, but it cannot be written to. Instructions that write to B15  are not allowed in a .proc or .cproc region."

    I try to use this instruction   " add  sp,20,sp" in the procedure,it issues an compile error:

    "histoequ_li.sa", ERROR!   at line 7: Stack pointer reg (B15) cannot
                                                    be written in a .(c)proc
                                                    region
        add  b15,20,b15

    I think I will try more on C,writing assembly is not easy for me.