Stack and SUBROUTINES Bindu Agarwalla
Stack and SUBROUTINES Bindu Agarwalla
Bindu Agarwalla
STACK
0
.
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
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
On Call
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
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