I am trying to write a program to calculate the exponential of a number using ARM-C inter-working. I am using LPC1769(cortex m3) for debuuging. The following is the code:
#include <<stdio.h>>
#include <<stdlib.h>>
extern int Start (void);
extern int* Exponentiatecore(int *m,int *n);
void print(int i);
int Exponentiate(int *m,int *n);
int main()
{
Start();
return 0;
}
int Exponentiate(int *m,int *n)
{
if (*n==0)
return 1;
else
{
int *result=NULL;
result=(int *)malloc(sizeof(int));
*result=Exponentiatecore(m,n);
return (result);
}
}
void print(int i)
{
printf("value=%d\n",i);
}
this is the assembly code which complements the above C code
.syntax unified
.cpu cortex-m3
.thumb
.align
.global Start
.global Exponentiatecore
.thumb
.thumb_func
Start:
mov r10,lr
ldr r0,=label1
ldr r1,=label2
bl Exponentiate
ldr r0,[r0]
bl print
mov lr,r10
mov pc,lr
Exponentiatecore:
ldr r0,[r0]
mov r2,r0
ldr r1,[r1]
loop:
mul r2,r0
subs r1,#1
bne loop
mov r0,r2
bx lr
label1:
.word 0x02
label2:
.word 0x03
however during the debug session, I encounter a Hardfault error for the execution of "Exponentiatecore(m,n)". During the first branch from assembly to C i.e. BL Exponentiate-- LR is saved. Again during second branch to assembly routine Exponentiatecore(m,n) LR is over written. This is the line I am facing the hardfault error. Any suggestions are appreciated.
as seen in debug window.
Name : HardFault_Handler
Details:{void (void)} 0x21c &lt;&lt;HardFault_Handler&gt;&gt;
Default:{void (void)} 0x21c &lt;&lt;HardFault_Handler&gt;&gt;
Decimal:&lt;&lt;error reading variable&gt;&gt;
Hex:&lt;&lt;error reading variable&gt;&gt;
Binary:&lt;&lt;error reading variable&gt;&gt;
Octal:&lt;&lt;error reading variable&gt;&gt;
Am I making some stack corruption during alignment or is there a mistake in my interpretation? please kindly help. thankyou in advance