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.

CCS6, MSP430F5529 Launchpad, Calling Assembly Funtion from C IDE

Other Parts Discussed in Thread: MSP430F5529

Dear Sir:

     I am new to the CCS6 IDE.   I am using CCS5, MSP430F5529 Launchphad , and I am trying to call an assembly language function from the C integrated development environment. 

     I am using as a guide I am using a Texas Instruments publication, The MSP430 Optimizing C/C++ Compiler User's Guide, SLAU132j.  I have posted a link to the publication for your convenience below: 

http://www.ti.com/lit/ug/slau132j/slau132j.pdf

Please go to page 127.

      I have copied and pasted the code below as a guide as a reference for out discussion:

 

The C program that calls the assembly language function:

 

extern "C" {

extern int asmfunc(int a); /* declare external asm function */

int gvar = 0; /* define global variable */

}

void main()

{

int var = 5;

var = asmfunc(var); /* call function normally */

The called assembly language function:

.global asmfunc

.global gvar

asmfunc:

MOV &gvar,R11

ADD R11,R12

RET

 

      I believe this discussion would be fruitful if someone could, first, get this program working and reply with working code that I can directly cut and paste directly into CCS6 because there are a lot of problems if one copies and pastes the code directly from the TI publication.   

       For example, the compiler does not like the extern C {} method of coding (this can be remedied by not writing the code in shorthand) and the assembly language program does not appear to accept instructions such as ADD without a bit length appended to it (for example ADD.w) .    I could go on and on with all the small problems I have encountered and if we went over every little problem this discussion would take an unreasonably long time.  

       Therefore, first, sending me the working code would be efficient.   It is possible that, maybe, my compiler settings are not correct, in addition to, the incompatible  format of code that I have been trying to work with.    I would like to look into this first because I have encountered some linking errors when I managed to get parts of the code to compile.  

        Can someone please help me get this program working so that I can step through the program?    Thank you for your consideration.

  • forum150110.c

    asmfunc.asm

    Try these two files.  Most important difference is the use of RETA (instead of RET) instruction.  Note that C function calls use the CALLA instruction.

    Also note that the extern "C" is necessary only if you are mixing C++ with C code.

  • Dear Quark;

        Thank you for offering your code.  You wrote a great program.   Your efforts were greatly appreciated.

        I got your code working on CCS6.  

        However, there were some modifications that needed to be made and I will outline them below:

    CCS6 will either lose track of the program (can't step through the program), or the compiler will otherwise output an error message when this line of code is inserted :

        PM5CTL0 &= ~LOCKLPM5;  //    Remedy:  Comment out or delete this line of code

        An interesting thing also happened when I tried to cut and paste your assembler program into the .asm template that is offered by CCS6.  I got this error message:

    "./lnk.cmd", line 48: error #10099-D: program will not fit into available     memory.  placement with alignment fails for section ".reset" size 0x4 .       This should not have happened because this assembly program uses such a small amount of memory.

        However, the program runs fine if you open and copy the .asm program that you wrote into the project folder.

        I have cut and pasted the CCS6 template program below and, hopefully, a TI engineer will offer us some feedback as to the source of the error message and how to remedy the error.   (Note:  No matter where I placed the .global declarations in the program I still received the same error message).    The template and program:

        

    ;-------------------------------------------------------------------------------

    ; MSP430 Assembler Code Template for use with TI Code Composer Studio

    ;

    ;-------------------------------------------------------------------------------

                .cdecls C,LIST,

    "msp430.h"       ; Include device header file

    ;-------------------------------------------------------------------------------

               

    .text                           ; Assemble into program memory

                .retain                 ; Overide ELF conditional linking

                                           ; and retain current section

                .retainrefs           ; Additionally retain any sections

                                           ; that have references to current section

    ;     .global asmfunc

    ;    .global gvar

     

    ;-------------------------------------------------------------------------------

    RESET      

    mov.w   #__STACK_END,SP         ; Initialize stackpointer

    StopWDT    

    mov.w   #WDTPW|WDTHOLD,&WDTCTL  ; Stop watchdog timer

             

     

    ;-------------------------------------------------------------------------------

                                               

    ; Main loop here

    ;-------------------------------------------------------------------------------

          .global asmfunc

       .global gvar

    asmfunc:

    MOV &gvar,R11

    ADD R11,R12

    RETA

    ;-------------------------------------------------------------------------------

    ;           Stack Pointer definition

    ;-------------------------------------------------------------------------------

               

    .global __STACK_END

               

    .sect .stack

    ;-------------------------------------------------------------------------------

    ;           Interrupt Vectors

    ;-------------------------------------------------------------------------------

               

    .sect   ".reset"                ; MSP430 RESET Vector

               

    .short  RESET

  • Dear Quark;

    You wouldn't believe what happened. To reproduce the error message that was output by using the code with the CCS6 .ASM template , I excluded your working .ASM file from my build, I then posted the error messages. After I posted the error messages, I then cleaned the project and included your working .ASM file again, so that I could get the program running again. To my surprise, CCS6 output error messages. I can't explain what happened.

    I closed the file and restarted my computer again, opened CCS6, rebuilt the program, and I still got the same error messages with regard to your .ASM file.

    This is the error message with regard to the .ASM file:

    Description Resource Path Location Type
    [E0003] Unexpected trailing operand(s) called_asm_function.asm /C IDE Calling ASM FUNCTION line 7 C/C++ Problem
    [E0003] Unexpected trailing operand(s) called_asm_function.asm /C IDE Calling ASM FUNCTION line 6 C/C++ Problem
    [E0200] Binary operator can't be applied called_asm_function.asm /C IDE Calling ASM FUNCTION line 7 C/C++ Problem
    [E0200] Bad term in expression called_asm_function.asm /C IDE Calling ASM FUNCTION line 7 C/C++ Problem
    [E0001] Illegal symbol called_asm_function.asm /C IDE Calling ASM FUNCTION line 7 C/C++ Problem
    [E0200] Binary operator can't be applied called_asm_function.asm /C IDE Calling ASM FUNCTION line 6 C/C++ Problem
    [E0200] Bad term in expression called_asm_function.asm /C IDE Calling ASM FUNCTION line 6 C/C++ Problem
    [E0001] Illegal symbol called_asm_function.asm /C IDE Calling ASM FUNCTION line 6 C/C++ Problem


    I can't explain what happened. I am hoping a TI engineer can show me a stable way of calling an assembly function.

    By the way, can you tell me what compiler you are using?
  • Dear Quark and all other readers of this post:

    My apologies for my last messge to Quark, Quark don't worry you .ASM program is fine.

    Ignore the [E0003] error messges. I got the program working again.

    What happened was, I was stepping through the program and writing notes before I posted to Quark (I did not rebuild the program). When I posted to Quark, I rebuilt the program again to reproduce the CC6 template error message. When I rebuilt the program again, I did not take into account that the notes I was leaving for myself in the .ASM program relied upon the C/C++ comment symbols (//). This was in error. The .ASM compiler uses the ; symbol to make comments. This was the source of the E0200 error messages. Again, my apologies for inconveniencing anyone.

    Quark, again I would like to thank you for your efforts, they were appreciated.
  • It should be noted that when mixing C and ASM, care must be taken regarding register usage.
    The registers which are used to pass arguments to and take results form the function are different.
    MSPGCC, for example, uses R12-R15 for passing up to 4 bytes of parameters (the remaining are passed on the stack) and uses the same registers for returning up to a long value. So the calling function considers there four as clobbered, but it would rely on R4-R11 coming back unchanged.
    Different compilers will do different. So the ASM code is not portable. Unless it is void func(void) and preserves all registers (working only on globals) or uses compiler-specific entry and exit code.

**Attention** This is a public forum