11.5.3 Program execution
We now address the final piece of the puzzle concerning machine language programs by illustrating how the machine actually goes about executing a program. At the machine level, all a computer ever does is perform the following task, known as the instruction cycle, over and over.
The instruction cycle consists of five steps:
That’s all a computer ever does.
To execute the program of Figure 11.28 a three (0000 0011two) must first be loaded into the Watson Virtual Machine program counter. The reason for this is that the first executable instruction of the program is located at memory address three.
As the program runs, the machine will faithfully execute the instruction cycle. First, the instruction “0001 1101 0000 0001” will be loaded into the instruction register. The program counter will then be incremented from three to four. Next, the instruction is decoded to mean “load into register D the value stored at memory location one”. The value at location one will then be loaded into register D. Since location one was initialized to a two as part of the construction of the program’s data segment, that value will be copied into register D. Having completed the execution of the current instruction, the machine will return to step one of the instruction cycle.
At this point, the Watson Virtual Machine copies into the instruction register the statement held at memory location four. The program counter is then incremented to five, so that it points to the next instruction in the current sequence. The contents of the instruction register “0001 1110 0000 0010” are then decoded to mean “load into register E the value stored at memory location two”. This instruction is then executed – placing the value three into register E. Next, the machine returns to the first step in the instruction cycle.
The instruction cycle repeats itself on statement after statement, until a HALT instruction is executed (or the instruction cycle is interrupted by outside influences – such as someone hitting the reset button or tripping over the power cord).
And so, that’s it. We have arrived at the bare machine that lies underneath the many layers of software. There is no magic. No smoke and mirrors. Just a simple – but very fast – machine running through the instruction cycle over and over, tens or hundreds of millions of times each second.
Exercises for Section 11.5