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 to assign local variables in assembly file

I am working on a project with mixed C and Assembly files. When it is switched to C part all variables defined in Asm file are not needed anymore, so they should be defined as local vars to avoid occupying ram space available to C part. How can I do this?

I am using CCS4 on msp430

  • Hi,

    Assembly could use the C stack to store temporary data. However, you must preserve the SP. Everything that is pushed onto the stack must be popped back before the function returns.

    Best regards,

    AES

  • Thanks. Well, actually what I need is to assign ram space from the start address (0x200) for both C variables and ASM variables. Is there a way to do this? It's like to reset the pointer to origin position and restart allocating.

     

  • Hi,

     

    At system initialization, SP is set to a designated address for the top of the stack. This address if the first location past the end of the .stack section. Since the position of the stack depends on where the .stack section is allocated, the actual address of the stack is determined at link time.

    The run-time stack grows from the high addresses to the low addresses. The compiler uses the R13 register to manage this stack. R13 is the stack pointer (SP), which points to the next unused location on the stack.

     

    The .stack section is defined in the lnk_msp430xxxxx.cmd file.

     

    AES

  • OK, I solved this by assigning fixed address for all my ASM variables (not many). There should be a better way. This is a firmware update via serial port project. I used the flash monitor  (see slaa341) asm program and moved it to CCS environment. When I tried to compile app and monitor in one single project I encountered this problem. 

  • Xiao Xu1 said:
    There should be a better way.

    There are several. However, both sides have to know the method you use.

    Best is indeed using the stack for storing local variabnles. This is how C does it and how ASM should do it. Both know of the stack pointer and where their local variables are relative to the stack pointer (or a frame pointer which is a copy of the stack pointer at funciton start).

    The other is to use a heap where you dynamically allocate and free space using a central management liek malloc/free. However, on systems with few ram and therefore a small heap, the usage of malloc (or rather, the use of free) can lead to heap congestion where small blocks of allocated heap space segmentate the heap so that no more contiguous memory of the required size is available.

    Third method is to allocate a global storage in the ASM file and declare the same name in C as external. However, ther emight be same name mangling (e.g. the C compiler automatically adds a '_' before all global variable names it decalares, so you'll have to add it too for the ASM definition - see the compiler description for details.)
    Like "extern unsighed char localspace[256];"

    Thsi way, both, ASM and C, can use 'localspace' as a pointer to a 256 byte area for their temporary data. However, it must be ensured that a C funciton tha tuses it does never call directly or indirectly an ASM function that also uses it (and v.v.)

**Attention** This is a public forum