[NSWI004] program counter register
Petr Tuma
petr.tuma at d3s.mff.cuni.cz
Fri Nov 6 18:41:09 CET 2020
Hi,
> I'm not sure I understand why you are writing about ra register, since my question was about pc register.
A good question. That's because you cannot 'set the pc register' in your program (you would immediately jump to that address). What you want is to call the `cpu_switch_context` function and set the address that this particular function returns to after restoring the context. And that is in the `ra` register (whose value will be stored in the `pc` register at the very end of `cpu_switch_context`).
> Each thread has it's own set of registers(context). Including pc register.
> I create new thread t1.
> I set t1 pc to the address of the entry function.
> So when I context_switch to t1, cpu starts executing instructions of the entry function.
Yes. But when you are executing the context switch, the `pc` register necessarily points to the context switch code - there is simply no way around this. So what you really want to do is "set the return address from the context switch code to point to the thread entry function". And that is the `ra` register.
> But context_t doesn't even have a pc register. How can I initialize pc in the beginning?
Is it clear now ?
Petr
More information about the NSWI004
mailing list