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.

CCS/CCSTUDIO: Trouble calling assembly functions in C code

Part Number: CCSTUDIO

Tool/software: Code Composer Studio

Hi all

I am a complete newbie to MSP430 launchpad with M430G2553 MCU. I want to perform the following tasks,

1. Calling an assembler function from a C program without passing parameters and return values.

2. Calling an Assembler Function From a C Program and Passing Parameters and a Return Value

3. the assembler function / or from main loop calls a standard C library function say rand() and calls a user made C function mult(a, b)

4. many more ....

I got a tutorial pdf : slaa140.pdf which has full tut with examples of all the above, but bad luck it is for IAR and just copying and pasting the codes didn't work in CCS

so I decided to try myself :  I tried the first : Calling an assembler function from a C program without passing parameters and return values.

I made two files inside my CCS project

1. blink.c

#include <msp430.h>				

/* -------------------external Function Prototypes ------------------- */
extern void set_port(void); /* Function Prototype for asm function */

int main(void) {
	WDTCTL = WDTPW | WDTHOLD;		// Stop watchdog timer
	P1DIR |= 0x01;					// Set P1.0 to output direction

	for(;;) {
		volatile unsigned int i;	// volatile to prevent optimization

		set_port();				// Toggle P1.0

		i = 10000;					// SW Delay
		do i--;
		while(i != 0);
	}
	
	return 0;
}

and 2. asmFile.asm

; *****************************************************************************
    .cdecls C,LIST,"msp430.h"

;============================================================================
; set_port
;============================================================================
.text        ; Code is relocatable
set_port:
_main
xoropr xor.b #01h,&P1OUT ; Toggle 0x01 bit Port 1 output
ret
.end

but on building the project i got errors

Description    Resource    Path    Location    Type
#10010 errors encountered during linking; "callingASMinC.out" not built    callingASMinC             C/C++ Problem
<a href="processors.wiki.ti.com/.../10234"> unresolved symbols remain    callingASMinC             C/C++ Problem
gmake: *** [all] Error 2    callingASMinC             C/C++ Problem
gmake[1]: *** [callingASMinC.out] Error 1    callingASMinC             C/C++ Problem
unresolved symbol set_port, first referenced in ./blink.obj    callingASMinC             C/C++ Problem

pls help me i am in a great need

thanks in advance

waiting patient-lessly for your reply pls reply soon

thanks

  • You need to add this statement to the assembly file ...

        .global    set_port

    This tells the assembler to make that a global symbol.  

    To read more about interfacing C and assembly, see the section titled Accessing Assembly Language Functions From C/C++ in the MSP430 compiler manual.  More about the .global directive can be found in the MSP430 assembly tools manual.

    Thanks and regards,

    -George

  • Hi great thanks for your reply

    i did what you said but no luck,

    //blink.c
    #include <msp430.h>
    
    extern "C" {
    extern int asmfunc(); /* declare external asm function */
    }
    
    int main(void) {
        WDTCTL = WDTPW | WDTHOLD;       // Stop watchdog timer
        P1DIR |= 0x01;                  // Set P1.0 to output direction
    
        for(;;) {
            asmfunc();             // Toggle P1.0
            delay();
        }
        return 0;
    }
    
    void delay()                        // delay routine
    {
        volatile unsigned int i;        // volatile to prevent optimization
        i = 10000;                      // SW Delay
        do i--;
        while(i != 0);
    }
    
    ;asmFile.asm
    ; *****************************************************************************
        	.cdecls C,LIST,"msp430.h"
    
        	.global asmfunc
    
      asmfunc:
    		xor.b   #00000001b,&P1OUT
    		ret
    

    and the errors are,

    Description	Resource	Path	Location	Type
    [E0002] Illegal mnemonic specified	asmFile.asm	/callingASMinC2	line 7	C/C++ Problem
    #41 expected an identifier	blink.c	/callingASMinC2	line 4	C/C++ Problem
    #66 expected a ";"	blink.c	/callingASMinC2	line 4	C/C++ Problem
    gmake: *** [asmFile.obj] Error 1	callingASMinC2		 	C/C++ Problem
    gmake: *** [blink.obj] Error 1	callingASMinC2		 	C/C++ Problem
    gmake: Target 'all' not remade because of errors.	callingASMinC2		 	C/C++ Problem
    

    I also refer to pg 132 of slau132n.pdf ex6.2 and 6.3 but still the same errors

    pls help me

    thanks

  • You may not have any whitespace on the line before the label asmfunc

    You have written a C file, not a C++ file. Don't use the 'extern "C"' wrapping, just use 'extern'. Alternately, you could rename the file to blink.cpp and leave it as a C++ file, in which case the 'extern "C"' would be required.
  • I got a strange behaviour

    when my asm file is as before i got errors but when i used the same indentation as used in the asm template it works

    ;-------------------------------------------------------------------------------
    ; MSP430 Assembler Code Template for use with TI Code Composer Studio
    ;-------------------------------------------------------------------------------
                .cdecls C,LIST,"msp430.h"       ; Include device header file
    
    ;-------------------------------------------------------------------------------
    
        		.global asmfunc
    
    asmfunc:
    			xor.b   #00000001b,&P1OUT
    			ret

    but if i shift the label asmfunc to right it says illegal mnemonic specified

    ;-------------------------------------------------------------------------------
    ; MSP430 Assembler Code Template for use with TI Code Composer Studio
    ;-------------------------------------------------------------------------------
                .cdecls C,LIST,"msp430.h"       ; Include device header file
    
    ;-------------------------------------------------------------------------------
    
        		.global asmfunc
    
        asmfunc:
    			xor.b   #00000001b,&P1OUT
    			ret

    so does like python ccs uses indentation ?

  • In assembly code, all labels must be placed at the very beginning of the line.  For additional details, please see the section titled Source Statement Format in the MSP430 assembly tools manual.

    Thanks and regards,

    -George

  • hi great thanks for guiding me not it works

    but i have a new problem printf doesn't work pls tell me how can i use printf in my programs

    thanks