Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Program counter and link register?

Tags:

arm

I have this question specific for the arm architecture.

I have seen in the ARM Register set we have link register (r14) and program counter (r15). During context switch time link register gets the address of program counter value of last function executed. but context switch also stores the program counter value.

Why do we need to save the program counter register status as we have link register that already has the program counter value ?

like image 661
user2760375 Avatar asked Oct 27 '25 09:10

user2760375


1 Answers

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.

like image 173
revelationnow Avatar answered Oct 29 '25 07:10

revelationnow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!