0

I am using a MSP432 Microcontroller and I need to use some assembly. I need to use C variable that are defined earlier in the program but I am not sure how to do this. The variable I need to use are OS_curr and OS_next. I have tried colons as well but nothing has worked.


    OSThread *volatile OS_curr;
    OSThread *volatile OS_next;

    void PendSV_Handler(void) 
    {
    
    __asm__("cpsid      i");

    //if(OS_curr != (OSThread *)0)
    __asm__("ldr        r0, =OS_curr");
    __asm__("ldr        r0, [r0]");
    __asm__("cbz        r0, PendSV_restore");

    // Push r4  - r11
    __asm__("PUSH {r4-r11}");

    __asm__("ldr        r1, =OS_curr");
    __asm__("ldr        r1, [r1]");

    // OS_curr -> sp = sp;
    __asm__("str         sp, [r1]");

    PendSV_restore:

    // sp=OS_next -> sp;
    __asm__("ldr r0, =OS_next;");
    __asm__("ldr        r0, [r0]");
    __asm__("ldr        r0, [r0]");
    __asm__("str        sp, [r13]");

    // OS_curr = OS_next;
    __asm__("ldr        r0, =OS_next");
    __asm__("ldr        r1, [pc, 0xc]");
    __asm__("ldr        r0, =OS_curr");
    __asm__("str        r0, [r1]");

    //Pop  r4-r11
    __asm__("POP        {r4-r11}");

    //            __enable_interrupts();
    __asm__("cpsie      i");

    //return to next thread
    __asm__("bx         r14");
    }
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Mperez
  • 85
  • 1
  • 6
  • What compiler are you programming for? Why do you believe you need inline assembly? – fuz Jan 15 '21 at 23:39
  • If this is GNU C (gcc/clang/etc), your code is totally unsafe and will break when the compiler inlines it a caller. Your modify registers without telling the compiler about it, and having multiple separate asm statements is normally a bad idea. Did you mean to use `__attribute__((naked))`? – Peter Cordes Jan 15 '21 at 23:44
  • I am using ARM Compiler. I am following a turtorial right now. I thought it was better practice to do this. I am following Quantum Leap LLC RTOS course. – Mperez Jan 15 '21 at 23:47
  • @Mperez Try to avoid inline assembly if at all possible. It is very difficult to get inline assembly right. Either program in assembly in a separate assembly source file or program in C without any assembly. – fuz Jan 15 '21 at 23:59
  • I thought about programming in assembly and then using that in the C file. Thank you. I'll try that – Mperez Jan 16 '21 at 00:00

0 Answers0