14
Addressing Modes Utility and Subroutines

Lec5 Add Mode Ex

Embed Size (px)

DESCRIPTION

csa

Citation preview

Page 1: Lec5 Add Mode Ex

Addressing Modes Utility and Subroutines

Page 2: Lec5 Add Mode Ex

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 3: Lec5 Add Mode Ex

Utility of using Indirect Addressing

Page 4: Lec5 Add Mode Ex

Using Auto-increment Mode

Page 5: Lec5 Add Mode Ex

Utility of Indexed Addressing

A list of students’ marks

Page 6: Lec5 Add Mode Ex

Find sum of Test1, Test2 and Test 3 scores

Page 7: Lec5 Add Mode Ex

Subroutines

• In a given program, it is often necessary to perform a particular subtask many times on different data values.

• Such a subtask is usually called a subroutine.• Only one copy of the instructions that constitute the

subroutine is placed in the memory,. • Any program that requires the use of the subroutine

simply branches to its starting location – Calling the subroutine.

• After a subroutine has been executed, the calling program must resume execution, continuing immediately after the instruction that called the subroutine – Returning from subroutine.

Page 8: Lec5 Add Mode Ex

Calling and Returning

• Calling– Store the contents of the PC in the link register.

– Branch to the target address specified by the instruction.

• Returning– Branch to the address contained in the link

register

Page 9: Lec5 Add Mode Ex

Calling and Returning

Page 10: Lec5 Add Mode Ex

Nested subroutine call

• One subroutine calls another, which calls another and so on.

• In this case, the return address of the second call is also stored in the link register, destroying its previous contents.

• Solution – The last subroutine returns first.– LIFO: Use Processor stack for storing return addresses.

• SP points the top of stack.

• The Call instruction pushes the contents of the PC onto the processor stack and loads the subroutine address into the PC.

• The Return instruction pops the return address from the processor stack into the PC.

Page 11: Lec5 Add Mode Ex

Nested subroutine calls - example

Page 12: Lec5 Add Mode Ex

Marks addition program using subroutine –passing parameters through registers

Page 13: Lec5 Add Mode Ex

Passing parameters using stack

Page 14: Lec5 Add Mode Ex

Stack contents