computer architecture 計算機結構 1 chapter 11 instruction sets: addressing modes and formats...

38
Computer Architecture 計計計計計 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 計計計 , Ph.D. [email protected] 計計計 7F 計計計 計計計計 : 計計計 13:25—16:10

Upload: jonah-palmer

Post on 17-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

1

Chapter 11Instruction Sets:

Addressing Modesand Formats

Ren-Jie Wang 王仁傑 , Ph.D. [email protected]

資訊館 7F 辦公室上課時間 : 星期三 13:25—16:10

Page 2: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

2

Addressing Modes 定址模式 Immediate Addressing 立即定址 Direct Addressing 直接定址 Indirect Addressing 間接定址 Register Addressing 暫存器定址 Register Indirect Addressing 暫存間接器定址 Displacement Addressing 位移定址

Relative Addressing 相對定址 Base-Register Addressing 基底暫存器定址 Indexed Addressing 索引定址

Stack Addressing 堆疊定址

Page 3: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

3

Immediate Addressing

Operand is part of instruction

Operand = address field

e.g. ADD 5 Add 5 to contents of accumulator ( AC←AC + 5 )

5 is operand

No memory reference to fetch data

Fast

Limited range ( why? )

Page 4: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

4

Immediate Addressing Diagram

OperandOpcode

Instruction

運算碼( 指令 )

運算元( 資料 )

Page 5: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

5

Direct Addressing

Address field contains address of operand

Effective address (有效位址) EA = address field A

e.g. ADD A Add contents of cell A to accumulator ( AC←AC + A )

Look in memory at address A for operand

Single memory reference to access data

No additional calculations to work out effective

address

Limited address space ( why? )

Page 6: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

6

Direct Addressing Diagram

Address AOpcode

Instruction

Memory

Operand

AADD

Page 7: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

7

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

(A) : Content of location A or register A

Page 8: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

8

Indirect Addressing (2)

Large address space

2N where N = word length

May be nested, multilevel, cascaded e.g. EA = (((A)))

Draw the diagram yourself

Multiple memory accesses to find operand

Hence slower

Page 9: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

9

Indirect Addressing Diagram

Address AOpcode

Instruction

Memory

Operand

Pointer to operand

(A)ADD

Page 10: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

10

Indirect Addressing Diagram

Address AOpcode

Instruction

Memory

Operand

Pointer to operand

((A))ADD

Pointer to operand

Page 11: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

11

Register Addressing (1)

Operand is held in register named in

address filed

EA = R

Limited number of registers

Very small address field needed Shorter instructions

Faster instruction fetch

Page 12: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

12

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 programming register int a;

c.f. Direct addressing

Page 13: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

13

Register Addressing Diagram

Register Address ROpcode

Instruction

Registers

Operand

Page 14: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

14

Register Indirect Addressing

C.f. indirect addressing

EA = (R)

Operand is in memory cell pointed to by

contents of register R

Large address space (2N)

One fewer memory access than indirect

addressing

Page 15: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

15

Register Indirect Addressing Diagram

Register Address ROpcode

Instruction

Memory

OperandPointer to Operand

Registers

(R)ADD

Page 16: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

16

Displacement Addressing

EA = A + (R)

Address field hold two values A = base value 基底 R = register that holds displacement 位移 or vice versa 反之亦然

Page 17: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

17

Displacement Addressing Diagram

Register ROpcode

Instruction

Memory

OperandPointer to Operand

Registers

Address A

+

Page 18: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

18

Relative Addressing

A version of displacement addressing

R = Program counter, PC

EA = A + (PC) PC-relative

i.e. get operand from A cells from current

location pointed to by PC

c.f locality of reference & cache usage

Page 19: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

19

Relative Addressing

Address AOpcode

Instruction

Memory

Operand+

PC

Pointer to Operand

Page 20: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

20

Base-Register Addressing

A holds displacement

R holds pointer to base address

R may be explicit or implicit 外顯或隱含 e.g. segment registers in 80x86

Page 21: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

21

Indexed Addressing

A = base

R = displacement

EA = A + (R)

Good for accessing arrays EA = A + (R)

(R) = (R) +1

Page 22: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

22

Combinations

Pre-index 前索引定址模式 ( 先更新索引 , 再存取資料 ) 基底暫存器 A 的值與偏移量先做加減運算當索引位址 , 來存取資料 然後基底暫存器的值再更新成新的位址 EA = (A+(R))

ARM 指令 : LDR R0, [R1, #4]! ;R0:=[R1+4],

R1:=R1+4

Post-index 後索引定址模式 ( 先存取資料 , 後更新索引 ) 基底暫存器的值會先用來當所要存取的記憶體位址 存取資料後,基底暫存器的值再更新成新的位址 EA = (A) + (R)

ARM 指令 : LDR R0, [R1], #4 ;R0:=[R1], R1:=R1+4

Page 23: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

23

Pre-index: EA = (A+(R))

Register ROpcode

Instruction

Memory

Operand

Displacement

Registers

Address A

+

Base

Pointer to Operand

Page 24: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

24

Register ROpcode

Instruction

Memory

OperandDisplacement

Registers

Address A

+

Post-index: EA = (A) + (R)

Base

Page 25: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

25

Stack Addressing

Operand is (implicitly) on top of stack

e.g. ADD Pop top two items from stack

and add

Textbook p. 398, problems #11.13

Page 26: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

26

Page 27: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

27

IA-32 (Pentium 4) RegistersIA-32 (Pentium 4) Registershttp://www.intel.com/design/processor/manuals/253665.pdf

Streaming SIMD Extensions MMX: SIMD instruction set

Page 28: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

28

purposes in different

Page 29: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

29

Page 30: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

30

Pentium Addressing Modes

Virtual or effective address is offset into segment Starting address plus offset gives linear address

This goes through page translation if paging enabled

12 addressing modes available Immediate

Register operand

Displacement

Base

Base with displacement

Scaled index with displacement

Base with index and displacement

Base scaled index with displacement

Relative

Page 31: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

31

Page 32: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

32

Page 33: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

33

Page 34: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

34

Pentium Addressing Mode Calculation

Page 35: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

35

Page 36: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

39

Pentium Instruction Format

Page 37: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

40

In-class exercise

Given the following memory values and a one-address machine with an accumulator, what values do the following instructions load into the accumulator? ( Textbook p. 397, problems #11.1 ) Word 20 contains 40 Word 30 contains 50 Word 40 contains 60 Word 50 contains 70

a. LOAD IMMEDIATE 20

b. LOAD DIRECT 20

c. LOAD INDIRECT 20

d. LOAD IMMEDIATE 30

e. LOAD DIRECT 30

f. LOAD INDIRECT 30

20 40

30 50

40 60

50 70

60

70

Page 38: Computer Architecture 計算機結構 1 Chapter 11 Instruction Sets: Addressing Modes and Formats Ren-Jie Wang 王仁傑, Ph.D. rjwang@ntit.edu.tw 資訊館 7F 辦公室 上課時間

Computer Architecture 計算機結構

41

In-class exercise

Assume a stack-oriented processor that includes the stack operations PUSH and POP. Arithmetic operations automatically involve the top one or two stack elements. Begin with an empty stack. What stack elements remain after the following instructions are executed? ( Textbook p. 398, problems #11.13 ) PUSH 4 PUSH 7 PUSH 8 ADD PUSH 10 SUB MUL