Wednesday 11 July 2018

Difference between Program counter and link register?

I have seen in the ARM Register set we have link register (r14) and program counter (r15).


Let's say thread one is running, and it is executing instruction 0x1000, it calls a function located at 0x2000 at this point, and the Link register stores 0x1004. The PC has moved to the location where the function is located, 0x2008 (There are some gotchas about how PC is updated, if I remember right it is 2 words ahead of the current instruction being fetched).
Now a context switch occurs and another thread needs to start running. You will need to store the PC which is 0x2008 and also the LR which is 0x1004 so that after you switch back you can jump back to your earlier function.


LR is link register used to hold the return address for a function call.




SP is stack pointer. The stack is generally used to hold "automatic" variables and context/parameters across function calls. Conceptually you can think of the "stack" as a place where you "pile" your data. 












No comments:

Post a Comment