1.4 处理器读取并解释在存储中的指令(Processors Read and Interpret Instructions Stored in Memory)
现在我们的hello.c源程序被编译器翻译成可执行对象文件,这个文件叫做hello并存储在硬盘中。要执行这样的文件在Unix系统中,我们需要在shell中输入程序的名称。
linux> ./hello
hello, world
linux>
shell是一个命令行的编译器,输出一个提示符且在等待输入命令,然后执行命令。如果这个命令的第一个单词不是一个内置命令,那么shell就会假设这是一个可执行文件名字,它将会加载并运行这个文件。在这个例子中,shell会加载和运行这个程序,然后等待终止。hello程序打印它的信息在屏幕上然后停止。然后shell输出一个提示符并等待下一个指令。
图1.4 一个典型系统的硬件组成
包括CPU(中央处理器)、ALU(算数逻辑单元)、PC(程序计数器)、USB(通用串行总线)
1.4.1 系统的硬件组成部分
想要知道在运行hello程序时发生了什么,我们需要了解如图1.4 经典系统的硬件组成。这张图是Intel家族的模型图,不过其他系统也会有类型的外观和结构,所以不用担心这张图的复杂性。我们会通过这本书分阶段介绍它的各种细节。
总线
贯穿整个系统的是称为总线的电子管道集合,它携带字节信息在各个组件中往返。总线通常被设计成传送固定大小的字节块,称为字。 字节是系统的基本参数,现在大部分的机器都是4个字节(32位)或8个字节(64位)。在本书中,我们不假设任何固定定义的字,相反我们会在上下文用到字的时候,会说明该定义。
IO设备
在第6章会更详细说明IO设备(硬盘)是如何工作的。在10章,你会学习到如何在你的程序中在Unix系统通过接口连接IO设备。我们会特别关注网络设备,但是一般来说其他类型的设备技术也差不多。
输入输出设备是系统的外部世界。我们的样机包括四种IO设备,包括用于输入的鼠标键盘,用于输出的显示器,和存储数据和程序的硬盘。一开始,可执行程序是存放在硬盘中。
每一个IO设备都是通过控制器或适配器连接在IO总线上。他们的区别主要是集成方式。
控制器集成在设备本身或者集成在系统的主板上。适配器一般作为一个卡插在主板上。无论怎样,其目的就是为了在IO总线和IO设备之间往返传输信息。
主存
主存是临时存储设备,当处理器在执行程序的时候,存放程序和程序要处理的数据。物理上,主存是由动态随机存储器组成的。逻辑上,内存是由存放字节的线性数组组成,数组每一个单元由0开始且有唯一标识的地址。一般来说,程序的每一个机器指令可以包含含有多组字节的。数据的大小对应着C语言程序的数据类型。例如,一台64位的X86机器运行的Linux系统,short类型需要2个字节。
第6章讲具体介绍内存技术(如DRAM是如何工作的,他们如何组合成内存的)。
中央处理器
中央处理单元,或处理器是在解释或执行存储在内存中的指令。其核心是容量为一个字大小的存储设备称为程序计数器。在任何时刻,程序计数器都会指向在内存中的机器指令集的某一条。
注:PC一般叫做个人电脑,但是他们的区别可以从上下文可以得知(其意思)。
从系统通电开始知道系统断电结束,这个过程中,处理器通过程序计数器指向下一条指令的动作重复着执行指令。处理器似乎是根据一种非常简单的指令执行模型和指令结构执行操作的,在这个模型中,指令在有序的队列中执行,执行一个简单的指令需要执行多个步骤。处理器根据程序计数器指向,读取内存的指令,在指令中解释成比特,执行在指令中定义好的简单操作,然后程序计数器更新并指向下一条指令,这条需要被执行的指令有可能是在内存中相邻或不相邻。上述的几个简单步骤,需要经过主存、寄存器、算数逻辑单元。寄存器是一个很小的存储设备,容量仅仅有1个字那么大,每个寄存器都有唯一的名称。ALU计算新的数据和地址值。以下是CPU指令要求可能会执行的简单例子:
- 加载:从内存中拷贝一个字节或一个字的内容到寄存器,从而覆盖在寄存器上一个内容。
- 存储:从寄存器中拷贝一个字节或一个字的内容到内存,从而覆盖在内存那个位置上一个内容。
- 操作:拷贝两个字的寄存器的内容到算数逻辑单元,在这两个字内容上执行算数运算,并且把结果保存在寄存器,从而覆盖在寄存器上一个内容。
- 跳转:从指令本身提取出一个字的内容,并且拷贝内容到程序计数器,从而覆盖在程序计数器上一个内容。
我们说过处理器似乎是一种它的指令结构简单应用,但事实上处理器会用很多复杂的方法来提升程序的执行效率。所以,我们需要知道处理器的指令结构和微结构的区别。当我们学习第3章的时候,我们会从机器的指令集上认识到它的抽象性。在第4章将会更详细介绍处理器实际上是如何实现的。第5章会根据现代处理器模型如何预测和优化机器语言程序的执行展开学习。
1.4.2 运行hello程序
通过以下简单的系统硬件组织和操作的示意图,我们开始懂得什么时候运行hello程序。我们必须忽略一些细节,这些后续将会补充上,现在我们需要从整体上去思考问题。
一开始,shell程序在执行自己的指令,同时在等待我们敲入命令。当我们在键盘上敲入字符./hello的时候,shell程序把每个字符读到寄存器,然后保存在内存,如图1.5所示。
当我们在键盘上敲下回车的时候,shell程序就知道我们完成了指令的输入。shell程序通过在hello程序中拷贝代码和数据等一系列的指令,加载到hello可执行程序,这个过程是把硬盘上的程序存储到内存中。
图 1.5 从键盘上读取指令
数据包括hello,world\n将会完整得打印出来。用的是直接存储器访问技术(DMA,在第6章会讨论),数据正确地从硬盘到内存,这个过程不会经过处理器。这个步骤如图1.6 所示。
一旦hello对象文件的代码和数据加载到内存,处理器开始执行hello程序的main程序中的机器语言指令,这些指令把”hello,world\n”从内存拷贝到寄存器,再到可以显示结果的显示设备。这个步骤如图1.7所示。
1.4 Processors Read and InterpretInstructions Stored in Memory
At this point, our hello.c source program has been translated by the compilation system into an executable object file called hello that is stored on disk. To run the executable file on a Unix system, we type its name to an application program known as a shell:
linux> ./hello
hello, world
linux>
The shell is a command-line interpreter that prints a prompt, waits for you to type a command line, and then performs the command. If thefirst word of the command line does not correspond to a built-in shellcommand, then the shell
assumes that it is the name of an executable file that it should load and run. So in this case, the shell loads and runs the hello program and then waits for it to terminate. The hello program prints its message to the screen and then terminates. The shell then prints a prompt and waits for the next input command line.
Figure 1.4 Hardware organization of a typical system.
CPU: central processing unit, ALU: arithmetic/logic unit, PC: program
counter, USB: Universal Serial Bus.
1.4.1 Hardware Organization of aSystem
To understand what happens to our hello program when we run it,we need to understand the hardware organization of a typical system,which is shown in Figure 1.4 . This particular picture is modeled after the family of recent Intel systems, but all systems have a similar look and feel. Don’t worry about the complexity of this figure just now.We will get to its various details in stages throughout the course of thebook.
Buses
Running throughout the system is a collection of electrical conduits called buses that carry bytes of information back and forth between the components. Buses are typically designed to transfer fixed-size chunks of bytes known as words. The number of bytes in a word (theword size) is a fundamental system parameter that varies across systems. Most machines today have word sizes of either 4 bytes (32bits) or 8 bytes (64 bits). In this book, we do not assume any fixed definition of word size. Instead, we will specify what we mean by a”word” in any context that requires this to be defined.
I/O Devices
Input/output (I/O) devices are the system’s connection to the externalworld. Our example system has four I/O devices: a keyboard andmouse for user input, a display for user output, and a disk drive (or simply disk) for long-term storage of data and programs. Initially, theexecutable hello program resides on the disk.
Each I/O device is connected to the I/O bus by either a controller or an adapter. The distinction between the two is mainly one of packaging.
Controllers are chip sets in the device itself or on the system’s mainprinted circuit board (often called the motherboard). An adapter is a card that plugs into a slot on the motherboard. Regardless, thepurpose of each is to transfer information back and forth between theI/O bus and an I/O device.
Chapter 6 has more to say about how I/O devices such as disks work. In Chapter 10 , you will learn how to use the Unix I/O interfaceto access devices from your application programs. We focus on the especially interesting class of devices known as networks, but the techniques generalize to other kinds of devices as well.
Main Memory
The main memory is a temporary storage device that holds both a program and the data it manipulates while the processor is executing the program. Physically, main memory consists of a collection of dynamic random access memory(DRAM) chips. Logically, memory is organized as a linear array of bytes, each with its own unique address (array index) starting at zero. In general, each of the machine instructions that constitute a program can consist of a variable number of bytes. The sizes of data items that correspond to C program variables vary according to type. For example, on an x86-64 machine running Linux, data of type short require 2 bytes, types int and float 4 bytes, and types long and double 8 bytes.
Chapter 6 has more to say about how memory technologies suchas DRAM chips work, and how they are combined to form main memory.
Processor
The central processing unit (CPU), or simply processor, is the engine that interprets (or executes) instructions stored in main memory. At its core is a word-size storage device (or register) called the program counter (PC). At any point in time, the PC points at (contains the address of) some machine-language instruction in main memory.
PC is also a commonly used acronym for “personal computer.” However, the distinction between the two should be clear from the context.
From the time that power is applied to the system until the time that the power is shut off, a processor repeatedly executes the instruction pointed at by the program counter and updates the program counter to point to the next instruction. A processor appears to operate according to a very simple instruction execution model, defined by its instruction set architecture. In this model, instructions execute in strict sequence, and executing a single instruction involves performing a series of steps. The processor reads the instruction from memory pointed at by the program counter (PC), interprets the bits in the instruction, performs some simple operation dictated by the instruction, and then updates the PC to point to the next instruction, which may or may not be contiguous in memory to the instruction that was just executed. There are only a few of these simple operations, and they revolve around main memory, the register file, and the arithmetic/logic unit (ALU). The register file is a small storage device that consists of acollection of word-size registers, each with its own unique name. TheALU computes new data and address values. Here are some examples of the simple operations that the CPU might carry out at the request of an instruction:
- Load: Copy a byte or a word from main memory into a register,overwriting the previous contents of the register.
- Store: Copy a byte or a word from a register to a location in mainmemory, overwriting the previous contents of that location
- Operate: Copy the contents of two registers to the ALU, performan arithmetic operation on the two words, and store the result in a register, overwriting the previous contents of that register
- Jump: Extract a word from the instruction itself and copy that wordinto the program counter (PC), overwriting the previous value ofthe PC.
We say that a processor appears to be a simple implementation of its instruction set architecture, but in fact modern processors use far more complex mechanisms to speed up program execution. Thus, we can distinguish the processor’s instruction set architecture, describing the effect of each machine-code instruction, from its microarchitecture, describing how the processor is actually implemented. When we study machine code in Chapter 3 , we will consider the abstraction provided by the machine’s instruction set architecture.Chapter 4 has more to say about how processors are actually implemented.Chapter 5 describes a model of how modern processors work that enables predicting and optimizing the performance of machinelanguage programs.
1.4.2 Running the hello Program
Given this simple view of a system’s hardware organization and operation, we can begin to understand what happens when we run ourexample program. We must omit a lot of details here that will be filledin later, but for now we will be content with the big picture.
Initially, the shell program is executing its instructions, waiting for us to type a command. As we type the characters ./hello at the keyboard,the shell program reads each one into a register and then stores it inmemory, as shown in Figure 1.5 .
When we hit the enter key on the keyboard, the shell knows that wehave finished typing the command. The shell then loads the executable hello file by executing a sequence of instructions that copies the code and data in the hello object file from disk to main memory.
Figure 1.5 Reading the hello command from the keyboard.
The data includes the string ocharacters hello, world\n that will eventually be printed out.Using a technique known as direct memory access (DMA, discussein Chapter 6 ), the data travel directly from disk to main memory,without passing through the processor. This step is shown in Figur1.6 .Once the code and data in the hello object file are loaded intomemory, the processor begins executing the machine-languageinstructions in the hello program’s main routine. These instructioncopy the bytes in the hello, world\n string from memory to theregister file, and from there to the display device, where they aredisplayed on the screen. This step is shown in Figure 1.7 .
今天的文章深入理解计算机系统:1.4 处理器读取并解释在存储中的指令(Processors Read and Interpret Instructions Stored in Memory)分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/66844.html