Presentation on theme: "Week 8 Stack and Subroutines. Stack The stack is a section of data memory (or RAM) that is reserved for storage of temporary data The data may represent."— Presentation transcript:
Week 8 Stack and Subroutines
Stack The stack is a section of data memory (or RAM) that is reserved for storage of temporary data The data may represent addresses The stack operates in a last-in first-out (LIFO) manner This 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
Stack Think of a stack of plates It is natural to add a plate to the top of stack It is natural to remove a plate from the top also The terminology used for stacks Push Meaning add to of stack Pop Meaning 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.
Stack 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 S tack P ointer (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!
Stack: Example operation
Stack: 8051 Details 8051 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 locations Initialised to address 07H on power up Can be changed if needed Stack grows with increasing memory addresses The two instructions are PUSH direct Operates by firstly incrementing the contents of the SP and then storing the value addressed by direct at the address in SP POP direct Operates 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 Model Note: The SP reg is shown twice here!
Example Assume SP contains 07H, A contains 3AH and address 40H contains 00H What does the stack look like after these instructions? PUSH ACC INC A PUSH ACC POP 39H PUSH 40H ADD A,#05H PUSH ACC POP 40H POP ACC
Example 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 Solution PUSH ACC; Store on Stack. PUSH B PUSH PSW ….; Do calculations ….; here. POP PSW; Restore from stack. POP B; Note order of instructions, POP ACC; this is important!!
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 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 is CALL 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: Example MOV P1,10H MOV A,90H mainXRL P1,A CALL Delay JMP main Delay: ; delay 100ms MOV R7,#200 ; 200*500us=100ms dy1:MOV R6,#250 ; 250*2us=500us dy2: 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: RET MOV P1,10H MOV A,90H Main:XRL P1,A CALL Delay JMP main Delay: ; delay 100ms MOV R7,#200 ; 200*500us=100ms dy1:MOV R6,#250 ; 250*2us=500us dy2: 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 is RET It has no operands The execution of this instruction causes the PC to be reloaded with two bytes (16 bits) that are popped off the stack
Subroutines 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 instructions See next example
Subroutines When subroutines are called, care must be taken not to corrupt resources that are used by the calling code Calling code refers to code from which subroutine called from Called code refers to subroutine code itself A good way to address this issue is to use the stack Consider the Delay subroutine example. This uses R6 and R7. What if the called function used these registers
Subroutines MOV P1,10H MOV A,90H Main:XRL P1,A CALL Delay JMP main Delay: PUSH 07 PUSH 06 ; delay 100ms MOV R7,#200 ; 200*500us=100ms dy1:MOV R6,#250 ; 250*2us=500us dy2: DJNZ R6,dy2 ; here for 500us DJNZ R7,dy1 ; repeat 200 times ; (100ms delay) POP 06 POP 07 RET 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
Subroutines Subroutines often require data to be passed in/out There are 2 standard approaches Both are flexible in that data does not have to be located at fixed addresses in memory The two approaches Through registers >Limited no. of registers >Fast Using the stack >More space available >Allows re-entrant operation (recursive calls)
Examples Write a delay routine that can provide a delay of between 100ms and 10s, in increments of 100ms. Use both approaches for passing data
Exercises What is instruction encoding for each of the following? PUSH 41H POP 41H