Understanding Hardware: CPU - Part 1
The most important item in the computing hardware is the CPU. CPU is also
refered as Microprocessor. The main job of CPU is to fetch (i.e. read)
instructions one by one from memory, decode them and execute them. This
goes on forever.
Note that here, memory means semiconductor memory like RAM or ROM, but not
hard disk. One should not call hard disk as memory, it is only a storage
device. Other storage devices are CD-ROM, DVD, USB Pen drivers, SD cards etc.
CPUs can not fetch and execute instructions directly from the storage devices.
Program Counter (PC)
Look at the CPU shown above. The Program Counter (PC) in the CPU is a special
register. It holds the address of the next instruction, which it is going to
fetch and execute. This address will be the address of a memory location,
from which it fetches the instruction. This memory can be either RAM or ROM.
In some CPUs, program counter is also refered as Instruction Pointer (IP).
Once power is applied to the CPU, it goes through a reset process and all
its registers will be initialized to some default values. In this reset
process, the PC will get initialized to zero. So CPU fetches its first
instruction from the memory address zero. After fetching the instruction,
PC will automatically incremented to the address of next instruction.
Instruction Decoder and Intruction format
The instruction fetched from the memory is decoded by the instruction
decoder. This decoder is shown in the diagram. Note that each instruction is
a number occupying one or more bytes of memory. We should view these
instructions as 8 bit or 16 bit (or any multiple of 8 bit) numbers.
Some bits in this number specify what operation to perform. Other bits
speicify on which this operation to perform. For example if operation
bits specify, ADD operation. Other bits specify the register numbers.
The bits in the instruction that specify operation is called opcode bits.
Other bits which specify the registers is called operand bits. So
instruction bits consist of opcode bits and operand bits.
The decoder unit inside the CPU, is responsible for decoding the opcode
bits and operand bits. From decoding, it finds out the operation to perform
and operands to be used. The execution unit takes this decoded information
and performs the operation with the help Arithmetic and Logic Unit (ALU).
Most of the time, operands specified in the instructions are registers.
So operations are performed on these register contents. Results are stored
back in the registers. But some times, operand may be immediate value
present in the operand bits. Such operands are called immediate operands.
In the CPU shown, it has got only four registers named R0 to R3. So to
specify register operand, only two bits are sufficient. Following is
the example of single byte (8 bit) instrution.
| Opcode bits | Operand Bits |
Sample Opcode values:
Following is the instruction code for : SUB R1, R2
| 0011 | 01 10 |
So total instruction code or number is 0x36(hex) or 54(decimal).
The CPU also have another special register called Program Status Register or
simply Status Register (SR). The bits in this register are will get updated
based on the last arithmetic and logic operation performed. For example
when SUB(subtract) operation is performed, if result is zero, the zero bit
in the status register will get set. If result is negative, the minus bit
will get set.
General Purpose registers
The registers R0 to R3 are called, general purpose registers. These registers
acts as operands for the operations. Most of the modern CPUs perform operations
only on the registers. There are instructions called Load and Store, which
will move memory contents to registes and vice versa. So typical programs
uses following kinds of instructions.
- Instructions to move data from memory to registers
- Instructions to perform arithmetic and logic operations on the registers
- Instructions to move registers to memory.
8 Bit / 16 Bit / 32 Bit / 64 Bit CPU
We classify CPUs as 8 bit or 16 bit and so on. What desides the number of
bits of a CPU? The arithmetic and logic unit (ALU) desides this. If ALU is
capable of taking only two 8 bits numbers at a time and producing the output,
then it is 8 bit CPU only. In 32 bit CPU, the ALU is capable of performing
operation on two 32 bit numbers at a time. Note that inputs to ALU is
coming from registers and output of ALU also goes back to register. So the
ALU size and register size will be same. So you can say a processor is
64 bit, if its general purpose registers are 64 bit width. So register width
gives the CPU's bit capability.
CPU to perform fetch, decode and execution operations continuously, it must
require some timing reference. The clock input to the CPU will provide such
timing source. CPU on receiving first clock, it fetches the instruction
from memory. On the second clock, it decodes the instruction. On receiving
third clock it executes the instruction. With the fourth clock, it again
fetches the next instruction and it goes on forever.
Understand that the clock speed decides the execution speed of the CPU.
CPUs will support clocks of certain frequency range.
Answer the following questions to check the understanding of the subject
1. What is the purpose of Program Counter (PC) register in a CPU?
2. What is the use of Program Status Register in a CPU?
3. Explain the format of instruction, by explaining opcode bits and operand
bits present in the instruction.
4. Write the instruction number in hex for the following instructions:
5. How can you tell, a CPU is a 8 bit or 16 or 32 or 64 bit CPU?
6. What is the role of clock in the working of a CPU?