Forums

problems while mixing C with asm

Started by leo July 8, 2008
Hi,

I'm newbie on the msp430 and I need to embed some asm in my C code.
I'm working w/ the msp430F2418 and using the IAR Workbench as a
developing environment.

I've spent some time googling and looking at some forum discussions.
Currently, I've already tried some ideas that i saw on the web: e.g.
to break my code into multiples as("instr") lines, to use the #pragma
asm #pragma endasm,.... I know there's the possibility of writing the
entire function in assembly, but in my case i really need to mix it w/
C language.

I've also couldn't find answers in the manual (I'm pretty sure they
might be there, but i couldn't figure out everything i've read
though -- as i said, i'm new on this platform).

I'm trying to run the following inline assembly code (in fact, i've
got two versions, the second is a gcc-based). The IAR outputs that:


for the first i got sth like this:
Error[Og006]: Syntax error in inline assembly: "Error[0]: Invalid
syntax"
Error[Og006]: Syntax error in inline assembly: "Error[0]:
Error[Og005]: Unknown symbol in inline assembly: "c"
Error[Og005]: Unknown symbol in inline assembly: "carry"
Error while running C/C++ compiler

and for the 2nd like that:
"Error[Pe018]: expected a ")" "


__asm (
  "MOV &a,r10\n"
  "MOV &b,r11\n"
  "MOV &c,r12\n"
  "MOV @r10+,r15\n"
  "MOV @r11+,r13\n"
  "ADD r13,r15\n"
  "MOV r15,(2*0)(r12)\n"
  "MOV @r10+,r15\n"
  "MOV @r11+,r13\n"
  "ADDC r13,r15\n"
  "MOV r15,(2*1)(r12)\n"
  "MOV @r10+,r15\n"
  "MOV @r11+,r13\n"
  "ADDC r13,r15\n"
  "MOV r15,(2*2)(r12)\n"
  "MOV @r10+,r15\n"
  "MOV @r11+,r13\n"
  "ADDC r13,r15\n"
  "MOV r15,(2*3)(r12)\n"
  "MOV @r10+,r15\n"
  "MOV @r11+,r13\n"
  "ADDC r13,r15\n"
  "MOV r15,(2*4)(r12)\n"
  "MOV @r10+,r15\n"
  "MOV @r11+,r13\n"
  "ADDC r13,r15\n"
  "MOV r15,(2*5)(r12)\n"
  "MOV @r10+,r15\n"
  "MOV @r11+,r13\n"
  "ADDC r13,r15\n"
  "MOV r15,(2*6)(r12)\n"
  "MOV @r10+,r15\n"
  "MOV @r11+,r13\n"
  "ADDC r13,r15\n"
  "MOV r15,(2*7)(r12)\n"
  "MOV @r10+,r15\n"
  "MOV @r11+,r13\n"
  "ADDC r13,r15\n"
  "MOV r15,(2*8)(r12)\n"
  "MOV @r10+,r15\n"
  "MOV @r11+,r13\n"
  "ADDC r13,r15\n"
  "MOV r15,(2*9)(r12)\n"
  "CLR r15\n"
  "ADC r15\n"
  "MOV r15,&carry\n"
  );

  asm(
  "mov %[a],r11\n\t"
  "mov %[b],r12\n\t"
  "mov @r11+,r13\n\t"
  "mov @r12+,r14\n\t"
  "add r14,r13\n\t"
  "mov %[c],r15\n\t"
  "mov r13,2*0(r15)\n\t"
  "mov @r11+,r13\n\t"
  "mov @r12+,r14\n\t"
  "addc r14,r13\n\t"
  "mov r13,2*1(r15)\n\t"
  "mov @r11+,r13\n\t"
  "mov @r12+,r14\n\t"
  "addc r14,r13\n\t"
  "mov r13,2*2(r15)\n\t"
  "mov @r11+,r13\n\t"
  "mov @r12+,r14\n\t"
  "addc r14,r13\n\t"
  "mov r13,2*3(r15)\n\t"
  "mov @r11+,r13\n\t"
  "mov @r12+,r14\n\t"
  "addc r14,r13\n\t"
  "mov r13,2*4(r15)\n\t"
  "mov @r11+,r13\n\t"
  "mov @r12+,r14\n\t"
  "addc r14,r13\n\t"
  "mov r13,2*5(r15)\n\t"
  "mov @r11+,r13\n\t"
  "mov @r12+,r14\n\t"
  "addc r14,r13\n\t"
  "mov r13,2*6(r15)\n\t"
  "mov @r11+,r13\n\t"
  "mov @r12+,r14\n\t"
  "addc r14,r13\n\t"
  "mov r13,2*7(r15)\n\t"
  "mov @r11+,r13\n\t"
  "mov @r12+,r14\n\t"
  "addc r14,r13\n\t"
  "mov r13,2*8(r15)\n\t"
  "mov @r11+,r13\n\t"
  "mov @r12+,r14\n\t"
  "addc r14,r13\n\t"
  "mov r13,2*9(r15)\n\t"
  "clr r14\n\t"
  "adc r14\n\t"
  "mov r14,%[carry]\n\t"
  : [carry] "=m" (carry)
  : [a] "m" (a), [b] "m" (b), [c] "m" (c), "[carry]" (carry)
  :"r11","r12","r13","r14","r15"
  );


In the latter, I've also tried sth like that in the end:
  "mov r14,%[carry]\n\t");
//  : [carry] "=m" (carry)
//  : [a] "m" (a), [b] "m" (b), [c] "m" (c), "[carry]" (carry)
//  :"r11","r12","r13","r14","r15"
//  );

But then i've got the following:

mrcomba.c
Error[Og006]: Syntax error in inline assembly: "Error[0]:
Error[Og006]: Syntax error in inline assembly: "Error[0]:
Error[Og006]: Syntax error in inline assembly: "Error[0]:
Error[Og006]: Syntax error in inline assembly: "Error[0]:
Error while running C/C++ compiler

Any help will be much appreciated

Cheers

  Leo
On Jul 9, 2:47 am, leo <barbosa.leona...@gmail.com> wrote:
> Hi, > > I'm newbie on the msp430 and I need to embed some asm in my C code. > I'm working w/ the msp430F2418 and using the IAR Workbench as a > developing environment. > > I've spent some time googling and looking at some forum discussions. > Currently, I've already tried some ideas that i saw on the web: e.g. > to break my code into multiples as("instr") lines, to use the #pragma > asm #pragma endasm,.... I know there's the possibility of writing the > entire function in assembly, but in my case i really need to mix it w/ > C language. > > I've also couldn't find answers in the manual (I'm pretty sure they > might be there, but i couldn't figure out everything i've read > though -- as i said, i'm new on this platform). > > I'm trying to run the following inline assembly code (in fact, i've > got two versions, the second is a gcc-based). The IAR outputs that: > > for the first i got sth like this: > Error[Og006]: Syntax error in inline assembly: "Error[0]: Invalid > syntax" > Error[Og006]: Syntax error in inline assembly: "Error[0]: > Error[Og005]: Unknown symbol in inline assembly: "c" > Error[Og005]: Unknown symbol in inline assembly: "carry" > Error while running C/C++ compiler > > and for the 2nd like that: > "Error[Pe018]: expected a ")" " > > __asm ( > "MOV &a,r10\n" > "MOV &b,r11\n" > "MOV &c,r12\n" > "MOV @r10+,r15\n" > "MOV @r11+,r13\n" > "ADD r13,r15\n" > "MOV r15,(2*0)(r12)\n" > "MOV @r10+,r15\n" > "MOV @r11+,r13\n" > "ADDC r13,r15\n" > "MOV r15,(2*1)(r12)\n" > "MOV @r10+,r15\n" > "MOV @r11+,r13\n" > "ADDC r13,r15\n" > "MOV r15,(2*2)(r12)\n" > "MOV @r10+,r15\n" > "MOV @r11+,r13\n" > "ADDC r13,r15\n" > "MOV r15,(2*3)(r12)\n" > "MOV @r10+,r15\n" > "MOV @r11+,r13\n" > "ADDC r13,r15\n" > "MOV r15,(2*4)(r12)\n" > "MOV @r10+,r15\n" > "MOV @r11+,r13\n" > "ADDC r13,r15\n" > "MOV r15,(2*5)(r12)\n" > "MOV @r10+,r15\n" > "MOV @r11+,r13\n" > "ADDC r13,r15\n" > "MOV r15,(2*6)(r12)\n" > "MOV @r10+,r15\n" > "MOV @r11+,r13\n" > "ADDC r13,r15\n" > "MOV r15,(2*7)(r12)\n" > "MOV @r10+,r15\n" > "MOV @r11+,r13\n" > "ADDC r13,r15\n" > "MOV r15,(2*8)(r12)\n" > "MOV @r10+,r15\n" > "MOV @r11+,r13\n" > "ADDC r13,r15\n" > "MOV r15,(2*9)(r12)\n" > "CLR r15\n" > "ADC r15\n" > "MOV r15,&carry\n" > ); > > asm( > "mov %[a],r11\n\t" > "mov %[b],r12\n\t" > "mov @r11+,r13\n\t" > "mov @r12+,r14\n\t" > "add r14,r13\n\t" > "mov %[c],r15\n\t" > "mov r13,2*0(r15)\n\t" > "mov @r11+,r13\n\t" > "mov @r12+,r14\n\t" > "addc r14,r13\n\t" > "mov r13,2*1(r15)\n\t" > "mov @r11+,r13\n\t" > "mov @r12+,r14\n\t" > "addc r14,r13\n\t" > "mov r13,2*2(r15)\n\t" > "mov @r11+,r13\n\t" > "mov @r12+,r14\n\t" > "addc r14,r13\n\t" > "mov r13,2*3(r15)\n\t" > "mov @r11+,r13\n\t" > "mov @r12+,r14\n\t" > "addc r14,r13\n\t" > "mov r13,2*4(r15)\n\t" > "mov @r11+,r13\n\t" > "mov @r12+,r14\n\t" > "addc r14,r13\n\t" > "mov r13,2*5(r15)\n\t" > "mov @r11+,r13\n\t" > "mov @r12+,r14\n\t" > "addc r14,r13\n\t" > "mov r13,2*6(r15)\n\t" > "mov @r11+,r13\n\t" > "mov @r12+,r14\n\t" > "addc r14,r13\n\t" > "mov r13,2*7(r15)\n\t" > "mov @r11+,r13\n\t" > "mov @r12+,r14\n\t" > "addc r14,r13\n\t" > "mov r13,2*8(r15)\n\t" > "mov @r11+,r13\n\t" > "mov @r12+,r14\n\t" > "addc r14,r13\n\t" > "mov r13,2*9(r15)\n\t" > "clr r14\n\t" > "adc r14\n\t" > "mov r14,%[carry]\n\t" > : [carry] "=m" (carry) > : [a] "m" (a), [b] "m" (b), [c] "m" (c), "[carry]" (carry) > :"r11","r12","r13","r14","r15" > ); > > In the latter, I've also tried sth like that in the end: > "mov r14,%[carry]\n\t"); > // : [carry] "=m" (carry) > // : [a] "m" (a), [b] "m" (b), [c] "m" (c), "[carry]" (carry) > // :"r11","r12","r13","r14","r15" > // ); > > But then i've got the following: > > mrcomba.c > Error[Og006]: Syntax error in inline assembly: "Error[0]: > Error[Og006]: Syntax error in inline assembly: "Error[0]: > Error[Og006]: Syntax error in inline assembly: "Error[0]: > Error[Og006]: Syntax error in inline assembly: "Error[0]: > Error while running C/C++ compiler > > Any help will be much appreciated > > Cheers > > Leo
As I have in 6 years never used inline assembler on the IAR EW430 tools, I can't explain the error messages other than to suggest that you are not following the requirements for entering the assembler source. My comment is really why would you want to do this. From a maintenance point of view this looks like a nightmare. You are using a lot of registers that the compiler would normally be using for its local variables and passed parameters. If you are using these variables both before and after the assembler fragment then your code will fail. There is no guarantee that the register usage within the surrounding function body will remain the same between different releases of the compiler, so while the register usage that you have chosen may work now there is no guarantee in the future. With the release of the MSP430F5x series of processors there have been two recent releases of the toolset and I expect that there may be another when TI release the production processor versions. If, and this is a big if, you absolutely have to use assembler, why don't you put this chunk of assembler into a separate file and make it into a function. The overhead of CALLA and RETA (assuming that you are using the 430X instruction set) is really quite small when compared to the length of your assembler block. You should also preserve the registers that you are using. Ian
leo wrote:
> Hi, > > I'm newbie on the msp430 and I need to embed some asm in my C code. > I'm working w/ the msp430F2418 and using the IAR Workbench as a > developing environment. >
> __asm ( "MOV &a,r10\n"
Some compilers are very fussy about embedded assembler and the first thing I would try is to use a separate __asm directive for each line of assember. The error message is the clue - looking for a closing ). As someone else pointed out, it will probably never work without saving the registers you use as well. You can program this sort of stufff far better in C. For example, fast access of an io port can be done by declaring a pointer to the port, then doing stuff like: *port = idle; *port = idle | databit; *port = idle | databit | clock; *port = idle; The above clocks a bit of data onto a port and translates almost directly to one line of register indirect assembler. The various write or'd values are handled by the preprocessor at compile time to produce an immediate value. You can't do it faster in raw assembler and it's far more portable :-)... Chris

leo wrote:

> Hi, > > I'm newbie on the msp430 and I need to embed some asm in my C code.
Only the newbies are embedding Asm into the C code. For many good reasons, Asm and C should be kept separately. If you need to do something in Asm, develop a function as the separate module in Asm, and call it from C.
> Any help will be much appreciated
How much is the appreciation? Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
Vladimir Vassilevsky wrote:
> > > leo wrote: > >> Hi, >> >> I'm newbie on the msp430 and I need to embed some asm in my C code. > > Only the newbies are embedding Asm into the C code. > > For many good reasons, Asm and C should be kept separately. If you need > to do something in Asm, develop a function as the separate module in > Asm, and call it from C. >
Normally, only newbies make wild, sweeping statements like that one. If you *do* need to include assembly in your C program, the choice for embedded inline assembly or separate assembly modules is dependant on the situation. But in this case, the use of embedded assembly is absurd. The OP should write his code in C.