This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)(Learn how and when to remove this template message)
An instruction cycle (sometimes called a fetch–decode–execute cycle) is the basic operational process of a computer. It is the process by which a computer retrieves a program instruction from its memory, determines what actions the instruction dictates, and carries out those actions. This cycle is repeated continuously by a computer's central processing unit (CPU), from boot-up to when the computer is shut down.
In simpler CPUs the instruction cycle is executed sequentially, each instruction being processed before the next one is started. In most modern CPUs the instruction cycles are instead executed concurrently, and often in parallel, through an instruction pipeline: the next instruction starts being processed before the previous instruction has finished, which is possible because the cycle is broken up into separate steps.
- Program counter (PC)
- An incrementing counter that keeps track of the memory address of the instruction that is to be executed next or in other words, holds the address of the instruction to be executed next.
- Memory address register (MAR)
- Holds the address of a block of memory for reading from or writing to.
- Memory data register (MDR)
- A two-way register that holds data fetched from memory (and ready for the CPU to process) or data waiting to be stored in memory. (This is also known as the memory buffer register (MBR).)
- Instruction register (IR)
- A temporary holding ground for the instruction that has just been fetched from memory.
- Control unit (CU)
- Decodes the program instruction in the IR, selecting machine resources, such as a data source register and a particular arithmetic operation, and coordinates activation of those resources.
- Arithmetic logic unit (ALU)
- Performs mathematical and logical operations.
- Floating-point unit (FPU)
- Performs floating-point operations.
Each computer's CPU can have different cycles based on different instruction sets, but will be similar to the following cycle:
- Fetch the instruction: The next instruction is fetched from the memory address that is currently stored in the program counter (PC), and stored in the instruction register (IR). At the end of the fetch operation, the PC points to the next instruction that will be read at the next cycle.
- Decode the instruction: During this cycle the encoded instruction present in the IR (instruction register) is interpreted by the decoder.
- Read the effective address: In case of a memory instruction (direct or indirect) the execution phase will be in the next clock pulse. If the instruction has an indirect address, the effective address is read from main memory, and any required data is fetched from main memory to be processed and then placed into data registers (Clock Pulse: T3). If the instruction is direct, nothing is done at this clock pulses. If this is an I/O instruction or a Register instruction, the operation is performed (executed) at clock Pulse.
- Execute the instruction: The control unit of the CPU passes the decoded information as a sequence of control signals to the relevant function units of the CPU to perform the actions required by the instruction such as reading values from registers, passing them to the ALU to perform mathematical or logic functions on them, and writing the result back to a register. If the ALU is involved, it sends a condition signal back to the CU. The result generated by the operation is stored in the main memory, or sent to an output device. Based on the condition of any feedback from the ALU, Program Counter may be updated to a different address from which the next instruction will be fetched.
The cycle is then repeated.
Initiating the cycle
The cycle begins as soon as power is applied to the system, with an initial PC value that is predefined by the system's architecture (for instance, in Intel IA-32 CPUs, the predefined PC value is
0xfffffff0). Typically this address points to a set of instructions in read-only memory (ROM), which begins the process of loading (or booting) the operating system.
Fetching the instruction
Step 1 of the instruction cycle is fetch cycle, which is the same for each instruction:
- The CPU sends the PC to the MAR and sends a read command on the address bus
- In response to the read command (with address equal to PC), the memory returns the data stored at the memory location indicated by PC on the data bus
- The CPU copies the data from the data bus into its MDR (also known as MBR, see section Components above)
- A fraction of a second later, the CPU copies the data from the MDR to the instruction register (IR)
- The PC is incremented so that it points to the following instruction in memory. This step prepares the CPU for the next cycle.
The control unit fetches the instruction's address from the memory unit.
Decoding the instruction
Step 2 of the instruction Cycle is called the Decode Cycle. The decoding process allows the CPU to determine what instruction is to be performed, so that the CPU can tell how many operands it needs to fetch in order to perform the instruction. The opcode fetched from the memory is decoded for the next steps and moved to the appropriate registers. The decoding is done by the CPU's Control Unit.
Reading the effective address
Step 3 is evaluating which operation it is. If this is a Memory operation - in this step the computer checks if it's a direct or indirect memory operation:
- Direct memory instruction - Nothing is being done.
- Indirect memory instruction - The effective address is being read from the memory.
If this is an I/O or Register instruction - the computer checks its kind and executes the instruction.
Executing the instruction
Step 4 of the Instruction Cycle is the Execute Cycle. Here, the function of the instruction is performed. If the instruction involves arithmetic or logic, the Arithmetic Logic Unit is utilized. This is the only stage of the instruction cycle that is useful from the perspective of the end user. Everything else is overhead required to make the execute phase happen.