ccortex-mfirmware

How do I get the asm variable value in C language?


Now I'm trying to understand the CM3 firmware code. But especially I found some of the code is made by asm code as the below, asm code

// Uart string output 
void UartPuts(unsigned char * mytext)
{
  unsigned char CurrChar;
  do {
    CurrChar = *mytext;
    if (CurrChar != (char) 0x0) {
      UartPutc(CurrChar);  // Normal data
      }
    *mytext++;
  } while (CurrChar != 0); 
  return;
}   
#if defined ( __CC_ARM   )
/* ARM RVDS or Keil MDK */
__asm void FlashLoader_ASM(void)
{
   MOVS  R0,#0
   LDR   R1,[R0]     ; Get initial MSP value
   MOV   SP, R1
   LDR   R1,[R0, #4] ; Get initial PC value
   BX    R1
}

and I want to print MSP value and PC value out by using UartPuts function.

Would you please guide me how am I supposed to do to print it out ?


Solution

  • One way (gas syntax):

    .thumb_func
    .globl GET32
    GET32:
        ldr r0,[r0]
        bx lr
    
    extern unsigned int GET32 ( unsigned int );
    unsigned int x;
    ...
    x = GET32(0x00000000);
    ...
    x = GET32(0x00000004);
    

    another way no asm required:

    #define ZERO (*((volatile unsigned int *)0x00000000))
    #define FOUR (*((volatile unsigned int *)0x00000004))
    ...
    unsigned int x;
    ...
    x = ZERO;
    ...
    x = FOUR;
    

    Can use uint32_t instead of unsigned int if you have a stdint.h or equivalent. (will result in unsigned int for gcc and clang/llvm).

    derived from your code/syntax

    __asm uint32_t read_zero(void)
    {
       MOVS  R0,#0
       LDR   R0,[R0]
       BX LR
    }
    
    __asm uint32_t read_four(void)
    {
       MOVS  R0,#4
       LDR   R0,[R0]
       BX LR
    }