Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assembly registers beginner

So I've been getting into a bit of assembly lately and I'm a beginner so i was wondering if someone could clarify something. I take it every process has it's own set of registers, and each thread can modify these registers right?. How then do multiple threads use the same registers without causing clashes? Or does each thread have its own set of registers?

like image 664
Dnaiel Avatar asked Mar 09 '10 01:03

Dnaiel


People also ask

What are the assembly registers?

To speed up the processor operations, the processor includes some internal memory storage locations, called registers. The registers store data elements for processing without having to access the memory. A limited number of registers are built into the processor chip.

Is assembly code easy to learn?

Assembly language makes it faster to run complex code. It is incredibly difficult to write code in assembly language compared to high-level languages. Simpler for humans to understand than machine-level language. The syntax is hard to learn and can be difficult to remember or use quickly.

What are EAX EBX ECX EDX registers?

The EAX, EBX, ECX, EDX, EBP, EDI, and ESI registers are all 32-bit general-purpose registers, used for temporary data storage and memory access. Some of CPU instructions modify specific registers.


2 Answers

A thread context switch involves saving the registers of the current execution context, and loading the registers with saved values from execution context begin switched to. (among other things). So each thread effectively has its own set of registers. Also its own stack, since ESP is one of the registers.

One way of thinking about this is that you get threads by saving off the current register state, and loading the registers with a new state. If that isn't happening, then Its not a thread switch. If you are also switching to a different set of virtual address tables, then what you have is a process switch rather than a thread switch.

you say:

I take it every process has it's own set of registers, and each thread can modify these registers right?

But this isn't quite right. Each CPU core has a single set of registers. These registers are changed whenever the OS switches to a different thread. But there is only one thread executing in a CPU core at any one time. Processes don't really have their own registers, processes own threads (or at least one thread), and threads have registers, or rather a place to keep the values for the registers while the thread is waiting for a CPU core to be available to run on.

like image 124
John Knoeller Avatar answered Oct 13 '22 03:10

John Knoeller


In hardware, there is only one set of registers for each processor core. Because of this, only one thread at a time may use the registers. Multiple threads are run at the same time on a single core by rapidly switching from one thread to another. Scheduling which thread runs when is the job the operating system.

When switching from one thread to another, the contents of the registers are saved to a special area of memory, and the registers for the next thread are copied back into the processor. This includes the instruction pointer, so the thread knows where to continue executing when it gets control back. This process is called context switching.

Since the operating system's scheduler is in yet another thread, it can only schedule processes when it is running. This means that a special hardware feature--an interrupt--is necessary to control context switches. Only the operating system can schedule context switch interrupts.

like image 41
Jeffrey L Whitledge Avatar answered Oct 13 '22 01:10

Jeffrey L Whitledge