CA_lecture_11
CA_lecture_11
Lecture 11.
. Instruction Sets:
Addressing modes.
Instructors
3
Elements of a Machine Instruction
4
Instruction Cycle State Diagram
5
Instruction Representation
6
Addressing Modes
• Immediate
• Direct
• Indirect
• Register
• Register Indirect
• Displacement (Indexed)
• Stack
7
Immediate Addressing
Instruction
Opcode Operand
8
Direct Addressing
9
Indirect Addressing (1)
• Memory cell pointed to by address field contains the address of (pointer to) the operand
• EA = (A)
• Look in A, find address (A) and look there for operand
• e.g. ADD (A)
• Add contents of cell pointed to by contents of A to accumulator
10
Indirect Addressing (2)
Instruction
Opcode Address A
Memory
• Large address space Pointer to operand
• 2n where n = word length
• May be nested, multilevel, cascaded
• e.g. EA = (((A)))
• Draw the diagram yourself Operand
• Multiple memory accesses to find operand
• Hence slower
11
Register Addressing
12
Register Addressing Diagram
Instruction
Opcode Register Address R
Registers
Operand
13
Register Indirect Addressing
14
Register Indirect Addressing Diagram
Instruction
Opcode Register Address R
Memory
Registers
15
Displacement Addressing
• EA = A + (R)
• Address field hold two values
• A = base value
• R = register that holds displacement
• or vice versa
16
Relative Addressing
17
Base-Register Addressing
• A holds displacement
• R holds pointer to base address
• R may be explicit or implicit
• e.g. segment registers in 80x86
18
Indexed Addressing
• A = base
• R = displacement
• EA = A + R
• Good for accessing arrays
• EA = A + R
• R++
19
Stack Addressing
• Operand is (implicitly) on
top of stack
• e.g.
• ADD Pop top two
items from stack and
add
20
x86 Addressing Modes
21
ARM Addressing Modes
Load/Store
• Only instructions that reference memory
• Indirectly through base register plus offset
• Offset
• Offset added to or subtracted from base register contents to
form the memory address
• Preindex
• Memory address is formed as for offset addressing
• Memory address also written back to base register
• So base register value incremented or decremented by offset
value
• Postindex
• Memory address is base register value
• Offset added or subtracted
Result written back to base register
• Base register acts as index register for preindex and
postindex addressing
• Offset either immediate value in instruction or another
register
• If register scaled register addressing available
• Offset register value scaled by shift operator
• Instruction specifies shift size
22
Instruction Formats
23
Instruction Length
24
Allocation of Bits
25
PDP-8 Instruction Format
26
PDP-10 Instruction Format
27
PDP-11 Instruction Format
28
x86 Instruction Format
29
ARM Instruction Formats
31
Expanding Thumb ADD Instruction to ARM
Equivalent
32
Assembler
33
Improvements
34
Program in:
Binary Hexadecimal
Address Contents Address Contents
35
Symbolic Addresses
36
Programs
201 DAT 2
I DATA 2
202 DAT 3
J DATA 3
203 DAT 4
K DATA 4
204 DAT 0 N DATA 0
37
Issues in design of ISAs
• Characterization of application programs
• What are typical programs like
• What mix of instructions (ALU, Branch, ..)
• Size of instruction word
• Length of program
• Number of instructions
• Number of bytes
• Number of instructions needed to code the same program may
be different depending on the ISA
• Clock period
• Needs to be long enough to do one cycle of instruction
• Single cycle instruction or multi-cycle instruction
• Multi-cycle instructions: (we are skipping this)
• Hardwired control vs micro-programmed control
• Pipelined instructions
38
Number of operands
• Another way to classify instruction sets is according to the number of operands that each data
manipulation instruction can have.
• Our example instruction set had three-address instructions, because each one had up to three
operands—two sources and one destination.
operation operands
Register transfer instruction:
ADD R0, R1, R2
R0 R1 + R2
destination sources
• This provides the most flexibility, but it’s also possible to have fewer than three operands.
39
Two-address instructions
• In a two-address instruction, the first operand serves as both the destination and one of the source registers.
operation operands
Register transfer instruction:
ADD R0, R1
destination source 2 R0 R0 + R1
and source 1
40
One-address instructions
• Some computers, like this old Apple II, have one-address instructions.
• The CPU has a special register called an accumulator, which implicitly serves as the destination
and one of the sources.
41
The ultimate: zero addresses
• If the destination and sources are all implicit, then you don’t have to specify any
operands at all!
• For the ALU instructions
• This is possible with processors that use a stack architecture.
• HP calculators and their “reverse Polish notation” use a stack.
• The Java Virtual Machine is also stack-based.
• How can you do calculations with a stack?
• Operands are pushed onto a stack. The most recently pushed element is at the
“top” of the stack (TOS).
• Operations use the topmost stack elements as their operands. Those values are
then replaced with the operation’s result.
42
Stack architecture example
• From left to right, here are three stack instructions, and what the stack looks like after each example
instruction is executed.
(Top)
PUSH R1 PUSH R2 ADD
R1 R2 R1 + R2
(Bottom)
… stuff 1 … R1 … stuff 1 …
… stuff 2 … … stuff 1 … … stuff 2 …
… stuff 2 …
TOS R1 + R2
43
Data movement instructions
X = (A + B)(C + D)
44
Register-to-register architectures
• Our programs so far assume a register-to-register, or load/store, architecture, which matches our
datapath from last week nicely.
• Operands in data manipulation instructions must be registers.
• Other instructions are needed to move data between memory and the register file.
• With a register-to-register, three-address instruction set, we might translate X = (A + B)(C + D)
into:
LD R1, A R1 M[A] // Use direct addressing
LD R2, B R2 M[B]
ADD R3, R1, R2 R3 R1 + R2 // R3 = M[A] + M[B]
LD R1, C R1 M[C]
LD R2, D R2 M[D]
ADD R1, R1, R2 R1 R1 + R2 // R1 = M[C] + M[D]
45
Memory-to-memory architectures
• In memory-to-memory architectures, all data manipulation instructions use memory addresses
as operands.
• With a memory-to-memory, three-address instruction set, we might translate X = (A + B)(C + D)
into simply:
ADD X, A, B M[X] M[A] + M[B]
ADD T, C, D M[T] M[C] + M[D] // T is temporary
storage
MUL X, X, T M[X] M[X] * M[T]
46
Register-to-memory architectures
• Finally, register-to-memory architectures let the data manipulation instructions access both
registers and memory.
• With two-address instructions, we might do the following:
47
Size and speed
• There are lots of tradeoffs in deciding how many and what kind of operands and addressing
modes to support in a processor.
• These decisions can affect the size of machine language programs.
• Memory addresses are long compared to register file addresses, so instructions with
memory-based operands are typically longer than those with register operands.
• Permitting more operands also leads to longer instructions.
• There is also an impact on the speed of the program.
• Memory accesses are much slower than register accesses.
• Longer programs require more memory accesses, just for loading the instructions!
48
Summary
49