can an ldr memory load instruction ..load from the stack part of the memory

Viewed 48

I have this set of arm instructions in mind.

sub r3,sp,4    
ldr r1, [r3]

just for clarity can a normal ldr instruction load a value from the stack part of the memory?.

2 Answers

Yes, absolutely. The stack is just a particular region of the 32-bit virtual address space, and pointers into that region can live in any register, not just sp.

It is extremely common to access the stack through loads and stores using something other than sp as the base register. Higher-level languages often do this, e.g.:

void func(void) {
    int x;
    scanf("%d", &x);
    // ...
}

The address of x, which is on the stack, would be put into r1 to pass to scanf. And scanf will do something like str r2, [r1] to store the input number there. scanf neither knows nor cares whether r1 points into stack or heap or whatever other part of the address space.

Any sort of emulation or modeling tool needs to account for this kind of aliasing.

You certainly can. This is actually how the PUSH and POP commands work. Most of the time you can use unified syntax to not have to write them, but they actually look like this. (Full disclosure: the only ARM CPU I have experience with is ARM7TDMI so I don't know for a fact this hasn't changed.)

STMFD sp!,{r0-r12}  ;ARM32 equivalent of PUSH  R0-R12
LDMFD sp!,{r0-r12}  ;ARM32 equivalent of POP R0-R12
Related