Download presentation
Presentation is loading. Please wait.
Published byGyles Gallagher Modified over 8 years ago
1
CPEN 231: Microcomputer Architecture and Assembly Programming Lecture 10: Stack and Subroutines John Tadrous, Ph.D. ECE Rice University jtadrous@rice.edu 1
2
Stack A range of memory locations that operate in a Last-in-first-out (LIFO) manner Stack can be used for -Temporary storage of information -Passing arguments to a subroutine -Storing the return address of a program that calls on a subroutine -Saving the processing environment upon the occurrence of an interrupt Bottom element Top element High address Low address Storatge Retrieval SP 2
3
Stack Characteristics and Requirements Characteristics Can only be accessed from one end Grows from high address to low address Stack overflow -If stack grows into locations with address lower than stack buffer Stack underflow - If stack is pulled after being emptied Requirements Dedicated block of memory Stack pointer (SP), 2 bytes (16 bits) - Points to the top element Bottom element Top element High address Low address Storatge Retrieval SP 3
4
Basic Stack Operations SP Two basic operations: Push and Pull -These two operations are in reverse order of each other PushPSHA;SP<- SP-1,[SP]<- A, PSHB;SP<- SP-1,[SP]<- B, PSHX;SP<- SP–1,[SP]=low byte of IX ;SP<- SP–1,[SP]=high byte of IX Pull PULX;high byte of IX=[SP],SP<-SP+1 ;low byte of IX=[SP],SP<-SP+1 PULB;B<-[SP],SP<-SP+1 PULA;A<-[SP],SP<-SP+1 Bottom element [A] [B] [IX (low)] SP [IX (high)] SP 4
5
Push and Pull Instructions MnemonicFunctionEquivalent Instruction PSHAPush A onto stackSTAA 1, –SP PSHBPush B onto stackSTAB 1, –SP PSHCPush CCR onto the stackNone PSHDPush D onto the stackSTD 2, –SP PSHXPush X onto stackSTX 2, –SP PSHYPush Y onto stackSTY 2, –SP PULAPull A from the stackLDAA 1, SP+ PULBPull B from the stackLDAA 1, SP+ PULCPull CCR from the stackNone PULDPull D from the stackLDD 2, SP+ PULXPull X from the stackLDX 2, SP+ PULYPull Y from the stackLDY 2, SP+ 5
6
Instructions for Stack Pointer MnemonicFunction DESdecrements the contents of the stack pointer by 1 INSincrements the contents of the stack pointer by 1 LDS loads the contents of a memory location or an immediate value into SP STS stores the contents of the stack pointer in a memory location TSXplaces the contents of SP into X: IX SP TSYplaces the contents of SP into Y: IY SP TXSplaces the contents of X into SP: SP IX TYSplaces the contents of Y into SP: SP IY 6
7
Example 1 What is the content of the stack after the execution of the following instruction sequence? LDS#$1500 LDAA#$20 PSHA LDAB #40 PSHB LDX #0 PSHX 7 $20 $28=40 $00 (low) $00 (high) $1500 $14FF $14FE $14FD $14FC $14FB Stack SP
8
Example 2 Suppose that a segment of code is executed with the reference of stack below. Determine the contents of A, B, X, SP and the stack after each instruction when initially A = $00, B = $00, and X = $00 Final values: A=$10, B=$10, X=$0A03, SP=$0A05 8 XX $11 $3F $4F $10 $A1 $CE $0A06 $0A05 $0A04 $0A03 $0A02 $0A01 $0A00 TSX LDAB 3, X PULA PULX PSHB TSX LDAA 2, X PULA PULB ; X <- SP=$0A01 ; B <- [3+$0A01]=[$0A04]=$10 ; A <- [$0A01] = $11, SP <- SP+1=$0A02 ; high of IX <- [$0A02]=$3F, SP <- SP+1=$0A03, ; low of IX <- [$0A03]=$4F, SP <- SP+1=$0A04 ; SP <-SP - 1 = $0A03, [$0A03] = $10 ; X <- SP = $0A03 ; A <- [2+$0A03]=[$0A05]=$A1 ; A <- [$0A03]=$10, SP <- SP+1=$0A03+1=$0A04 ; B <- [$0A04]=$10, SP <- SP+1=$0A04+1=$0A05 SP $10 SP
9
Subroutines Subroutine is a subprogram that is usually executed more than once during the execution of the main program Example: Compute the average age of students in 100 different groups 9
10
Advantages of Subroutines Modularity -Easier to manage, understand, and maintain Code Reuse - Makes the code shorter 10
11
Subroutine Call BSR : branch subroutine is specified in relative addressing mode and is in the range of -128 to +127 bytes from the instruction immediately after the BSR instruction. The return address is pushed into the stack. JSR : jump subroutine is specified in direct, extended, indexed, and indexed indirect mode. The subroutine being called can be within the range of 64 k locations. The return address is pushed into the stack CALL : call subroutine (work with expanded memory mode) The field specifies the page number and the address of the subroutine within the page to be called The page number is to be loaded into the PPAGE register 11
12
JSR Call Example JSR $A037 Store current PC into stack Load PC with address $A037 1.SP SP – 1 2.Push PCL 3.SP SP – 1 4.Push PCH 5.PC $A037 12
13
Subroutine Return RTS: return from subroutine -Retrieves the return address from the stack and return to the caller RTC: return from call -This instruction retrieves the page number and the return address from the stack 13
14
Example RTS Pull stored PC from stack 1. Pull PCH 2. SP SP +1 3. Pull PCL 4. SP SP +1 14
15
Example 3 A subroutine to delay for a certain amount of time The subroutine is called by: “ JSRDelay” Delay:PSHA ; Save registers used by subroutine on stack PSHX LDAA#250 Loop2:LDX#800 Loop1:DEX BNELoop1 DECA BNELoop2 PULX ; Restore registers in opposite order PULA RTS Save important registers used by the parent program on the stack at the beginning of the subroutine, and restore them in the end. 15
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.