The machine instruction cycle: Difference between revisions

From Computer Science Wiki
No edit summary
No edit summary
 
(25 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[File:binary.png|frame|right|This is a basic concept in computer science]]
[[File:binary.png|frame|right|This is a basic concept in computer science]]


The machine instruction cycle describes the order that instructions are processed in a computer.


Instructions are processed under the direction of the control unit in a step-by-step manner.
[[File:Fetch-execute-cycle.png|400px]]


There are four fundamental steps in the instruction cycle:
<br />


'''1.  Fetch the instruction'''
The machine instruction cycle is the process a CPU follows to execute instructions stored in memory. It consists of four main stages: Fetch, Decode, Execute, and Store. Below is a breakdown of the cycle, along with the roles of key components such as the Control Unit (CU), Arithmetic Logic Unit (ALU), and various registers.
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.


'''2. Decode the instruction'''
=== 1. Fetch ===
The decoder interprets the instruction. During this cycle the instruction inside the IR (instruction register) gets decoded.
The Control Unit (CU) initiates the fetch stage by using the Program Counter (PC) to store the address of the next instruction to be executed. This address is sent to the Memory Address Register (MAR), which holds the address of the required instruction in memory.


'''3. Execute'''
* The CU signals the memory to retrieve the instruction at the address in the MAR.
The Control Unit of 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 retrieved instruction is temporarily stored in the Memory Data Register (MDR).
* The instruction is then transferred to the Current Instruction Register (CIR) for decoding.
* The PC is incremented to point to the address of the next instruction in RAM.


'''4. Store result'''
=== 2. Decode ===
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.
Once the instruction is in the CIR, the Control Unit (CU) decodes it to determine what operation needs to be performed. The decoded instruction identifies whether data needs to be retrieved, stored, or manipulated.


=== 3. Execute ===
The Arithmetic Logic Unit (ALU) performs the necessary calculations or logical operations, depending on the instruction. For example, if the instruction involves an arithmetic operation (addition, subtraction, etc.), the ALU processes the data.


'''Data Bus:'''
* Data required for the operation may be fetched from memory or registers.
A data bus is a connection between the different parts of a computer that information is sent on.
* The Accumulator (ACC) stores the result of operations carried out by the ALU, such as the sum of two numbers.


'''Address Bus:'''
=== 4. Store ===
The address bus is a data bus that is used to specify a physical address. A CPU will specify the memory location.<ref>https://compsci2014.wikispaces.com/2.1.4+Explain+the+machine+instruction+cycle</ref>
After execution, the result in the ACC may need to be stored back into memory or passed to another register for further operations. If the instruction involves interacting with external devices, the Interrupt Register (IR) may be involved, handling requests from I/O devices to ensure they are serviced when required.


== A helpful video ==
During the entire instruction cycle, the Control Unit (CU) coordinates the movement of data between the registers and manages the flow of control within the CPU.


<html>
=== Registers' Summary ===
<iframe width="420" height="315" src="https://www.youtube.com/embed/7hB2SEZ3zqg" frameborder="0" allowfullscreen></iframe>
PC (Program Counter): Stores the address of the next instruction to be fetched.
</html>
MAR (Memory Address Register): Stores the memory address of the data or instruction to be fetched or stored.
MDR (Memory Data Register): Holds the data fetched from or to be written to memory.
CIR (Current Instruction Register): Stores the current instruction being decoded and executed.
ACC (Accumulator): Holds the result of calculations performed by the ALU.


== Do you understand this topic? ==
The cycle then repeats, fetching the next instruction from memory as indicated by the Program Counter (PC).


* Explain the machine instruction cycle.


== Do you have an advanced understanding about this topic? ==


* Simulate the machine instruction cycle.
== Excellent simulations of the machine instruction cycle ==
 
# It can be helpful to imagine there is a little mailman inside your computer! [https://en.wikipedia.org/wiki/Little_man_computer Click here to learn more about this technique]
# [https://tools.withcode.uk/cpu/ Click here for a great simulation! ]
# [http://www.peterhigginson.co.uk/LMC/ This is also very good simulation (little man simulation)]


== References ==
== References ==

Latest revision as of 11:18, 11 September 2024

This is a basic concept in computer science
The machine instruction cycle describes the order that instructions are processed in a computer. 

Fetch-execute-cycle.png


The machine instruction cycle is the process a CPU follows to execute instructions stored in memory. It consists of four main stages: Fetch, Decode, Execute, and Store. Below is a breakdown of the cycle, along with the roles of key components such as the Control Unit (CU), Arithmetic Logic Unit (ALU), and various registers.

1. Fetch[edit]

The Control Unit (CU) initiates the fetch stage by using the Program Counter (PC) to store the address of the next instruction to be executed. This address is sent to the Memory Address Register (MAR), which holds the address of the required instruction in memory.

  • The CU signals the memory to retrieve the instruction at the address in the MAR.
  • The retrieved instruction is temporarily stored in the Memory Data Register (MDR).
  • The instruction is then transferred to the Current Instruction Register (CIR) for decoding.
  • The PC is incremented to point to the address of the next instruction in RAM.

2. Decode[edit]

Once the instruction is in the CIR, the Control Unit (CU) decodes it to determine what operation needs to be performed. The decoded instruction identifies whether data needs to be retrieved, stored, or manipulated.

3. Execute[edit]

The Arithmetic Logic Unit (ALU) performs the necessary calculations or logical operations, depending on the instruction. For example, if the instruction involves an arithmetic operation (addition, subtraction, etc.), the ALU processes the data.

  • Data required for the operation may be fetched from memory or registers.
  • The Accumulator (ACC) stores the result of operations carried out by the ALU, such as the sum of two numbers.

4. Store[edit]

After execution, the result in the ACC may need to be stored back into memory or passed to another register for further operations. If the instruction involves interacting with external devices, the Interrupt Register (IR) may be involved, handling requests from I/O devices to ensure they are serviced when required.

During the entire instruction cycle, the Control Unit (CU) coordinates the movement of data between the registers and manages the flow of control within the CPU.

Registers' Summary[edit]

PC (Program Counter): Stores the address of the next instruction to be fetched. MAR (Memory Address Register): Stores the memory address of the data or instruction to be fetched or stored. MDR (Memory Data Register): Holds the data fetched from or to be written to memory. CIR (Current Instruction Register): Stores the current instruction being decoded and executed. ACC (Accumulator): Holds the result of calculations performed by the ALU.

The cycle then repeats, fetching the next instruction from memory as indicated by the Program Counter (PC).


Excellent simulations of the machine instruction cycle[edit]

  1. It can be helpful to imagine there is a little mailman inside your computer! Click here to learn more about this technique
  2. Click here for a great simulation!
  3. This is also very good simulation (little man simulation)

References[edit]