汇编寄存器初学者

发布于 2024-08-24 13:25:55 字数 127 浏览 2 评论 0原文

所以我最近开始接触一些汇编,我是一个初学者,所以我想知道是否有人可以澄清一些事情。我认为每个进程都有自己的一组寄存器,并且每个线程都可以修改这些寄存器,对吧?那么多个线程如何使用相同的寄存器而不引起冲突呢?或者每个线程都有自己的一组寄存器?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

苍白女子 2024-08-31 13:25:55

线程上下文切换涉及保存当前执行上下文的寄存器,以及从开始切换到的执行上下文加载具有保存值的寄存器。 (除其他外)。因此每个线程实际上都有自己的一组寄存器。还有它自己的堆栈,因为 ESP 是寄存器之一。

考虑这个问题的一种方法是,通过保存当前寄存器状态并用新状态加载寄存器来获取线程。如果这种情况没有发生,那么它不是线程切换。如果您还切换到一组不同的虚拟地址表,那么您拥有的是进程切换而不是线程切换。

你说:

我认为每个进程都有自己的一组寄存器,并且每个线程都可以修改这些寄存器,对吗?

但这并不完全正确。每个 CPU 核心都有一组寄存器。每当操作系统切换到不同的线程时,这些寄存器都会更改。但在任何时刻,CPU 核心中都只有一个线程在执行。进程实际上没有自己的寄存器,进程有自己的线程(或至少一个线程),并且线程有寄存器,或者更确切地说,在线程等待 CPU 核心可用时保存寄存器值的地方继续跑。

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.

不喜欢何必死缠烂打 2024-08-31 13:25:55

在硬件中,每个处理器核心只有一组寄存器。因此,一次只有一个线程可以使用寄存器。通过快速从一个线程切换到另一个线程,多个线程在单个内核上同时运行。调度哪个线程何时运行是操作系统的工作。

当从一个线程切换到另一个线程时,寄存器的内容被保存到内存的特殊区域,并且下一个线程的寄存器被复制回处理器。这包括指令指针,因此线程知道当它重新获得控制权时在哪里继续执行。这个过程称为上下文切换。

由于操作系统的调度程序位于另一个线程中,因此它只能在运行时调度进程。这意味着需要一种特殊的硬件功能(中断)来控制上下文切换。只有操作系统可以调度上下文切换中断。

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.

北凤男飞 2024-08-31 13:25:55

线程是由内核或操作系统完成的,因此程序不应该关心它。如果没有可用的内核或操作系统,那么您需要自己实现。为此,您将需要:

  • 一个可以保存状态的函数
    CPU 中所有寄存器的数量(SP:Stack
    指针、内部寄存器值、
    PC:程序计数器等...)
    切换到新的其他内存空间
    线。
  • 一个将线程环境加载到您的CPU环境中的函数,将之前保存的内部寄存器值恢复到您的CPU寄存器中。

The thread is done by a kernel or an OS, so the program should not care about it. If no kernel or OS is available, then you need to implement it yourself. for that you will need:

  • a function which will save the sate
    of all register in your CPU (SP:Stack
    pointer, internal register value,
    PC:Program counter etc...) in an
    other memory space to switch to a new
    thread.
  • a function to load a thread environment to your CPU environment, restore the previously saved internal register value to your CPU register.
剪不断理还乱 2024-08-31 13:25:55

您有

  • 一组进程,它是您的操作系统的一个,
  • 每个进程都有一个内存空间,其中包含动态分配的内存 >静态数据代码汇编
  • 每个进程都有一个线程列表,
  • 每个线程都有其自己的寄存器组程序计数器堆栈

以及上下文切换,您的调度程序会交换线程数据以将执行传递给另一个。

通常进程线程重,并且存在各种调度方法:

  • 仅在程序(您的操作系统)内部进行上下文切换(绿色线程)将仅将其视为单个进程,因此:硬多核)
  • 您可以分配许多实际进程以采用混合方法,从而轻松实现多核优化。

you have

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

with context switch your scheduler swaps thread data to pass the execution to another one.

Usually a process is heavier than a thread and various scheduling approaches exist:

  • doing context switches just internally (green threads) to your program (your OS will just consider it a single process so: hard multi-core)
  • you can assign a number of real processes to have an hybrid approach allowing easy multi-core optimization.
你与昨日 2024-08-31 13:25:55

根据处理器的不同,只有一组寄存器。每个线程不一组。

有多种方法可以保存所有寄存器的状态,以便线程可以从中断的地方继续运行。

某些处理器会促进此操作

Depending on the processor, there is only one set of registers. Not one set per thread.

There are ways to save the state of all registers, so that a thread can take up where it left off.

Some processors facilitate this.

带上头具痛哭 2024-08-31 13:25:55

每个线程都有自己的上下文,其中包括寄存器集、CPU 标志、堆栈等。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文