If you’re anything like me, you’re probably curious about how your computer runs the programs you use every day. Here’s a quick rundown of the steps your CPU takes to run a program, courtesy of your friendly neighborhood blog writer.
- The program is loaded into memory from storage.
- The CPU fetches instructions from memory and decodes them.
- The CPU executes the instructions and produces results.
- The results are stored in memory or output to the user.
So there you have it! That’s how your computer runs a program, in a nutshell. Thanks for reading and feel free to leave any questions or comments below!
Each instruction has two parts: opcode and operand
The instruction set is the basic, built-in commands that your CPU can understand and execute. Every program you run on your computer consists of a series of these instructions. The two parts of an instruction are the opcode (operation code) and the operand (usually either a number or an address). The opcode tells the CPU what operation to perform, while the operand tells it which data to perform that operation.
In a simple pipelined CPU, each instruction goes through four stages:
- Fetch: The instruction is fetched from memory.
- Decode: The instruction is decoded, and the operands are read from registers.
- Execute: The instruction is executed.
- Writeback: The result is written back to memory (if needed).
The opcode is a set of instructions that tells the CPU what to do. It is the first step in the process of running a program. The opcode is followed by one or more operands, which are the data that the instruction uses.
The most common opcodes are:
- Load: Loads data from memory into a register
- Store: Stores data from a register into memory
- Add: Adds two values and stores the result in a register
- Subtract: Subtracts two values and stores the result in a register
- Multiply: Multiplies two values and stores the result in a register
- Divide: Divides two values and stores the result in a register
In order to run a program, the CPU must be able to understand the instructions written in that program. The language that these instructions are written in is called instruction set. An operand is an entity on which the operation is carried out by the instruction. For example, in the instruction ADD A, B; A and B are operands.
The instruction set of CPU consists of a number of operations like data transfer, arithmetic, logic etc. Each operation requires certain number of operands to be specified along with it. Data transfer operations require two operands – one for source and one for destination. Arithmetic and logic operations require two or three operands depending on the nature of operation
1) one-byte instructions
The 8086 can fetch and execute a one-byte instruction in a single clock cycle. The machine language instructions of the 8086 are all one byte long and are encoded using a special encoding scheme. This opcode byte is followed by from 0 to 3 bytes of operands. Thus, the total length of an instruction is 1 to 4 bytes.
a) two-byte instructions
The 8086 can also fetch and execute certain two-byte instructions in a single clock cycle. These instructions are called jump (JMP), call (CALL), and return (RET).
b) Instructions with longer encodings
Apart from the one-byte and two-byte instructions, there are a number of other instructions whose machine language encodings are 3 or 4 bytes long. However, the fetch and execution of such instructions take more than one clock cycle.
2) Two‐byte instructions
These instructions are 2‐bytes long and have an opcode followed by an operand. The operand can be either an 8‐bit immediate value or a 16‐bit memory address. The opcodes for two‐byte instructions start from 0x01. For example, the following instruction will move the value 0x12 to accumulator.
MVI A, 0x12
The complete instruction set for two ‐ byte instructions is given below. Note that some instructions have different opcodes for register operands and memory operands.
|Opcode (in hex)|Instruction|
|04|MOV E,data ||
05 MOV H,data |
06 MOV L,data |
07 MOV M,data |
08 MOV A,data |
3) Three‐byte instructions:
In some cases, the instruction may be three bytes long. This is necessary when there is not a single register that can hold all of the data required by the instruction. For example, an instruction might require two registers and a memory location. In this case, the first byte would identify which operation to perform, and the next two bytes would identify the operands.
Today, the most common type of CPU is the CISC (Complex Instruction Set Computer). CISC processors have a large number of operations that they can perform; this makes them more flexible and powerful than RISC processors.
However, this increased flexibility means that CISC processors are more difficult to design, build, and program.
In computer engineering, RISC (pronounced /rɪsk/), or reduced instruction set computing, is a type of CPU architecture that generally has fewer and simpler instructions than those in a complex instruction set computing (CISC) design. RISC architectures were evolved in an attempt to improve performance through the simplification of the processor instruction set,
while complex instruction set architectures were designed with a focus on programming efficiency. These two approaches represent a continuous spectrum of designs, not two distinct classes. The precise definition of “RISC” varies somewhat among uses; however, all share some common characteristics such as supporting few addressing modes and directions for data movement; wide use of register files;
simple instructions which require one clock cycle to complete; fixed-width instructions; and separate code and data spaces. The term “RISC” was coined by David Patterson and John Hennessy (co-authors of a well-known textbook on computer architecture) at Berkeley. They wrote:
Reduced Instruction Set Computing (RISC) is a type of microprocessor architecture that utilizes a small, highly optimized set of instructions, rather than a more specialized or complex set of instructions often found in other types of architectures.
The processing of an instruction by the CPU comprises the following steps:
- Fetching the instruction from memory
- Decoding the instruction
- Executing the instruction
- Storing the results back in memory
The first step, fetch, involves the retrieval of an instruction from memory. The instruction is placed into the instruction register (IR). The addresses of the operands (if any) are also fetched and placed into memory address register(s) (MAR).
The instruction set is specific to a class of processors. instructions are stored as binary values, in the form of opcodes, that the CPU reads and interprets. When the CPU decodes an opcode, it uses the operands to determine what actions to take. In some cases, the operand is a memory address or a value that is stored in memory. The operand tells the CPU where to find data or where to store data.
The very first thing that a CPU does when it is turned on is to execute a bootstrap program stored in ROM. The purpose of this program is to initialize the CPU and its associated hardware, and to load and start an operating system.
Once the CPU has finished executing the bootstrap program, it enters an idle loop, waiting for an interrupt. When an interrupt occurs, the CPU fetches and executes the next instruction from memory. If the instruction is a branch or jump instruction, the CPU will fetch and execute instructions from a different memory location. Otherwise, it will simply fetch and execute the next instruction in sequence.
When a program is written in a high-level language, it needs to be translated into machine code that the CPU can understand. This process is called compiling. Once the program has been compiled, it can be stored in memory until it is needed.
Now that you understand the steps taken by the CPU to run a program, you can see how important it is to choose the right instruction set for your needs.
RISC architectures are designed for performance, while CISC architectures are designed for programming efficiency. Choose the architecture that best meets your needs.
Thank you for reading!