title: ARM-C Inter-working
body: <p>I am trying out a simple program for ARM-C inter-working. Here is the code:</p>
  
  <pre><code> #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  int Double(int a);
  extern int Start(void);
  int main(){
  
  int result=0;
  printf("in C main\n");
  result=Start();
  printf("result=%d\n",result);
  return 0;
  }
  
  int Double(int a)
  {
    printf("inside double func_argument_value=%d\n",a);
  
    return (a*2);
  }
  </code></pre>
  
  <p>The assembly file goes as-</p>
  
  <pre><code>.syntax unified
      .cpu cortex-m3
      .thumb
  
      .align
      .global Start
      .global Double
      .thumb_func
  
  
  
  Start:
        mov r10,lr
        mov r0,#42
        bl Double
        mov lr,r10
        mov r2,r0
        mov pc,lr
  </code></pre>
  
  <p>During debugging on LPC1769(embedded artists board), I get an hardfault error on the instruction " result=Start(). " I am trying to do an arm-C internetworking here. the lr value during the execution of the above the statement(result=Start()) is 0x0000029F, where the faulting instruction is,and the pc value is 0x0000029E. 
  This is how I got the faulting instruction in r1</p>
  
  <pre><code>__asm("mrs r0,MSP\n"
  "isb\n"
  "ldr r1,[r0,#24]\n");
  </code></pre>
  
  <p>Can anybody please explain where I am going wrong? Any solution is appreciated.
  Thank you in advance.</p>
  
  <p>I am a beginner in cortex-m3 &amp; am using the NXP LPCXpresso IDE powered by Code_Red.
  Here is the disassembly of my code.</p>
  
  <pre><code>      IntDefaultHandler:
  00000269:   push {r7} 
  0000026b:   add r7, sp, #0
  0000026d:   b.n 0x26c &lt;IntDefaultHandler+4&gt;
  0000026f:   nop 
  00000271:   mov r3, lr
  00000273:   mov.w r0, #42 ; 0x2a
  00000277:   bl 0x2c0 &lt;Double&gt;
  0000027b:   mov lr, r3
  0000027d:   mov r2, r0
  0000027f:   mov pc, lr
  
  
  
  main:
  00000281:   push {r7, lr}
  00000283:   sub sp, #8
  00000285:   add r7, sp, #0
  00000287:   mov.w r3, #0
  0000028b:   str r3, [r7, #4]
  0000028d:   movw r3, #11212 ; 0x2bcc
  00000291:   movt r3, #0
  00000295:   mov r0, r3
  00000297:   bl 0xd64 &lt;printf&gt;
  0000029b:   bl 0x270 &lt;Start&gt;
  0000029f:   mov r3, r0
  000002a1:   str r3, [r7, #4]
  000002a3:   movw r3, #11224 ; 0x2bd8
  000002a7:   movt r3, #0
  000002ab:   mov r0, r3
  000002ad:   ldr r1, [r7, #4]
  000002af:   bl 0xd64 &lt;printf&gt;
  000002b3:   mov.w r3, #0
  000002b7:   mov r0, r3
  000002b9:   add.w r7, r7, #8
  000002bd:   mov sp, r7
  000002bf:   pop {r7, pc}
  
  Double:
  000002c0:   push {r7, lr}
  000002c2:   sub sp, #8
  000002c4:   add r7, sp, #0
  000002c6:   str r0, [r7, #4]
  000002c8:   movw r3, #11236 ; 0x2be4
  000002cc:   movt r3, #0
  000002d0:   mov r0, r3
  000002d2:   ldr r1, [r7, #4]
  000002d4:   bl 0xd64 &lt;printf&gt;
  000002d8:   ldr r3, [r7, #4]
  000002da:   mov.w r3, r3, lsl #1
  000002de:   mov r0, r3
  000002e0:   add.w r7, r7, #8
  000002e4:   mov sp, r7
  000002e6:   pop {r7, pc}
  </code></pre>
  
  <p>As per your advice Dwelch, I have changed the r10 to r3. </p>