Lec3 instructions branch carl hamcher

35
Instruction Execution: Straight-line sequencing and branching

Transcript of Lec3 instructions branch carl hamcher

Page 1: Lec3 instructions branch carl hamcher

Instruction Execution: Straight-line sequencing and branching

Page 2: Lec3 instructions branch carl hamcher

Instruction Execution and Straight-Line Sequencing

R0,C

B,R0

A,R0

Movei + 8

Begin execution here Movei

ContentsAddress

C

B

A

the programData for

segmentprogram3-instruction

Addi + 4

A program for C +

Assumptions:- One memory operand per instruction- 32-bit word length- Memory is byte addressable- Full memory address can be directly specified in a single-word instruction

Two-phase procedure-Instruction fetch-Instruction execute

Page 3: Lec3 instructions branch carl hamcher

Branching

NUMn

NUM2

NUM1

R0,SUM

NUMn ,R0

NUM3,R0

NUM2,R0

NUM1,R0

A straight-line program for adding n numbers.

Add

Add

Move

SUM

i

Move

Add

i 4n+

i 4n 4-+

i 8+

i 4+

•••

•••

•••

Page 4: Lec3 instructions branch carl hamcher

BranchingN,R1Move

NUMn

NUM2

NUM1

R0,SUM

R1

"Next" number to R0

Using a loop to add n numbers.

LOOP

Decrement

Move

LOOP

loopProgram

Determine address of"Next" number and add

N

SUM

n

R0Clear

Branch>0

•••

•••

Branch target

Conditional branch

Page 5: Lec3 instructions branch carl hamcher

Condition Codes

• Condition code flags• Condition code register / status register• N (negative)• Z (zero)• V (overflow)• C (carry)• Different instructions affect different flags

Page 6: Lec3 instructions branch carl hamcher

Conditional Branch Instructions

• Example:A: 1 1 1 1 0 0 0 0B: 0 0 0 1 0 1 0 0

A: 1 1 1 1 0 0 0 0

+(−B): 1 1 1 0 1 1 0 0

1 1 0 1 1 1 0 0

C = 1

N = 1

V = 0

Z = 0

Page 7: Lec3 instructions branch carl hamcher

Status Bits

ALU

V Z N C

Zero Check

Cn

Cn-1

Fn-1

A B

F

Page 8: Lec3 instructions branch carl hamcher

Addressing Modes

Page 9: Lec3 instructions branch carl hamcher

Generating Memory Addresses

• How to specify the address of branch target?• Can we give the memory operand address

directly in a single Add instruction in the loop?• Use a register to hold the address of NUM1;

then increment by 4 on each pass through the loop.

Page 10: Lec3 instructions branch carl hamcher

Addressing Modes

• Immediate• Direct• Indirect• Register• Register Indirect• Displacement (Indexed) • Implicit

Page 11: Lec3 instructions branch carl hamcher

Immediate Addressing

• Operand is part of instruction• 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

Page 12: Lec3 instructions branch carl hamcher

Immediate Addressing Diagram

OperandOpcode

Instruction

Page 13: Lec3 instructions branch carl hamcher

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– 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

Page 14: Lec3 instructions branch carl hamcher

Direct Addressing Diagram

Address AOpcode

Instruction

Memory

Operand

Page 15: Lec3 instructions branch carl hamcher

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

Page 16: Lec3 instructions branch carl hamcher

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 17: Lec3 instructions branch carl hamcher

Indirect Addressing Diagram

Address AOpcode

Instruction

Memory

Operand

Pointer to operand

Page 18: Lec3 instructions branch carl hamcher

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 19: Lec3 instructions branch carl hamcher

Register Addressing (2)

• No memory access• Very fast execution• Very limited address space• Multiple registers helps performance

– Requires good assembly programming or compiler writing

– N.B. C programming • register int a;

• c.f. Direct addressing

Page 20: Lec3 instructions branch carl hamcher

Register Addressing Diagram

Register Address ROpcode

Instruction

Registers

Operand

Page 21: Lec3 instructions branch carl hamcher

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 22: Lec3 instructions branch carl hamcher

Register Indirect Addressing Diagram

Register Address ROpcode

Instruction

Memory

OperandPointer to Operand

Registers

Page 23: Lec3 instructions branch carl hamcher

Displacement Addressing

• EA = A + (R)• Address field hold two values

– A = base value– R = register that holds displacement– or vice versa

Page 24: Lec3 instructions branch carl hamcher

Displacement Addressing Diagram

Register ROpcode

Instruction

Memory

OperandPointer to Operand

Registers

Address A

+

Page 25: Lec3 instructions branch carl hamcher

Relative Addressing

• A version of displacement addressing• R = Program counter, PC• EA = A + (PC)• i.e. get operand from A cells from current

location pointed to by PC• c.f locality of reference & cache usage

Page 26: Lec3 instructions branch carl hamcher

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 27: Lec3 instructions branch carl hamcher

Indexed Addressing

• A = base• R = displacement• EA = A + (R)• Good for accessing arrays

– EA = A + (R)– R++

Page 28: Lec3 instructions branch carl hamcher

Combinations

• Postindex• EA = (A) + (R)

• Preindex• EA = (A+(R))

• (Draw the diagrams)

Page 29: Lec3 instructions branch carl hamcher

Implicit Addressing

• Operand is (implicitly) on top of stack• e.g.

– ADD Pop top two items from stackand add

Page 30: Lec3 instructions branch carl hamcher

Addressing Modes• The different

ways in which the location of an operand is specified in an instruction are referred to as addressing modes.

Name Assembler syntax Addressingfunction

Immediate #Value Operand=ValueRegister R i EA= RiAbsolute(Direct) LOC EA= LOCIndirect (Ri ) EA= [Ri]

(LOC) EA= [LOC]Index X(Ri) EA= [Ri]+ XBasewithindex (Ri ,Rj ) EA= [Ri]+ [Rj]Basewithindex X(Ri,Rj ) EA= [Ri]+ [Rj] + Xandoffset

Relative X(PC) EA= [PC] + XAutoincrement (Ri)+ EA= [Ri] ;

Increment RiAutodecrement (Ri ) Decrement R i ;

EA = [Ri]

Page 31: Lec3 instructions branch carl hamcher

BranchingN,R1Move

NUMn

NUM2

NUM1

R0,SUM

R1

"Next" number to R0

Using a loop to add n numbers.

LOOP

Decrement

Move

LOOP

loopProgram

Determine address of"Next" number and add

N

SUM

n

R0Clear

Branch>0

•••

•••

Branch target

Conditional branch

Page 32: Lec3 instructions branch carl hamcher

Utility of using Indirect Addressing

Page 33: Lec3 instructions branch carl hamcher

Using Auto-increment Mode

Page 34: Lec3 instructions branch carl hamcher

Utility of Indexed Addressing

A list of students’ marks

Page 35: Lec3 instructions branch carl hamcher

Find sum of Test1, Test2 and Test 3 scores