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.

Mixing C and ASM

Other Parts Discussed in Thread: MSP430F2272, MSP430F147

Hey

I have made this ASM fil to a msp430f2272 and I use IAR V5.30

 This is a delay function that take to paremeters, but when it reaches the red line the call stack become corrupted, so when the function end, it do not return to the right place.

#include <msp430f2272.h>

NAME Delay

 

        RTMODEL "__SystemLibrary", "CLib"

        RTMODEL "__core", "430"

        RTMODEL "__double_size", "32"

        RTMODEL "__pic", "no"

        RTMODEL "__reg_r4", "free"

        RTMODEL "__reg_r5", "free"

       RTMODEL "__rt_version", "3"

 

        RSEG CSTACK:DATA:SORT:NOROOT(0)

 

        EXTERN ?longjmp_r4

        EXTERN ?longjmp_r5

        EXTERN ?setjmp_r4

        EXTERN ?setjmp_r5

 

        PUBWEAK ?setjmp_save_r4

        PUBWEAK ?setjmp_save_r5

        PUBLIC Delay

        FUNCTION Delay,0203H

        ARGFRAME CSTACK, 0, STACK

        LOCFRAME CSTACK, 2, STACK

       

        CFI Names cfiNames0

        CFI StackFrame CFA SP DATA

        CFI Resource PC:16, SP:16, SR:16, R4:16, R5:16, R6:16, R7:16, R8:16

        CFI Resource R9:16, R10:16, R11:16, R12:16, R13:16, R14:16, R15:16

        CFI EndNames cfiNames0

       

        CFI Common cfiCommon0 Using cfiNames0

        CFI CodeAlign 2

        CFI DataAlign 2

        CFI ReturnAddress PC CODE

        CFI CFA SP+2

        CFI PC Frame(CFA, -2)

        CFI SR Undefined

        CFI R4 SameValue

        CFI R5 SameValue

        CFI R6 SameValue

        CFI R7 SameValue

        CFI R8 SameValue

        CFI R9 SameValue

        CFI R10 SameValue

        CFI R11 SameValue

        CFI R12 Undefined

        CFI R13 Undefined

        CFI R14 Undefined

        CFI R15 Undefined

        CFI EndCommon cfiCommon0

       

 

        RSEG CODE:CODE:REORDER:NOROOT(1)

Delay:

        CFI Block cfiBlock0 Using cfiCommon0

        CFI Function Delay

 

OutLoop: MOV.W   R14, R13;                                 // copy DcoCorrection to R13

DcoDelay:MOV     #0x18, R15;                               // approx. 100usec delay

Loop:    SUB.W   #0x1, R15;

         JNZ     Loop;

         SUB.W   #0x1, R13;                                // Delay--

         JNZ     DcoDelay;                                 // if DcoCorrection <> 0 make new 100uSec loop

         SUB.W   #0x1, R12;                                // Delay-- (R12=Delay)

         JNZ     OutLoop;

 

Exit:    RET

        CFI EndBlock cfiBlock0

 

        RSEG CODE:CODE:REORDER:NOROOT(1)

?setjmp_save_r4:

        REQUIRE ?setjmp_r4

        REQUIRE ?longjmp_r4

 

        RSEG CODE:CODE:REORDER:NOROOT(1)

?setjmp_save_r5:

        REQUIRE ?setjmp_r5

        REQUIRE ?longjmp_r5

  • Hello Michael,

    Normally I have a rule against posting a comment unless I can provide and answer, but this time, I have to ask...

    Why Michael, Why???

    I've used ASM in a few places where there was absolutely no other choice, but a

    while(--x)
    {
    ...
    }

    is really very efficient. Or even embedding the ASM in your C code like asm("JNZ Outloop");

    May I ask why you are doing an approximate delay this way instead of in C?

  •  Two possibility, WDOG not set or DCO wrong setup that overclock cpu and you lose debug interface.

  • it is some old code, I try to get on a msp430f2272 and yes C for the win :o)

  • I found the problem. The R12 to R16 register was not the same on msp430f2272 and msp430f147

  • Michael Hansen said:
    I found the problem. The R12 to R16 register was not the same on msp430f2272 and msp430f147

    Besides that there is no R16...

    The registers are the same for both CPUs. And the way the compiler generates the code and doe sthe assembly calls should be too. Your ASM code doesn't do anything unusual and only uses R12 to R15. Which usually (but not necessarily) is the register range for parameter and return value passing.

    I don't see why the R12/R14 values would clobber the stack.

    A different compiler (or even just a different revision, even though not likely) may change the calling conventions, so the parameters are passed on different registers. This is the main problem when mixing assembly and C. But this would only lead to wrong delay times or, in case of completely different calling registers, in clobbered registers of the calling function (because you changed them without restoring them before funciton end).

    A big change would beif the new MSP had a 20 bit MSP430X or X2 CPU core. Then the compiler would likely default to large code model, calling your function with CALLA instruction and requiring a RETA instruction for the return. (or else 16 bit return address will remain on the stack). But the 2272 doesn't have an X core.

**Attention** This is a public forum