Instructions and Instruction Sequencing
Instructions and Instruction Sequencing
Instructions and Instruction Sequencing
The processor control circuits use information in PC to fetch & execute instructions one at a
time in order of increasing address.
This is called straight line sequencing.
Executing an instruction-2 phase procedures.
Branching
Branch-type of instruction loads a new value into program counter.
So processor fetches & executes instruction at this new address called branch target
Conditional branch-causes a branch if a specified condition is satisfied.
E.g. Branch>0 LOOP conditional branch instruction .it executes only if it satisfies
condition.
A straight-line program for adding n numbers
Condition codes
Condition Codes
N (negative)
Z (zero)
V (overflow)
C (carry)
Superscalar processor --can execute more than one instruction per cycle.
Cycle--smallest unit of time in a processor.
The Instruction Set Architecture (ISA) is the part of the processor that is visible to the
programmer or compiler writer. The ISA serves as the boundary between software and hardware.
We will briefly describe the instruction sets found in many of the microprocessors used today.
The ISA of a processor can be described using 5 categories:
The 3 most common types of ISAs are:
1. Stack - The operands are implicitly on top of the stack.
2. Accumulator - One operand is implicitly the accumulator.
3. General Purpose Register (GPR) - All operands are explicitly mentioned, they are either registers or
memory locations.
in all 3 architectures:
Stack
PUSH A
PUSH B
ADD
POP C
Accumulator
LOAD A
ADD B
STORE C
-
GPR
LOAD R1,A
ADD R1,B
STORE R1,C
-
Stack
Advantages: Simple Model of expression evaluation (reverse polish). Short instructions.
Disadvantages: A stack can't be randomly accessed This makes it hard to generate eficient code.
The stack itself is accessed every operation and becomes a bottleneck.
Accumulator
Advantages: Short instructions.
Disadvantages: The accumulator is only temporary storage so memory traffic is the highest for
this approach.
GPR
Advantages: Makes code generation easy. Data can be stored for long periods in registers.
Disadvantages: All operands must be named leading to longer instructions.
Earlier CPUs were of the first 2 types but in the last 15 years all CPUs made are GPR processors.
The 2 major reasons are that registers are faster than memory, the more data that can be kept
internally in the CPU the faster the program wil run. The other reason is that registers are easier
for a compiler to use.
ADDRESSING MODES
The different ways in which location of an operand is specified in an instruction are referred as
addressing modes.
TYPES OF ADDRESSING MODES
Variable-represented by allocating a register or memory location to hold its value.
1. REGISTER MODE
The operand is the contents of processor register; name of register is given in instruction.
E.g. Move Loc, R2.
Processor registers are used as temporary storage locations where data in a register are
accessed using register mode.
2. ABSOLUTE MODE (OR) DIRECT MODE
The operand is in a memory location, the address of this location is given explicitly in
the instruction.
E.g. Integer A, B
Absolute mode is used to access these variables.
3.
IMMEDIATE MODE
Address and data constants-represented in assembly language using immediate mode.
Operand is given explicitly in the instruction.
E.g. Move #200, R0
(#)-value is used as an immediate operand.
Mainly used to specify value of a source operand.
4.
INDIRECT MODE
Memory address of an operand can be determined by instruction.
Address-called Effective Address (EA) of an operand.
EA of an operand contents of a register.
When absolute mode-not available, indirect addressing through registers use to access
global variables.
5.
INDEX MODE
Deals With lists and arrays.
EA-generated by adding constant value to contents of register.
Index registers one of set of general purpose registers in a processor.
E.g. X(Ri)
X-constant value in instruction.
Ri-name of the register involved.
EA=X+[Ri]
Second register is used, index mode-(Ri, Rj).
EA-sum of contents of registers Ri, Rj.
Second register-base register.e.g X(Ri,Rj)
EA=X+[Ri]+[Rj]
Gives more flexibility.
6.
RELATIVE MODE
EA-for index mode is given using program counter.
This mode used to access data operands.
Common use-specify target address in branch instruction.
E.g. Branch>0 Loop.
Program execution got to branch target location identified by name loop if branch
condition is satisfied.
7.
AUTO INCREMENT MODE.
Useful for accessing data items in successive locations in memory.
EA of an operand contents of register specified in instruction.
After accessing operand contents of register is automatically incremented to point to
next item in a list. E.g. (Ri)+
Increment amount 1 for byte specified operands.
2 for 16-bit operands.
4 for 32-bit operands.
8.
AUTODECREMENT MODE.
Contents of register specified in instruction are first automatically decremented & used as
a EA of the operand.
E.g. (Ri)
Minus sign indicate contents to be decremented before being used as EA.
ALU DESIGN
Instructions that involve an arithmetic or logic operation can be executed using similar steps.
They differ from the Load instruction in two ways:
There are either two source registers, or a source register and an immediate source operand.
No access to memory operands is required.
A typical instruction of this type is
Add R3, R4, R5
It requires the following steps:
1. Fetch the instruction and increment the program counter.
2. Decode the instruction and read the contents of source registers R4 and R5.
3. Compute the sum [R4] + [R5].
4. Load the result into the destination register, R3.
The Add instruction does not require access to an operand in the memory, and therefore could be
completed in four steps instead of the five steps needed for the Load instruction.
However, as we will see in the next chapter, it is advantageous to use the same multi-stage
processing hardware for as many instructions as possible. This can be achieved if we arrange for
all instructions to be executed in the same number of steps. To this end, the Add instruction
should be extended to five steps, patterned along the steps of the Load instruction. Since no
access to memory operands is required, we can insert a step in which no action takes place
between steps 3 and 4 above. The Add instruction would then be performed as follows:
1. Fetch the instruction and increment the program counter.
2. Decode the instruction and read registers R4 and R5.
3. Compute the sum [R4] + [R5].
4. No action.
5. Load the result into the destination register, R3.
If the instruction uses an immediate operand, as in
Add R3, R4, #1000 the immediate value is given in the instruction word. Once the instruction is
loaded into the IR, the immediate value is available for use in the addition operation. The same
five-step sequence can be used, with steps 2 and 3 modified as:
2. Decode the instruction and read register R4.
3. Compute the sum [R4] + 1000.
n-bit adder
Carry-in c0 into the LSB position provides a convenient way to perform subtraction.
n-bit subtractor
n-bit adder/subtractor
Sequential multiplication
INTRODUCTION
FUNDAMENTAL CONCEPTS
But, in cases where an instruction occupies more than one word, steps
1 and 2 must be repeated as many times as necessary to fetch the
complete instruction.
Two first steps are ussually referred to as the fetch phase.
Step 3 constitutes the execution phase
But, in cases where an instruction occupies more than one word, steps 1 and
2 must be repeated as many times as necessary to fetch the complete
instruction.
Fetch the contents of a given memory location and load them into a CPU
Register
Store a word of data from a CPU register into a given memory location.
EXECUTING AN INSTRUCTION
Fetch the contents of a given memory location and load them into a
processor register.
REGISTER TRANSFER
The input and output gates for register Ri are controlled by the signals Riin
and Riout, respectively.
Thus, when Riin is set to 1, the data available on the common bus is
loaded into Ri.
While Riout is equal to 0, the bus can be used for transferring data
from other registers.
Let us now consider data transfer between two registers. For example, to
transfer the contents of register R1 to R4, the following actions are needed:
Enable the input gate of register R4 by setting R4in to 1. This loads data
from
ALU gets the two operands from MUX and bus. The result is temporarily
stored in register Z
R1out, Yin
Zout, R3in
CPU transfers the address of the required information word to the memory
address register (MAR). Address of the required word is transferred to the
main memory.
Meanwhile, the CPU uses the control lines of the memory bus to indicate
that a read operation is required.
After issuing this request, the CPU waits until it receives an answer from
the memory, informing it that the requested function has been completed.
This is accomplished through the use of another control signal on the
memory bus, which will be referred to as Memory Function Completed
(MFC).
The memory sets this signal to 1 to indicate that the contents of the
specified location in the memory have been read and are available on the
data lines of the memory bus.
We will assume that as soon as the MFC signal is set to 1, the information
on the data lines is loaded into MDR and is thus available for use inside
the CPU. This completes the memory fetch operation.
MAR [R1]
R2 [MDR]
MDRinE, WMFC
MDRout, R2in
Storing a word in Memory
Then data to be written are loaded into MDR, and a write command is
issued.
MAR [R1]
MDR [R2]
Write
Fetch the first operand (the contents of the memory location pointed to
by R3)
MDRout, IRin
Branch Instructions
MDRout, Irin
Internalprocessor
bus
Controlsignals
PC
Instruction
Address
lines
decoderand
MAR
controllogic
Memory
bus
MDR
Data
lines
IR
Y
R0
Constant4
Select
MUX
Add
ALU
control
lines
Sub
R n 1
ALU
Carryin
XOR
TEMP
Z
Figure7.1.Singlebusorganizationofthedatapathinsideaprocessor.
WFMC