Instruction Sets: Addressing Modes and Formats
Instruction Sets: Addressing Modes and Formats
Addressing Modes
and Formats
1
Definition
Instruction set: The operation of the CPU is determined by the instructions it
executes, called machines instructions. The collection of different instructions
that the CPU can executes is refereed to as the instruction set.
The instruction set provides commands to the processor, to tell it what it needs to do
Elements of machine instructions
• Operation code (Op code)
– Do this
• Source Operand reference
– To this
• Destination (Result) Operand reference
– Put the answer here
• Next Instruction Reference
– When you have done that, do this...
2
Instruction Representation
Within the computer, each instruction is
represented by sequence of bits. The
instruction is divided into fields corresponding
to the constituent elements of the instruction.
12 bits specify an address
Three bits specify the operation(opcode)
The mode bit specify the address mode.
4
Instruction Representation
It is worthy to note that it is difficult for even
programmer to deal with binary representation
of the machines instructions. Thus, symbolic
representation of machines codes becomes
necessary.
Opcodes are therefore represented by
abbreviation called mnemonics, that indicate the
operation to be performed.
5
Definition
• Addressing mode: Addressing modes are
concerned with how the CPU accesses the
operands used by its instructions
• Effective address: The address of operand
produced by the application of the rule for
interpreting or modifying the address field of
the instruction before the operand is actually
referenced.
6
Addressing Modes
• Immediate (Implied)
• Direct
• Indirect
• Register
• Register Indirect
• Displacement (Indexed)
• Stack
7
Immediate Addressing
• Operand is part of instruction
• Immediate addressing is indicated by a # symbol
in front of the source operand.
• Operand = address field
• e.g. ADD 5
– Add 5 to contents of accumulator
– 5 is operand
• No memory reference to fetch data
• Fast
• Limited range
8
Immediate Addressing Diagram
Instruction
Opcode Operand
9
Immediate Addressing Sample
1
10
Direct Addressing
• Address field contains address of operand
• That is, the instruction provides the address of the operand in memory
• Effective address (EA) = address field (A)
• In this mode, the address field contain the Effective Address of operand
• e.g. ADD A
– Add contents of cell A to accumulator
– Look in memory at address A for operand
• Direct addressing requires two memory accesses. The first is to access the
instruction and the second is to access the actual operand.
11
Direct Addressing Diagram
Instruction
Opcode Address A
Memory
Operand
12
Direct Addressing Sample
13
Direct Addressing Result
M em ory
M O V E .B 2 0 ,D 0
20 42
42 D0
16
Indirect Addressing Diagram
Instruction
Opcode Address A
Memory
Pointer to operand
Operand
17
Indirect Addressing Sample
1000 42 D0
Addressing Modes 18
Indirect Addressing Result
M em ory
A0 M O V E .B (A 0 ),D 0
1000
1000 57 D0
Addressing Modes 20
Register Addressing (1)
• Operand is held in register named in address filed
• The operands are in registers that reside within the CPU
• In this mode, instruction specifies a register in CPU, which
contain the operand.
• It is like Direct, but address field refers to a register instead of
memory location.
• EA = R
• Limited number of registers
• Very small address field needed
– Shorter instructions
– Faster instruction fetch
21
Register Addressing (2)
• No memory access
• Very fast execution
• Very limited address space
• Multiple registers helps performance
– Requires good assembly programming or compiler
writing
• c.f. Direct addressing
22
Register Addressing Diagram
Instruction
Opcode Register Address R
Registers
Operand
23
Register Indirect Addressing
• In these address mode, the selected register contain
the address of operand rather than the operand itself
• EA = (R)
• Control fetches instruction from memory and then
uses its address to access Register and looks in
Register(R) for effective address of operand in
memory.
• That is, operand is in memory cell pointed to by
contents of register R
• Large address space (2n)
• One fewer memory access than indirect addressing
24
Register Indirect Addressing Diagram
Instruction
Opcode Register Address R
Memory
Registers
25
Displacement Addressing
• This addressing mode combines the capabilities of direct
addressing and register indirect addressing
• EA = A + (R)
• Address field hold two values
– A = base value
– R = register that holds displacement
• Value is contained in addressing field A, which is used directly
and the value in other address field is R, which refers to a
register whose contents are to be added to produce effective
address.
• This are of three types
– Relative addressing mode
– Base register addressing mode
26
– Index addressing mode
Displacement Addressing
• EA = A + (R)
• Address field hold two values
– A = base value
– R = register that holds displacement
• Value is contained in addressing field A, which is used directly
and the value in other address field is R, which refers to a
register whose contents are to be added to produce effective
address.
• This are of three types
– Relative addressing mode
– Base register addressing mode
– Index addressing mode
27
Displacement Addressing Diagram
Instruction
Opcode Register R Address A
Memory
Registers
28
Relative Addressing
• In Relative Addressing Mode, the address of the
operand is obtained by adding a constant to the
content of a register, called the Program Counter (PC)
• R = Program counter, PC
• EA = A + (PC)
• That is, the address field of the instruction is added to
implicitly reference register Program Counter to
obtain effective address
• Example, LOAD X(PC), R, implies loads register R with
the contents of the memory location whose address
is the sum of the contents of the program counter
(PC) and the value X
29
Relative Addressing Diagram
.
30
Relative Addressing Example
• Assume that PC contains the value 825 and the address part of instruction
contain 24, then the instruction at location 825 is read from memory
during fetch phase and the Program Counter is then incremented by one
to 826. What is the effective address?
33
Indexed Addressing
• In this addressing mode, the address of the operand
is obtained by adding a constant to the content of a
register, called the index register
• Example, LOAD X(Rind), R. This instruction loads
register R with the contents of the memory location
whose address is the sum of the contents of register
Rind and the value X.
• EA = A + R
• Good for accessing arrays
– EA = A + R
– R++
34
Indexed Addressing Diagram
35
Sample Indexed Addressing
36
Index Addressing operation
• .
• This instruction loads register Ri with the operand whose
address is the content of register Rauto. After loading the
operand into register Ri, the content of register Rauto is
incremented, pointing for example to the next item in a list of
items.
37
Combinations
• Postindex
• EA = (A) + (R)
• Preindex
• EA = (A+(R))
38
PostIndex; Before Execution
After Execution
39
PreIndex Before Execution
After Execution
40
Stack Addressing
• Operand is (implicitly) on top of stack
• e.g.
– ADD Pop top two items from stack
and add
41
Instruction Format
• The operation of CPU is determined by the instructions it
executes (machine or computer instructions)
• CPU’s instruction set – the collection of different instructions
that CPU can execute
• Each instruction must contain the information required by CPU
for execution :-
1. Operation code (opcode) -- specifies the operation to be performed (eg:
ADD, I/O) do this
2. Source operand reference -- the operation may involve one or more source
operands (input for the operation) to this
3. Result operand reference -- the operation may produce a result put the
answer here
4. Next instruction reference -- to tell the CPU where to fetch the next
instruction after the execution of this instruction is complete do this
when you have done that
opcode
2 3 4
1 42
• Operands (source & result) can be in one of the 3 areas:-
– Main or Virtual Memory
– CPU register
– I/O device
• It is not efficient to put all the information required by CPU in a machine
instruction
• Each instruction is represented by sequence of bits & is divided into 2 fields;
opcode & address
opcode address
opcode address for Operand 1 address for Operand 2 address for Result address for Next instruction
44
Allocation of Bits
• Number of addressing modes
• Number of operands
• Register versus memory
• Number of register sets
• Address range
• Address granularity
45
Instruction-3-address
opcode address for Result address for Operand 1 address for Operand 2
46
Example of 3 Address Instruction
3 Address instruction
Instruction: ADD R1,R2,R3
Operation: R1 R2 + R3
Example:
Evaluate X = (A+ B)* (C+D)
ADD R1 A,B
ADD R2 C,D
MULTI X,R1,R2
47
Instruction-2-address
48
Example of 2 Address Instruction
2 Address instruction
Instruction: ADD R1,R2
Operation: R1 R1 + R2
Example:
Evaluate X = (A+ B)* (C+D)
Move R1, A
ADD R1, B
Move R1, C
ADD R1, D
MULTI R1,R2
Move X, R1
49
Instruction-1-address
• Example :
LOAD A
ADD B AC = AC + B
or
SUB B AC = AC - B
– Operand 1 & Result in AC (accumulator), a register
– SUB B B subtracts from AC, the result stored in AC
– Address for Next instruction? program counter (PC)
• Short instruction (requires less bit) but need more instructions
for arithmetic problem
50
Types of Address Instruction
1 Address instruction
One address can be a register name or memory
address.
It uses AC register for all data manipulation.
Example:
Instruction: ADD X
Operation: AC AC + M[X ]
Assignment: Evaluate X = (A+ B)* (C+D), using one
address instruction.
51
Zero Address instruction
Stack is used. Arithmetic operation pops two operands
from the stack and pushes the results.
Also refers to as stack organization.
Example, Evaluate X = (A+ B)* (C+D). Push A
Push A Push B
Push B
ADD
ADD
Push C Push C
Push D Push D
MULTI ADD
POP X MULTI
POP X
Store
52