lecture03
Shared by: smbutt
-
Stats
- views:
- 22
- posted:
- 8/25/2009
- language:
- English
- pages:
- 4
Document Sample


The Execution Cycle
Last lecture we introduced some of the key components of the stored program computers
Lecture 3 The Execution Cycle
This lecture is about what happens within the processor when a piece of code is executed
Some useful reading: – Tanenbaum 2.1.2 – Stallings 3.2 – Patterson 5.4 (more advanced)
Dr Iain Styles, School of Computer Science October 2006
2
The Processor Cycle
The Instruction Cycle
Almost all modern computers are synchronous machines
The clock cycle is something that we are all familiar with
Their timing is controlled by an external “clock” signal
This is just a square electric pulse that is supplied to the processor (and memory etc) by an external source
The instruction execution cycle is triggered by the clock cycle, but has several stages – Each stage is triggered by successive clock pulses – The exact timing depends on the details of a particular machine
Time
– A complete instruction cycle usually takes several clock cycles to execute The instruction cycle is divided into several stages – In some machines, some of these stages are performed simultaneously, which speeds things up – The stages are common to most architectures
The computers operations are therefore broken up in cycles
A cycle is triggered by the rising edge of the clock pulse
Whatever the computer does must be completed before the start of the next cycle (but tasks can be split up – pipelining) A machine running at 3GHz receives 3x10 clock pulses per second
3
9
Sometimes called the Fetch-Decode-Execute Cycle
– One pulse lasts 0.0000000003 seconds!
4
The Instruction Cycle 2
Starting a program
The steps that are followed when an instruction is executed are roughly as follows: 1) Inspect the program counter to find the address of the next instruction 2) Load the next instruction from memory into the instruction register 3) Update the program counter to point at the next instruction 4) Determine the type of instruction fetched
When you run a piece of code, the first thing that must be done is to load the code into memory – Copy it off the disk into the main memory Once the program is in memory, it can be executed
The instructions and data associated with the program will each occupy a block of memory (exact location is determined by the operating system)
The memory address of the first instruction in the program is called the entry point Running a new piece of code loads the entry point into the program counter PC = entry_point;
5) If the instruction requires data from memory, determine its address 6) Fetch the data from memory into one of the CPU registers 7) Execute the instruction 8) Return to step 1 for the next instruction
5 6
Once this has been done, the computer can then begin to run the code
Executing an Instruction 1
Executing an Instruction 2
The entry point is only the address of the first instruction – we need the instruction itself
At this point, the value of the PC is changed to point to the next instruction – Usually, this is just an increment PC = PC + 1; – But if there is a jump/branch in the code, the increment will be different – more on this later
At the start of the next clock cycle, the CPU issues (via the load/store unit) a request to the memory – Load/store unit sends the memory address and a request to read from the memory via the address bus
At some later time, the instruction will be received from the memory by the load/store unit – The instruction is put into the Instruction Register (IR) IR = memory(PC); – How long this takes depends on how fast the memory is Could be in time for the next clock cycle, or could be many cycles later if the instruction is in slow memory
The CPU then acts upon the instruction in the IR
First, the type of instruction is determined by the control unit
The instruction could be one which – Moves data around (e.g. a = b;) – Combines two operands (e.g. c = a + b) – Manipulates one operand (e.g. Bit shift/rotation) – Does a test/comparison and changes the program flow. This is known as a branch
7
8
Executing an Instruction 3
Executing an Instruction 4
Other types of instruction are used to – Call a new procedure/function (need to change PC) – Do I/O – Control loops – Do other special operations (e.g. Intel MMX)
Each variable is fetched from memory and loaded into one of the local registers within the CPU – Some instructions on some machines can operate directly on the contents of the memory Once the variables have been loaded, the control unit configures the ALU (or appropriate functional unit) to execute the operation
Once the type of instruction has been deduced, any data needed must be fetched from memory – To do a + b (the “add” instruction), need to fetch data items a and b from the memory
The result of the operation is usually stored in one of the registers, but can be written directly to the memory in some architectures
– This is done in the same way as the instruction was fetched – The operating system has assigned each variable a memory address
9 10
Some operations may change the program counter
Branches and Jumps
The Next Instruction
For example: if (a == b) ...run the code starting at address x (PC=x) else ...run the code starting at address y (PC=y) end – The result of the conditional determines which branch the program takes
Once the result of the operation is known, and any changes to the PC have been made the next instruction can be executed The next instruction follows exactly the same procedure
The basic instruction cycle is quite simple, and we can write a simple piece of pseudo-code which describes the cycle quite neatly
If we've reached the last statement of a loop, we need to jump back to the start of it – Need to change PC here as well
11
12
Pseudo-code for the Instruction Cycle
PC = entry_point; IR = memory(PC); PC = PC + 1; itype = type(IR); // Set the starting point while(program_is_running){ // Load next instruction // Increment PC // Find type of instr
Some comments
This is the basic instruction cycle that all modern machines use
Also called the fetch-decode-execute cycle – First, you have to fetch the instruction – Then you have to decode it (determine its type and its data requirements) – Finally you can execute it
data_loc = find_data(IR,itype); // Get data location(-1 if none) if(data_loc >= 0){ } execute(itype,data) } // execute instruction // if data is needed data = memory(data_loc); // Fetch it
Modern machines are a bit more complex: – Some machines use out-of-order execution to do things whilst other parts of the code are waiting for e.g. external data – Branch prediction is now common – the processor tries to predict which path of a branch will be taken and speculatively executes that section of code
We'll look at these later in the course
13 14
Summary
This lecture we have – Introduced the processor clock – Outlined the basic instruction execution cycle – Discussed what happens when the program has branches and jumps
Next lecture we will – Introduce the idea of an instruction set – Talk about different types of instruction set – Introduce some common types of instruction and explain what they do
15