可执行文件是为硬件编写的机器代码吗?
这是来自维基百科。
“在计算中,可执行文件使计算机“根据编码指令执行指定的任务”,(机器代码??)
“现代操作系统保留对计算机资源的控制,要求各个程序进行系统调用以访问特权资源。由于每个操作系统系列都有自己的系统调用架构,因此可执行文件通常与特定操作系统相关联。”
这是我的观点。 可执行文件不能是机器代码,因为它们需要与操作系统进行硬件服务(系统调用),因此可执行文件还不是“机器代码”......也许代码的某些部分是实际的机器代码,而某些部分是只是为了调用操作系统中嵌入的机器代码?总的来说,它包含一些垃圾机器代码以及一些调用操作系统的垃圾代码。
在达蒙的回答后编辑: 归根结底,操作系统是一组机器代码。基本上,操作系统将完成复制粘贴用户的机器代码(由 C 编译器创建)的工作,然后如果指令是系统调用,则传输到操作系统内存区域进行处理。现在的问题是用 C 生成的机器代码可以完成这部分工作吗?就像要求将控制权转移到操作系统等 - 我想它的系统调用更高的抽象但在幕后 - 它是如何工作的。
我有一种感觉,它类似于鸡生蛋问题,C 创建操作系统,C 使用操作系统,无法准确地找到该过程是如何进行的。 谁能帮我解开这个谜题吗?
Here is from Wiki .
"In computing, an executable file causes a computer "to perform indicated tasks according to encoded instructions," ( Machine Code ?? )
"Modern operating systems retain control over the computer's resources, requiring that individual programs make system calls to access privileged resources. Since each operating system family features its own system call architecture, executable files are generally tied to specific operating systems."
Well this is my perspective .
Executables cannot be Machine Code as they need to tal to the OS for hardware services ( system calls) Hence executable is just not yet "Machine Code" ... Perhaps it is like some part of the code is actual Machine Code and some parts are just meant to call the Machine code embedded in the Operating system ? Overall it contains some junks of Machine Code - and some junks of codes to call the operating system .
Edited after Damon's Answer :
In the end OS is a set of machine codes . Basically OS would be doing the job of copy pasting user's Machine Code ( created by C Compiler ) and then if the instruction is a system call , the transfer goes to OS memory region for handling it . Now the question is what Machine Code generated in C can do this part ? Like asking to transfer control to OS etc - I suppose its system calls at higher abstraction but under the hood - how does it work .
I get a feeling its similar to chicken egg problem , C creates OS and C uses OS Cant find the exactly how the process goes .
Can anyone break the puzzle for me ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一件事并不排除另一件事。可执行文件是(除非它们是在虚拟机中运行的某种形式的字节码)机器代码。然而,有不同种类的指令,其中一些指令在某些权限级别下不可用。
这就是操作系统的用武之地,它是在最高权限级别运行的“机器代码”,充当“重要”部分和任务的仲裁者,例如决定谁获得 CPU 时间以及哪些值进入某些硬件寄存器。
(最初评论,按要求回答)
编辑:关于您的扩展问题,其工作原理大致如下。当计算机打开时,处理器以其最高权限级别运行。在这种“模式”下,BIOS、引导加载程序和操作系统可以做他们想做的事。这听起来不错,但您不希望任何类型的代码都能做它想做的事。
例如,代码可以告诉 MMU 允许读取或写入哪些内存页,哪些不允许。或者,它可以定义如果发生“特殊情况”(例如陷阱或中断)时调用哪个地址。或者,它可以直接写入一些映射某些设备(磁盘、网络等)端口的特殊内存地址。
最终,操作系统切换到“非特权”模式并调用一些非操作系统代码。当发生陷阱或中断时,执行会被中断并在其他地方继续(如操作系统先前指定的那样),并且特权级别会再次提升。一旦处理完中断,特权就会被取消,并再次调用用户代码。
如果用户程序需要操作系统执行“类似操作系统”的操作,它会根据约定的方案(例如在某些特定寄存器中)设置参数并执行陷阱指令。
例如,这就是多线程或虚拟内存等事物的实现方式。计时器会定期触发中断,该中断会停止“正常”代码的执行,并调用内核中的某些代码(在特权模式下)。然后,在某种优先级方案之后,该代码决定用户进程控制应该返回到哪个。这些是分发的“CPU 时间片”。
如果某个进程读取或写入不允许的页面,则 MMU 会生成陷阱。然后,操作系统查看发生的情况和位置,并决定是否将某些数据从磁盘加载到某些内存区域(并可能清除其他内容)并更改进程的映射,或者是否以“分段错误”错误终止进程。
当然,实际上,它要复杂一百万倍,但原则上这就是它的工作原理。
操作系统或程序最初是用 C 语言还是汇编程序编写的并不重要。对于处理器来说,它只是一系列机器指令。即使是 python 或 perl 脚本最终也“只是机器指令”,只是通过解释器绕道而行。
One thing does not exclude the other. Executables are (unless they are some form of bytecode running in a virtual machine) machine code. However, there are different kinds of instructions, some of which are not usable at certain privilegue levels.
That is where the operating system comes in, it is "machine code" that runs at the highest privilegue level, working as arbiter for the "important" parts and tasks, such as deciding who gets CPU time and what value goes into some hardware register.
(originally comment, made an answer by request)
EDIT: About your extended question, this works approximately as follows. When the computer is turned on, the processor runs at its highest privilegue level. In this "mode", the BIOS, the boot loader, and the operating system can do just what they want. This sounds great, but you don't want any kind of code being able to do just whatever it wants.
For example, the code can tell the MMU which memory pages are allowed to be read or written to, and which ones are not. Or, it can define what address is called if "something special" such as a trap or interrupt happens. Or, it can directly write to some special memory addresses that map ports of some devices (disk, network, whatever).
Eventually, the OS switches to "unprivileged" mode and calls some non-OS code. When a trap or interrupt happens, execution is interrupted and continues elsewhere (as specified by the OS previously), and the privilege level is upped again. Once the interrupt has been dealt with, privilege is taken away, and user code is called again.
If a user program needs the OS to do something "OS like", it sets up parameters according to an agreed scheme (for example in some particular registers) and executes a trap instruction.
This is for example how things like multithreading or virtual memory are implemented. In regular intervals, a timer fires off an interrupt, which stops execution of "normal" code, and calls some code in the kernel (in privileged mode). That code then decides what user process control should returned to, after some kind of priority scheme. Those are the "CPU time slices" that are handed out.
If some process reads from or writes to a page that it isn't allowed, a trap is generated by the MMU. The OS then looks at what happened and where, and decides whether to load some data from disk into some memory region (and possibly purge something else) and change the process' mappings, or whether to kill the process with a "segmentation fault" error.
Of course in reality, it is a million times more complicated, but in principle that's about as it works.
It does not really matter whether the OS or the programs were originally written in C or with an assembler. To the processor, it's just a sequence of machine instructions. Even a python or perl script is "just machine instructions" in the end, only with a detour via the interpreter.