Search code examples
cassemblyarminline-assemblykeil

Stack Pointer reading incorrect value from register


Why is Stack-pointer register not reading correct value from another register? When I move a value from register (r0) to stack pointer (r13), the SP reads incorrect value. This is what is mean:

MOV R0, 10
MOV R13, R0

In this case, "A" should move to R13 but instead it gets 8. Similarly,

MOV R0, 9
MOV R13, R0

In this case R13 stores 8 instead of 9.

Here's a simple program program that demonstrates the problem,

void Init()
{               
    __asm(
        "LDR R0, =0x3FFFFDA7\n"
        "MOV R13, R0\n"
    );
}


int main(void)
{
    Init();     
    return (1);
}

void SystemInit(void)
{
}

Nothing much is going on here. Just a simple function call. Inside the function I moved the address to r0. Then I moved the address to R13(SP), but instead of actual address i.e. 0x3FFFFDA7, SP received 0x3FFFFDA4. The images shows the disassembly,

enter image description here

So what is going on here? Why is Stack pointer Register reading incorrect values?

I am using ARM inline Assembly with C. The IDE is KEIL.

Thanks in Advance.


Solution

  • For those who might find this helpful.

    Stack-Pointer for armv7 must be 4 bytes aligned. You can write there 0,4,8,12,16 etc but not 9,10,F etc.

    So if you want to move any value to Stack-Pointer, make sure it is 4 bytes aligned.