Week 8
Stack and Subroutines
StackStackThe stack is a section of data memory (or RAM) that is
reserved for storage of temporary dataThe data may represent addresses
The stack operates in a last-in first-out (LIFO) mannerThis means that access to the stack is in a restricted manner,
whereby new data is added to the top of the stack and data is removed from the top also
While it appear that such a structure for storing and retrieving data seems limited, the stack actually makes handling of certain programming tasks very easy
See important uses for stack soon
StackStack
Think of a stack of platesIt is natural to add a plate to the
top of stackIt is natural to remove a plate from
the top alsoThe terminology used for stacks
Push Meaning add to of stack
PopMeaning remove from top of stack
The terminology associated with stacks comes from the spring loaded plate containers common in dining halls.
When a plate is washed it is pushed on the stack.
When someone needs food, a clean plate is popped off the stack.
StackStack
Data is pushed on top and poped from the top of a stack
To access a stack in memory, the address of the top of the stack is needed
Processers have a special register called a Stack Pointer (SP) to store the address of the stack top
Actually processor designers differ in how this is handled.
Some processors have a stack pointer that points to the last data pushed on the stack, while others point to the next available location!
SP
n+5n+4n+3n+2n+1
n
Stack: Example operationStack: Example operation
SP
n+5n+4n+3n+2n+1
n
SP
n+5n+4
4n+3n+2n+1
n
PUSH #4
SP
n+58n+44n+3
n+2n+1
n
PUSH #8
SP
n+58n+44n+3
n+2n+1
n
POP A
8 A
SP
n+515n+44n+3
n+2n+1
n
PUSH 15
8 A
11
SP
n+515n+44n+3
n+2n+1
n
PUSH 11
8 A
Stack: 8051 DetailsStack: 8051 Details8051 has a SP reg
8 bit register in the SFR block (address
Used to address (indirectly) a location in internal data memory
Max of 256 locationsInitialised to address 07H on
power upCan be changed if needed
Stack grows with increasing memory addresses
The two instructions arePUSH direct
Operates by firstly incrementing the contents of the SP and then storing the value addressed by direct at the address in SP
POP directOperates by copying the byte
pointed to by SP into the location specified by direct. The contents of SP is then decremented by 1
8051 Programmer’s Model8051 Programmer’s Model
PC
A
B
R7R6R5R4R3R2R1R0
8-bits
16-bits
SP
128bytes
8 bits 32 bytesused
00h
20h
80h
FFh
128 bytes
80h
FFhSFRs
128bytes
DirectAddressing
InDirectAddressing
Direct &Indirect
Addressing
Note: The SP reg is shown twice here!
ExampleExample
SP
000AH
0006H0007H0008H0009H
A
000AH
000BH000CH000DH000EH
0041H
0038H0039H0040H
Assume SP contains 07H, A contains 3AH and address 40H contains 00H
What does the stack look like after these instructions?
PUSH ACCINC APUSH ACCPOP 39HPUSH 40HADD A,#05HPUSH ACCPOP 40HPOP ACC
ExampleExample
The A, B and PSW registers are required to perform some calculations, but contain values that are needed later. Use the stack to solve the problem
SolutionPUSH ACC ; Store on Stack.PUSH BPUSH PSW…. ; Do calculations…. ; here.POP PSW ; Restore from stack.POP B ; Note order of instructions,POP ACC ; this is important!!
Subroutines
SubroutinesSubroutines
Subroutines provide a mechanisn to reuse a piece of code
They also simplify the design and coding process, allowing the programmer to partition and abstract subtasks
Subroutines also makes testing and debugging easier
Subroutines: ACALL and LCALL
Subroutines: ACALL and LCALL
Just as there was AJMP and LJMP, similarly there is ACALL and LCALL. The assembler will select the approapiate call instruction if the CALL instruction is used (see earlier lectures)
The CALL instruction isCALL addr
Operates by saving the address of the next instruction on the stack and transferring program control to the subroutine code starting at addr
By saving the address of the next instruction, it is possible to resume execution of the next instruction when the subroutine is finished
Subroutines: ExampleSubroutines: Example
MOV P1,10HMOV A,90H
main XRL P1,A CALL Delay
JMP main
Delay:; delay 100ms
MOV R7,#200 ; 200*500us=100msdy1: MOV R6,#250 ; 250*2us=500usdy2: DJNZ R6,dy2 ; here for 500us DJNZ R7,dy1 ; repeat 200 times ; (100ms delay)
RET
When the instruction CALL Delay is executed,
The address of the next instruction is pushed onto the stack automatically
2 address bytes The PC is loaded with the
address of the Delay subroutine
The next instruction to be fetched will then be the first instruction in the Delay subroutine
Subroutines: RETSubroutines: RET
MOV P1,10HMOV A,90H
Main: XRL P1,A CALL Delay
JMP main
Delay:; delay 100ms
MOV R7,#200 ; 200*500us=100msdy1: MOV R6,#250 ; 250*2us=500usdy2: DJNZ R6,dy2 ; here for 500us DJNZ R7,dy1 ; repeat 200 times ; (100ms delay)
RET
How does the processor know when to return from the subroutine?
When a RET instruction is executed
The instruction isRET
It has no operandsThe execution of this
instruction causes the PC to be reloaded with two bytes (16 bits) that are popped off the stack
Example:SimulationExample:
Simulation
SubroutinesSubroutines
Always make sure to include a RET instruction in the subroutine
What are the consequences of not doing so?Because the stack is used, make sure that it is
not corrupted between calling the subroutine and returning
Can still use PUSH and POP instructionsSee next example
SubroutinesSubroutinesWhen subroutines are called, care must be taken not
to corrupt resources that are used by the calling codeCalling code refers to code from which subroutine called
fromCalled code refers to subroutine code itself
A good way to address this issue is to use the stackConsider the Delay subroutine example.
This uses R6 and R7. What if the called function used these registers
Subroutines
Subroutines
MOV P1,10HMOV A,90H
Main: XRL P1,ACALL Delay
JMP main
Delay:PUSH 07PUSH 06; delay 100ms
MOV R7,#200 ; 200*500us=100msdy1: MOV R6,#250 ; 250*2us=500usdy2: DJNZ R6,dy2 ; here for 500us DJNZ R7,dy1 ; repeat 200 times
; (100ms delay)POP 06POP 07RET
By storing the values of R6 and R7 on the stack at start of subroutine and restoring the original values from the stack at the end of the routine, it is now possible to use the delay routine, even when it is called from a section of code that uses R6 or R7 for a different purpose.
This really makes the Delay routine very usable.
Assumes Bank 0 used
SubroutinesSubroutines
Subroutines often require data to be passed in/outThere are 2 standard approaches
Both are flexible in that data does not have to be located at fixed addresses in memory
The two approachesThrough registers
> Limited no. of registers> Fast
Using the stack> More space available> Allows re-entrant operation (recursive calls)
ExamplesExamples
Write a delay routine that can provide a delay of between 100ms and 10s, in increments of 100ms.
Use both approaches for passing data
ExercisesExercises
What is instruction encoding for each of the following?PUSH 41H
POP 41H
Top Related