Assembly recorders

So, lately I’m going to build a little bit, and I’m new to it, so I was wondering if anyone could clarify something. I believe that each process has its own set of registers, and each thread can change these registers correctly ?. How then do several threads use the same registers without causing conflicts? Or does each thread have its own set of registers?

+7
assembly cpu-registers
source share
6 answers

The thread context switch includes saving the registers of the current execution context and loading the registers with the saved values ​​from the execution context. (by the way). Thus, each thread effectively has its own set of registers. Also its own stack, since ESP is one of the registers.

One way to think about what you are getting streams is by keeping the current state of the register and loading the registers into a new state. If this does not happen, then this is not a thread switch. If you also switch to another set of virtual address tables, then you have a process switch, not a thread switch.

you speak:

I understand that each process has its own set of registers, and each thread can change these registers correctly?

But this is not entirely correct. Each CPU core has one set of registers. These registers change whenever the OS switches to another thread. But there is only one thread running in the processor core at any given time. Processes actually do not have their own registers, process their own threads (or at least one thread), and threads have registers or, rather, a place to store the values ​​for the registers, while the thread waits for the CPU core will be available to run.

+11
source share

In hardware, there is only one set of registers for each processor core. Because of this, only one thread at a time can use registers. Several threads simultaneously run on one core, quickly moving from one thread to another. Planning which thread runs when the operating system runs.

When switching from one thread to another, the contents of the registers are stored in a special memory area, and the registers for the next stream are copied back to the processor. This includes an instruction pointer, so the thread knows where to continue execution when it receives control. This process is called context switching.

Because the operating system scheduler is in another thread, it can only schedule processes when it is running. This means that to control the context switches, a special hardware function is needed - interruption. Only the operating system can schedule context switching interruptions.

+4
source share

The thread is executed by the kernel or OS, so the program does not have to worry about this. If the kernel or OS is not available, you need to implement it yourself. for this you will need:

  • function that saves the status of all the registers in your CPU (SP: Stack pointer, internal register value, PC: program counter, etc.) to another memory space for switching to a new thread.
  • the function of loading the flow medium into the environment of your processor, restoring the previously saved value of the internal register to the CPU register.
+2
source share

Depending on the processor, there is only one set of registers. Not one set for flow.

There are ways to preserve the state of all registers so that the thread can take the place where it stopped.

Some processors make this easier .

+1
source share

you have

  • a set of processes that is your operating system,
  • each process has a memory space containing dynamic allocated memory , static data and code assembly ,
  • each process has a list of threads
  • each thread has its own set of registers , program counter and stack

with a context switch, your scheduler swaps the stream data to pass execution to another.

Usually a process is heavier than a thread , and there are various planning approaches:

  • executing the context only switches internally (green threads) to your program (your OS will simply consider it as one process: hard multi-core)
  • You can designate a number of real processes to have a hybrid approach that allows easy multi-core optimization.
+1
source share

Each thread has its own context, which includes a set of registers, CPU flags, stack, etc.

0
source share

All Articles