As far as x86 is concerned, there are not many general-purpose registers that can be controlled by the user program. The most important ones are eip and esp, which correspond to the first instruction of the user program and the stack address of the user program respectively. These two The os of the register will be set according to the format of the elf file when loading the user program. When the user program is running, the use of other general-purpose registers such as eax, ebx, ecx, and edx is managed by the compiler. Before the user program uses a register, the compiler will also load the required value into in the register. Segment registers such as cs, ds, es, and fs are also set by the operating system. As for the registers like cr3 that control the virtual memory to physical memory mapping table, they are also set by the OS and the user program does not have the authority to change them.
The way the operating system runs a program (for example, compiled in C language) is: first create a new process, then run the program loader, set up the stack and copy the program from disk to memory, and then finally It transfers control to the main() function (actually main() is not the first function called in the program, but I say this here for simplicity), and then the program runs happily. After executing so much code before, I have to manipulate the registers...
How hard does the OS work to load and execute a program? The OS needs to create page tables, read files, cut LDTs, and set registers. Otherwise, you won’t be able to see hello world. Did you create it?
CPU, who has done all the hard work in the background, is crying silently...
As far as x86 is concerned, there are not many general-purpose registers that can be controlled by the user program. The most important ones are eip and esp, which correspond to the first instruction of the user program and the stack address of the user program respectively. These two The os of the register will be set according to the format of the elf file when loading the user program. When the user program is running, the use of other general-purpose registers such as eax, ebx, ecx, and edx is managed by the compiler. Before the user program uses a register, the compiler will also load the required value into in the register. Segment registers such as cs, ds, es, and fs are also set by the operating system. As for the registers like cr3 that control the virtual memory to physical memory mapping table, they are also set by the OS and the user program does not have the authority to change them.
eax
,ebx
,ecx
are general-purpose registers, and will have a default value (should be 0) when the CPU starts.The way the operating system runs a program (for example, compiled in C language) is: first create a new process, then run the program loader, set up the stack and copy the program from disk to memory, and then finally It transfers control to the main() function (actually main() is not the first function called in the program, but I say this here for simplicity), and then the program runs happily. After executing so much code before, I have to manipulate the registers...
How hard does the OS work to load and execute a program? The OS needs to create page tables, read files, cut LDTs, and set registers. Otherwise, you won’t be able to see hello world. Did you create it?
CPU, who has done all the hard work in the background, is crying silently...