Program Execution 程式的執行

John Smith發表於2014-10-17

A computer follows a program stored in its memory by copying the instructions from memory into the CPU as needed.
計算機執行程式時根據需要從記憶體中拷貝程式指令到CPU。

Once in the CPU, each instruction is decoded and obeyed.
指令在CPU中被解碼和執行。

The order in which the instructions are fetched from memory corresponds to the order in which the instructions are stored in memory unless otherwise altered by a JUMP instruction.
程式指令從記憶體中被fetch的順序對應程式指令在記憶體中的儲存順序,除非被JUMP指令打亂。

To understand how the overall execution process takes place, it is necessary to consider two of the special purpose registers within the CPU: the instruction register and the program counter (see again Figure 2.4).
為能從整體上了解程式的執行過程,必須知道CUP內兩種特殊用途暫存器:命令暫存器和程式計數器。(圖2.4)

The instruction register is used to hold the instruction being executed.
命令暫存器用來寄存將要被執行的指令。

The program counter contains the address of the next instruction to be executed, thereby serving as the machine's way of keeping track of where it is in the program.
程式計數器寄存著下一個被執行命令的地址,機器因此知道自身在被執行程式中的位置。

enter image description here

The CPU performs its job by continually repeating an algorithm that guides it through a three-step process known as the machine cycle.
CPU通過執行一種演算法不斷重複一個3步過程進行工作:這個3步過程叫做機器週期。

The steps in the machine cycle are fetch, decode, and execute (Figure 2.8).
機器週期的3步過程分別是:讀取,解碼,執行。(圖2.8)

During the fetch step, the CPU requests that main memory provide it with the instruction that is stored at the address indicated by the program counter.
1. fetch:CPU請求讀取由程式計數器提供的主存地址內的指令。

Since each instruction in our machine is two bytes long, this fetch process involves retrieving the contents of two memory cells from main memory.
我們這臺機器上的指令長度均為兩位元組,因此需要從兩個記憶體單元fetch內容。

The CPU places the instruction received from memory in its instruction register and then increments the program counter by two so that the counter contains the address of the next instruction stored in memory.
CPU把記憶體傳送過來的指令寄存到指令暫存器,然後把程式計數器的值增加2,這樣程式計數器就包含了下一個要fetch指令的記憶體地址。

Thus the program counter will be ready for the next fetch.
這樣程式計數器已經為下一次fetch作好了準備。

enter image description here

With the instruction now in the instruction register, the CPU decodes the instruction, which involves breaking the operand field into its proper components based on the instruction's op-code.
2. decode:CPU decode 指令暫存器內的指令,這需要根據指令的操作碼來正確分解指令的運算元域。

The CPU then executes the instruction by activating the appropriate circuitry to perform the requested task.
3. execute:CPU把指令分配到相應的電路去執行。

For example, if the instruction is a load from memory, the CPU sends the appropriate signals to main memory, waits for main memory to send the data, and then places the data in the requested register; if the instruction is for an arithmetic operation, the CPU activates the appropriate circuitry in the arithmetic/logic unit with the correct registers as inputs and waits for the arithmetic/logic unit to compute the answer and place it in the appropriate register.
舉例:如果指令是要從記憶體載入資料,CPU就會向主存傳送相應的訊號,等待主存傳送資料,然後把資料寄存到指定的暫存器中;如果指令是一個算術操作,CPU就會啟用算術/邏輯單元內相應的電路,並提供暫存器內的資料,等待執行完成後把結果寄存到相應的暫存器。

Once the instruction in the instruction register has been executed, the CPU again begins the machine cycle with the fetch step.
一旦指令暫存器內的指令被執行,CPU繼續執行機器迴圈的第一步:fetch。

Observe that since the program counter was incremented at the end of the previous fetch, it again provides the CPU with the correct address.
上次fetch後程式計數器已經加2,現在能夠立刻提供給CPU正確的地址。

A somewhat special case is the execution of a JUMP instruction.
一個特例是JUMP指令。

Consider, for example, the instruction B258 (Figure 2.9), which means "JUMP to the instruction at address 58 (hexadecimal) if the contents of register 2 is the same as that of register 0."
考慮一下指令B258(圖2.9):比較暫存器2和暫存器0的內容,如果一樣,JUMP到記憶體地址為58(十六進位制)的指令。

In this case, the execute step of the machine cycle begins with the comparison of registers 2 and 0.
在上例中,機器迴圈的execute,開始於比較兩個暫存器的內容。

If they contain different bit patterns, the execute step terminates and the next machine cycle begins.
如果它們包含的位模式不同,execute終結,下一個機器迴圈開始。

If, however, the contents of these registers are equal, the machine places the value 58 (hexadecimal) in its program counter during the execute step.
如果兩個暫存器的內容相同,CPU把58(十六進位制)寄存到程式計數器中,此時機器迴圈處於execute階段(而非fetch)。

In this case, then, the next fetch step finds 58 in the program counter, so the instruction at that address will be the next instruction to be fetched and executed.
這樣,到下一次fetch,按照程式計數器的值為58進行。

enter image description here

Note that if the instruction had been B058, then the decision of whether the program counter should be changed would depend on whether the contents of register 0 was equal to that of register 0.
注意,如果指令是B058,那麼程式計數器的值是否要被改變取決於暫存器0和暫存器0的內容是否相等。

But these are the same registers and thus must have equal content.
暫存器0和它自身當然相等。

In turn, any instruction of the form B0XY will cause a jump to be executed to the memory location XY regardless of the contents of register 0.
所以,任何形如B0XY的指令都會導致一個JUMP的繼續執行(而非終止)。

An Example of Program Execution

Let us follow the machine cycle applied to the program presented in Figure 2.7, which retrieves two values from main memory, computes their sum, and stores that total in a main memory cell.
圖2.7:從主存獲取兩個值,計算它們的和,並把結果儲存到一個主存單元。

We first need to put the program somewhere in memory.
首先要把程式放進記憶體。

For our example, suppose the program is stored in consecutive addresses, starting at address A0 (hexadecimal).
假設程式儲存在連續的地址中,第一個地址為A0。

With the program stored in this manner, we can cause the machine to execute it by placing the address (A0) of the first instruction in the program counter and starting the machine (Figure 2.10).
機器要執行程式,首先把地址A0寄存到程式計數器。(圖2.10)

enter image description here

The CPU begins the fetch step of the machine cycle by extracting the instruction stored in main memory at location A0 and placing this instruction (156C) in its instruction register (Figure 2.11a).
CPU開始機器迴圈的第一步fetch:讀取地址A0上的指令(156C),並把它寄存到指令暫存器。(圖2.11a)

Notice that, in our machine, instructions are 16 bits (two bytes) long.
注意,這臺機器的指令長度為16位。

Thus the entire instruction to be fetched occupies the memory cells at both address A0 and A1.
因此fetch的首個指令佔據A0和A1兩個地址單元。

The CPU is designed to take this into account so it retrieves the contents of both cells and places the bit patterns received in the instruction register, which is 16 bits long.
CPU的設計已經考慮到這一點,所以它讀取兩個地址單元上的內容,並寄存到指令暫存器,暫存器的長度為16位)。

The CPU then adds two to the program counter so that this register contains the address of the next instruction (Figure 2.11b).
CPU然後把程式計數器的值加2(圖2.11b)。

At the end of the fetch step of the first machine cycle, the program counter and instruction register contain the following data:
第一個機器週期的fetch結束時,程式計數器和指令暫存器分別包含如下資料:

Program Counter: A2
Instruction Register: 156C

enter image description here

Next, the CPU analyzes the instruction in its instruction register and concludes that it is to load register 5 with the contents of the memory cell at address 6C.
然後,CPU分析(decode)指令暫存器內的指令,並得出結論:為暫存器5載入記憶體單元6C上的內容。

This load activity is performed during the execution step of the machine cycle, and the CPU then begins the next cycle.
上面load這一步發生在機器週期的execute;CPU開始下一個週期。

This cycle begins by fetching the instruction 166D from the two memory cells starting at address A2.
從兩個記憶體單元上fetch指令166D。

The CPU places this instruction in the instruction register and increments the program counter to A4. The values in the program counter and instruction register therefore become the following:
把指令寄存到指令暫存器,為程式計數器加2:

Program Counter: A4
Instruction Register: 166D

Now the CPU decodes the instruction 166D and determines that it is to load register 6 with the contents of memory address 6D.
CPU decode 指令166D:為暫存器6載入記憶體地址6D上的內容。

It then executes the instruction. It is at this time that register 6 is actually loaded.
execute上一步解碼的指令。

Since the program counter now contains A4, the CPU extracts the next instruction starting at this address.
程式計數器的值為A4,CPU開始下一個fetch。

The result is that 5056 is placed in the instruction register, and the program counter is incremented to A6.
指令暫存器獲得指令5056,程式計數器加2後得到A6。

The CPU now decodes the contents of its instruction register and executes it by activating the two's complement addition circuitry with inputs being registers 5 and 6.
CPU decode 並 execute 指令5056:啟用二進位制補碼假髮電路,並輸入暫存器5和6上的內容。

During this execution step, the arithmetic/logic unit performs the requested addition, leaves the result in register 0 (as requested by the control unit), and reports to the control unit that it has finished.
在execute期間:算數/邏輯單元執行了加法運算,把結果寄存到暫存器0(由控制單元決定),並通知控制單元計算結束。

The CPU then begins another machine cycle. Once again, with the aid of the program counter, it fetches the next instruction (306E) from the two memory cells starting at memory location A6 and increments the program counter to A8. This instruction is then decoded and executed. At this point, the sum is placed in memory location 6E.

The next instruction is fetched starting from memory location A8, and the program counter is incremented to AA. The contents of the instruction register (C000) are now decoded as the halt instruction. Consequently, the machine stops during the execute step of the machine cycle, and the program is completed.

In summary, we see that the execution of a program stored in memory involves the same process you and I might use if we needed to follow a detailed list of instructions. Whereas we might keep our place by marking the instructions as we perform them, the CPU keeps its place by using the program counter. After determining which instruction to execute next, we would read the instruction and extract its meaning. Then, we would perform the task requested and return to the list for the next instruction in the same manner that the CPU executes the instruction in its instruction register and then continues with another fetch.

Programs Versus Data

Many programs can be stored simultaneously in a computer's main memory, as long as they occupy different locations. Which program will be run when the machine is started can then be determined merely by setting the program counter appropriately.

One must keep in mind, however, that because data are also contained in main memory and encoded in terms of 0s and 1s, the machine alone has no way of knowing what is data and what is program. If the program counter were assigned the address of data instead of the address of the desired program, the CPU, not knowing any better, would extract the data bit patterns as though they were instructions and execute them. The final result would depend on the data involved.

We should not conclude, however, that providing programs and data with a common appearance in a machine's memory is bad. In fact, it has proved a useful attribute because it allows one program to manipulate other programs (or even itself) the same as it would data. Imagine, for example, a program that modifies itself in response to its interaction with its environment and thus exhibits the ability to learn, or perhaps a program that writes and executes other programs in order to solve problems presented to it.

相關文章