0% found this document useful (0 votes)
472 views15 pages

Stack and SUBROUTINES Bindu Agarwalla

The document discusses stack and subroutines. It describes how a stack works using a diagram showing push and pop operations. It explains that the stack pointer SP always points to the top of the stack. It also discusses subroutine linkage using a link register, and describes how stacks can support nested subroutine calls. Pushing involves decrementing SP then storing the item, while popping involves loading the item then incrementing SP. Subroutines are called using the link register to store the return address, and return by branching to the saved address. Stacks allow nested calls by saving return addresses in a last-in, first-out order.

Uploaded by

Nobody
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
472 views15 pages

Stack and SUBROUTINES Bindu Agarwalla

The document discusses stack and subroutines. It describes how a stack works using a diagram showing push and pop operations. It explains that the stack pointer SP always points to the top of the stack. It also discusses subroutine linkage using a link register, and describes how stacks can support nested subroutine calls. Pushing involves decrementing SP then storing the item, while popping involves loading the item then incrementing SP. Subroutines are called using the link register to store the return address, and return by branching to the saved address. Stacks allow nested calls by saving return addresses in a last-in, first-out order.

Uploaded by

Nobody
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Stack and SUBROUTINES

Bindu Agarwalla
STACK

0
.

SP -12 Current top element


23
88
Stack

BOTTOM 56 Bottom element


.

2k -1
STACK [Assumption: The machine is byte addressable
and each element in the stack occupies 4Bytes]
Push Operation
Substract #4, SP .
Move NEWITEM, (SP) 0

19 Current top element


SP
SP always points to the top
element, so to push a -12
newitem, first, it has to be 23
updated to the location,
where a new element can be 88
pushed. As stack grows
Stack
downward in the memory,
so first SP is decremented,
then the NEWITEM is
pushed. BOTTOM 56 Bottom element
Note: This rule is applicable .
for pushing any element If stack stack starts in
onto the stack. memory from 2000, then the
initial value of SP should be
NEWITEM 2k -1 2004, then only the 1st
19 element will be pushed at
correct location.
STACK
Pop Operation

Move (SP), ITEM 0


Add #4, SP .
SP always points to the top
element, so to pop an -12
SP Current top element
element, first, it has to be
popped off, then the SP need 23
to be updated to the next top
88
element. As stack grows
Stack
downward in the memory,
so SP is inecremented, after
the top item is popped off.
BOTTOM 56 Bottom element
.

ITEM
2k -1
-28
STACK[Assumption: the stack is from 2000 to 1500]
SafePush Operation
In a full stack, push operation should not be
SAFEPUSH Compare #1500, SP done. So, if the value of SP is 1500 or less than
1500, that means already the stack is full. Hence,
Branch <= 0 FULLERROR compare operation gives either 0 or a negative
value after the operation. If SP is 1500, then the
Move NEWITEM, -(SP) next push operation will be done at 1496, which
is not the part of stack.
SafePop Operation

SAFEPOP Compare #2000, SP


From a empty stack, no element should be
Branch > 0 EMPTYERROR popped out. So, if the value of SP is greater than
2000, that means already the stack is empty.
Move (SP)+, ITEM Hence, compare operation gives a positive value
after the operation.
SUBROUTINES
The way in which a computer makes it possible to call and return from
subroutines is referred to as subroutine linkage method.
Linkage using Link Register

On Call

1. Store the contents of the PC in the link register.

2. Branch to the target address specified by the instruction

On returning from a subroutine


Branch to the address contained in the link register
Subroutine Linkage using link register

1000 CALL Sum 2000 First Instruction


1004 Next Instruction

Return
2000

PC 1004

Link 1004

Call Return

Limitation??
No support for Nesting of Functions.
Solution
Using Stack: To support nesting of functions.
Stack as Subroutine Linkage
Method
Calling program

1000 Move N, R1

1004 Move #Num1, R2


1008 Call LISTADD

1012 Move R0, SUM


.
. 1012
.
14
Subroutine
LISTADD Clear R0
LOOP Add (R2)+, R0

Decrement R1
Branch > 0 LOOP
Return
Numerical
The content of the top of memory stack is 2452.The content of SP is
1258.A two byte call subroutine instruction is located in memory
address 1456 followed by address field of 5490 at location 1457.What
are the content of PC , SP and top of stack;
1.Before call instruction execution
2.After call instruction execution
3.After return from subroutine
Numerical
How many times a subroutine should be called so that the stack becomes full, Assume
that the stack address space ranges from 2000 to 1600 and each stack word consumes 4
bytes and machine is byte addressable.[Note: No parameter, return value, registers,
local variables are stored in the stack due to subroutine call]
Numerical
Given the following program fragment
Main Program First Subroutine SUB1 Second Subroutine SUB2
2000 ADD R1, R2 3000 MOV R1,R2 4000 SUB R6, R1
2004 XOR R3, R4 3004 ADD R5, R1 4008 XOR R1, R5
2008 CALL SUB1 3008 CALL SUB2 4012 RETURN
2012 SUB R4, R5 3012 RETURN

Initially the stack pointer SP contains 5000.


What are the content of PC, SP, and the top of the stack?
i) After the subroutine call instruction is executed in the main program?
ii) After the subroutine call instruction is executed in the subroutine SUB1?
iii) After the return from SUB2 subroutine?
Numerical
Numerical
The content of the top of the memory stack is 5000. The content of the stack
pointer SP is 3000. Assume you want to organize a nested subroutine calls on a
computer as follows:
the routine Main calls a subroutine SUB1 by executing a two-word call subroutine
instruction located in memory at address 1000 followed by the address field of 6000 at
location 1001.Again subroutine SUB1 calls another subroutine SUB2 by executing a
two-word call subroutine instruction located in memory at address 6050 followed by the
address field of 8000 at location 6051 . What are the content of PC, SP, and the top of
the stack?
i) After the subroutine call instruction is executed in the main routine?
ii) After the subroutine call instruction is executed in the subroutine SUB1?
iii) After the return from SUB2 subroutine?
Thank You

You might also like