Chapter 8 Stacks 1996 1998 1982 1995 Topics

  • Slides: 34
Download presentation
Chapter 8 – Stacks 1996 1998 1982 1995

Chapter 8 – Stacks 1996 1998 1982 1995

Topics to Cover… n n n The Stack Subroutines Subroutine Linkage Saving Registers Stack

Topics to Cover… n n n The Stack Subroutines Subroutine Linkage Saving Registers Stack Operations Activation Records n n n Example 8. 1: Activation Records Recursive Subroutines Interrupt Stack Usage BYU CS/ECEn 124 Chapter 8 - Stacks 2

Levels of Transformation Problems Algorithms Language (Program) Programmable Machine (ISA) Architecture Computer Specific Microarchitecture

Levels of Transformation Problems Algorithms Language (Program) Programmable Machine (ISA) Architecture Computer Specific Microarchitecture Manufacturer Specific Circuits Devices BYU CS/ECEn 124 Chapter 8 - Stacks 3

The Stacks n n n Stacks are the fundamental data structure of computers today.

The Stacks n n n Stacks are the fundamental data structure of computers today. A stack is a Last In, First Out (LIFO) abstract data structure. A true stack is a restricted data structure with two fundamental operations, namely push and pop. Elements are removed from a stack in the reverse order of their addition. Memory stacks are used for random access of local variables. BYU CS/ECEn 124 Chapter 8 - Stacks 4

The Stack MSP 430 Stack n Hardware support for stack n n Register R

The Stack MSP 430 Stack n Hardware support for stack n n Register R 1 – Stack Pointer (SP) Initialized to highest address of available RAM MSP 430 G 2553 0 x 0400 (512 bytes) MSP 430 F 2274 0 x 0600 (1 k bytes) n n Stack grows down towards lower memory addresses. Initialize stack pointer at beginning of program STACK . equ start: mov. w BYU CS/ECEn 124 0 x 0400 ; top of stack #STACK, SP ; initialize stack pointer Chapter 8 - Stacks 5

The Stack MSP 430 Stack n The MSP 430 stack is a word structure

The Stack MSP 430 Stack n The MSP 430 stack is a word structure n n Elements of the stack are 16 -bit words. The LSB of the Stack Pointer (SP) is always 0. The SP points to the last word added to the stack (TOS). The stack pointer is used by n n n PUSH – put a value on the stack POP – retrieve a value off the stack CALL – put a return address on the stack RET – retrieve a return address off the stack RETI – retrieve a return address and status register off the stack Interrupts – put a return address and status register on the stack BYU CS/ECEn 124 Chapter 8 - Stacks 6

The Stack Computer Memory – Up or Down? x 0000 x. FFFF Up Down

The Stack Computer Memory – Up or Down? x 0000 x. FFFF Up Down 1995 1982 1998 1996 Down x. FFFF BYU CS/ECEn 124 Up x 0000 Chapter 8 - Stacks 7

The Stack Implementing Stacks in Memory Unlike a coin stack, in a memory stack,

The Stack Implementing Stacks in Memory Unlike a coin stack, in a memory stack, the data does not move in memory, just the pointer to the top of stack. n Current SP X 0280 x 0282 x 0280 x 027 E x 027 C x 027 A //// Current SP x 027 E R 1 x 0282 TOP x 0280 //// x 027 E //// x 027 A x 027 C //// R 1 x 027 A x 0282 x 0280 //// Current SP x 027 C R 1 x 0282 //// #18 x 0280 #18 //// TOP x 027 E x 027 C #25 x 027 C //// x 027 A #58 TOP x 027 A Push #0 x 0018 BYU CS/ECEn 124 Current SP Push #0 x 0025 Push #0 x 0058 x 027 E //// #18 #25 #58 Pop R 15 Current SP R 1 x 027 A x 0282 x 0280 x 027 E TOP x 027 C x 027 A R 1 //// #18 #25 #36 TOP Push #0036 #58 -> R 15 Chapter 8 - Stacks 8

Quiz 8. 1 1. What is the value of the stack pointer after the

Quiz 8. 1 1. What is the value of the stack pointer after the second call to delay? 2. Is there a problem with the program? start: mov. w bis. b #0 x 0400, SP #WDTPW+WDTHOLD, &WDTCTL #0 x 01, &P 1 DIR ; P 1. 0 as output mainloop: bis. b push call bic. b call jmp #0 x 01, &P 1 OUT #1000 #delay #0 x 01, &P 1 OUT #delay mainloop ; turn on LED delay: mov. w 2(sp), r 15 ; get delay counter delaylp 2: dec. w jnz ret r 15 delaylp 2 ; y ; delay over? ; n . sect. word. end ". reset" start ; reset Vector ; start address BYU CS/ECEn 124 Chapter 8 - Stacks ; turn off led 9

Subroutines n A subroutine is a program fragment that performs some useful function. n

Subroutines n A subroutine is a program fragment that performs some useful function. n n Subroutines help to organize a program. Subroutines should have strong cohesion – perform only one specific task. Subroutines should be loosely coupled – interfaced only through parameters (where possible) and be independent of the remaining code. Subroutines keep the program smaller n n Smaller programs are easier to maintain. Reduces development costs while increasing reliability. Fewer bugs – copying code repeats bugs. Subroutines are often collected into libraries. BYU CS/ECEn 124 Chapter 8 - Stacks 10

Subroutines The Call / Return Mechanism Faster programs. Less overhead. BYU CS/ECEn 124 Smaller

Subroutines The Call / Return Mechanism Faster programs. Less overhead. BYU CS/ECEn 124 Smaller programs. Easier to maintain. Reduces development costs. Increased reliability. Fewer bugs do to copying code. More library friendly. Chapter 8 - Stacks 11

Subroutine Linkage n n n A subroutine is “called” in assembly using the MSP

Subroutine Linkage n n n A subroutine is “called” in assembly using the MSP 430 CALL instruction. The address of the next instruction after the subroutine call is saved by the processor on the stack. Parameters are passed to the subroutine in registers and/or on the stack. Local variables are created on the stack at the beginning of the subroutine and popped from the stack just before returning from the subroutine. At the end of a subroutine, a RET instruction “pops” the top value from the stack into the program counter. BYU CS/ECEn 124 Chapter 8 - Stacks 12

Subroutine Linkage Stack Operations n Single operand instructions: Mnemonic Operation Description PUSH(. B or.

Subroutine Linkage Stack Operations n Single operand instructions: Mnemonic Operation Description PUSH(. B or. W) src SP-2 SP, src @SP Push byte/word source on stack CALL dst tmp , SP-2 SP, PC @SP, tmp PC Subroutine call to destination TOS SR, SP+2 SP TOS PC, SP+2 SP Return from interrupt dst RETI n Emulated instructions: Mnemonic Operation Emulation Description RET @SP PC SP+2 SP MOV @SP+, PC Return from subroutine POP(. B or. W) dst @SP temp SP+2 SP temp dst MOV(. B or. W) @SP+, dst Pop byte/word from stack to destination BYU CS/ECEn 124 Chapter 8 - Stacks 13

Call Instruction call #func PC 0 x 12 b 0 0 x 801 e

Call Instruction call #func PC 0 x 12 b 0 0 x 801 e dr CPU Data Bus Da (1 cycle) us (+1 cyc 0 x 4130 Address BYU CS/ECEn 124 PC SP fffe ta B SP SP Registers 0 x 12 b 0 IR le) ADDE R 1 (+ +2 ) cle y c le) PC PC PC us B s es (+1 cyc Memory Ad ; M(--sp) = PC; PC = M(func) Bus +1 Bus ( a t a D ) cycle ALU Chapter 8 - Stacks 14

Subroutine Linkage Subroutine Call n CALL Syntax Operation n Description n n Status Bits

Subroutine Linkage Subroutine Call n CALL Syntax Operation n Description n n Status Bits Example BYU CS/ECEn 124 Subroutine CALL dst tmp (SP− 2) SP PC @SP tmp PC A subroutine call is made to an address anywhere in the 64 K address space. All addressing modes can be used. The return address (the address of the following instruction) is stored on the stack. The call instruction is a word instruction. Status bits are not affected. Chapter 8 - Stacks 15

Subroutine Linkage CALL Examples n n n n CALL #EXEC ; Call on label

Subroutine Linkage CALL Examples n n n n CALL #EXEC ; Call on label EXEC or immediate address (e. g. #0 A 4 h) ; @PC+ → tmp, SP− 2 → SP, PC → @SP, tmp → PC CALL EXEC ; Call on the address contained in EXEC ; X(PC)→tmp, PC+2→PC, SP− 2→SP, PC→@SP, tmp→PC CALL &EXEC ; Call on the address contained in absolute address EXEC ; X(0)→tmp, PC+2→PC, SP− 2→SP, PC→@SP, tmp→PC CALL R 5 ; Call on the address contained in R 5 ; R 5→tmp, SP− 2→SP, PC→@SP, tmp→PC CALL @R 5 ; Call on the address contained in the word pointed to by R 5 ; @R 5→tmp, SP− 2→SP, PC→@SP, tmp→PC CALL @R 5+ ; Call on the address contained in the word pointed to by R 5 ; and increment pointer in R 5. ; @R 5+→tmp, SP− 2→SP, PC→@SP, tmp→PC CALL X(R 5) ; Call on the address contained in the address pointed to by ; R 5 + X (e. g. table with address starting at X) ; X can be an address or a label ; X(R 5)→tmp, PC+2→PC, SP− 2→SP, PC→@SP, tmp→PC BYU CS/ECEn 124 Chapter 8 - Stacks 16

Subroutine Linkage Caution… n n The destination of branches and calls is used indirectly,

Subroutine Linkage Caution… n n The destination of branches and calls is used indirectly, and this means the content of the destination is used as the address. Errors occur often when confusing symbolic and absolute modes: n n n ; Subroutine’s address is stored in MAIN ; Subroutine starts at address MAIN The real behavior is easily seen when looking to the branch instruction. It is an emulated instruction using the MOV instruction: n n n CALL MAIN CALL #MAIN BR MAIN ; Emulated instruction BR MOV MAIN, PC ; Emulation by MOV instruction The addressing for the CALL instruction is exactly the same as for the BR instruction. BYU CS/ECEn 124 Chapter 8 - Stacks 17

Return Instruction ret ; mov. w @sp+, PC Memory e) cy cl B Registers

Return Instruction ret ; mov. w @sp+, PC Memory e) cy cl B Registers 0 x 4130 IR PC SP 0002 +2 Bu s (+ PC s es r d Ad us 1 0 x 12 b 0 0 x 801 e CPU le) D 0 x 4130 SP SP SP BYU CS/ECEn 124 Address (+1 cyc PC PC PC at a ADDE R Bus Data Bus (+1 cycle) ALU Chapter 8 - Stacks 18

Subroutine Linkage Return from Subroutine n n n n RET Syntax Operation Emulation Description

Subroutine Linkage Return from Subroutine n n n n RET Syntax Operation Emulation Description Status Bits Example BYU CS/ECEn 124 Return from subroutine RET @SP→ PC SP + 2 → SP MOV @SP+, PC The return address pushed onto the stack by a CALL instruction is moved to the program counter. The program continues at the code address following the subroutine call. Status bits are not affected. Chapter 8 - Stacks 19

Quiz 8. 2 1. What is wrong (if anything) with the following code? 2.

Quiz 8. 2 1. What is wrong (if anything) with the following code? 2. How many times will delay be called for each loop? 3. How long will my. Delay delay? loop: jmp call #my. Delay loop my. Delay: mov. w call #0, r 15 #delay: #1, r 15 delay BYU CS/ECEn 124 sub. w jne ret Chapter 8 - Stacks 20

Saving Registers Saving and Restoring Registers n Called routine -- “callee-save” n n Calling

Saving Registers Saving and Restoring Registers n Called routine -- “callee-save” n n Calling routine -- “caller-save” n n n At beginning of subroutine, save all registers that will be altered (unless a register is used to return a value to the calling program or is a scratch register!) Before returning, restore saved registers in reverse order. Or, avoid using registers altogether. If registers need to be preserved across subroutine calls, the calling program would save those registers before calling routine and restore upon returning from routine. Obviously, avoiding the use of registers altogether would be considered caller-safe. Values are saved by storing them in memory, preferably on the stack. BYU CS/ECEn 124 Chapter 8 - Stacks 21

Saving Registers Caller-Save vs. Callee-Save Registers call subroutine Save Registers subroutine call subroutine Restore

Saving Registers Caller-Save vs. Callee-Save Registers call subroutine Save Registers subroutine call subroutine Restore Registers BYU CS/ECEn 124 subroutine Restore Registers Chapter 8 - Stacks 22

Stack Operations n Single operand instructions: Mnemonic Operation Description PUSH(. B or. W) src

Stack Operations n Single operand instructions: Mnemonic Operation Description PUSH(. B or. W) src SP-2 SP, src @SP Push byte/word source on stack CALL dst tmp , SP-2 SP, PC @SP, tmp PC Subroutine call to destination TOS SR, SP+2 SP TOS PC, SP+2 SP Return from interrupt dst RETI n Emulated instructions: Mnemonic Operation Emulation Description RET @SP PC SP+2 SP MOV @SP+, PC Return from subroutine @SP temp SP+2 SP temp dst MOV(. B or. W) @SP+, dst Pop byte/word from stack to destination POP(. B or dst . W) BYU CS/ECEn 124 Chapter 8 - Stacks 23

Push Instruction push. w cnt ; M(--sp) = M(cnt) us Memory PC PC PC

Push Instruction push. w cnt ; M(--sp) = M(cnt) us Memory PC PC PC cnt 0 x 1210 0 x 000 c s. B es dr Ad CPU Data Bus Address 0 xa 5 a 5 Address 0 x 1210 IR PC fffe Data Bus (+1 cycle) Data Bus SP SP (+1 cycle) ADDE R Bus 1 (+ Registers PC SP +2 ) cle y c (+1 cycle ) Bus 0 xa 5 a 5 ALU Dat a Bu s (+ BYU CS/ECEn 124 1 cy cle) Chapter 8 - Stacks 24

Stack Operations Push Operand Push word or byte onto stack n PUSH{. W or.

Stack Operations Push Operand Push word or byte onto stack n PUSH{. W or. B} src n SP − 2 → SP src → @SP n Description The stack pointer is decremented by two, then the source operand is moved to the RAM word addressed by the stack pointer (TOS). n Status Bits Status bits are not affected. n Example PUSH SR ; save SR PUSH R 8 ; save R 8 PUSH. B &TCDAT ; save data at address ; TCDAT onto stack Note: The system stack pointer (SP) is always decremented by two, independent of the byte suffix. n PUSH Syntax Operation BYU CS/ECEn 124 Chapter 8 - Stacks 25

Stack Operations Pop Operand Pop word or byte from stack to destination n POP{.

Stack Operations Pop Operand Pop word or byte from stack to destination n POP{. W or. B} dst n @SP −> temp SP + 2 −> SP temp −> dst n Emulation MOV{. W or. B} @SP+, dst n Description The stack location pointed to by the stack pointer (TOS) is moved to the destination. The stack pointer is incremented by two afterwards. n Status Bits Status bits are not affected. n Example POP R 7 ; Restore R 7 POP. B LEO ; The low byte of the stack is ; moved to LEO. Note: The system stack pointer (SP) is always incremented by two, independent of the byte suffix. n POP Syntax Operation BYU CS/ECEn 124 Chapter 8 - Stacks 26

Subroutine Linkage Stack Operations 0400: 0 xf 820: . . . 0 xf 822:

Subroutine Linkage Stack Operations 0400: 0 xf 820: . . . 0 xf 822: call #subroutine 0 xf 826: . . . 03 fe: 03 fc: 03 fa: subroutine: 0 xf 852: push r 15 0 xf 854: push r 14. . . 03 f 8: 03 f 6: 03 f 4: 03 f 2: 0 xf 882: pop r 14 0 xf 884: pop r 15 0 xf 886: ret BYU CS/ECEn 124 0 xf 826 r 15 r 14 SP SP Unprotected! Chapter 8 - Stacks 27

Activation Records n n A subroutine is activated when called an activation record is

Activation Records n n A subroutine is activated when called an activation record is allocated (pushed) on the stack. An activation record is a template of the relative positions of local variables on the stack as defined by the subroutine. n n n n Return address Memory for local subroutine variables Parameters passed to subroutine from caller Saved registers used in subroutine (callee-save) A new activation record is created on the stack for each invocation of a subroutine or function. A frame pointer indicates the start of the activation record. When the subroutine ends and returns control to the caller, the activation record is discarded (popped). BYU CS/ECEn 124 Chapter 8 - Stacks 28

Activation Records Example 8. 1: Activation Record DELAY . cdecls C, "msp 430. h".

Activation Records Example 8. 1: Activation Record DELAY . cdecls C, "msp 430. h". equ (50/8) ; MSP 430 reset: . text mov. w bis. b #0 x 0400, SP #WDTPW+WDTHOLD, &WDTCTL #0 x 01, &P 1 DIR ; ; xor. b push. w call jmp #0 x 01, &P 1 OUT #DELAY #delay mainloop: beginning of code init stack pointer stop WDT set P 1. 0 as output Delay Activation Record: 4(SP) = delay ; toggle P 1. 0 count ; pass count on stack 2(SP) delay = return address ; call subroutine 0(SP) delay = r 15 ; delay subroutine: stack usage 4| DELAY | ; 2| ret | subroutine frame (6 bytes) ; (SP) => 0| r 15 | / Stack: delay: push. w r 15 ; callee-save 2(SP)R 15 = delay mov. w #0, r 15 ; use as count inner counter 0(SP) = return address delay 02: BYU CS/ECEn 124 dec. w jne pop. w mov. w ret r 15 delay 02 4(SP) delay 02 r 15 @SP+, 0(SP) ; ; ; ; . sect. word. end ". reset" reset ; MSP 430 reset Vector Stack: ; start address (emply) Chapter 8 - Stacks inner delay over? n y, outer done? n. Stack: y, restore register(s) 0(SP) = return address pop input delay count return from subroutine 29

Quiz 8. 3 Change the following code to use a callee-save, loosely coupled, cohesive

Quiz 8. 3 Change the following code to use a callee-save, loosely coupled, cohesive subroutine. start: . cdecls. text mov. w bis. b C, "msp 430. h" #0 x 0400, SP #WDTPW+WDTHOLD, &WDTCTL #0 x 01, &P 1 DIR ; P 1. 0 as output mainloop: bis. b mov. w #0 x 01, &P 1 OUT ; turn on LED #10000, r 15 ; delay counter delaylp 1: dec. w jnz bic. b mov. w r 15 delaylp 1 #0 x 01, &P 1 OUT #0, r 15 ; delay over? ; n ; turn off led ; delay counter delaylp 2: dec. w jnz mov. w r 15 delaylp 2 #0, r 15 ; delay over? ; n ; delay counter delaylp 3: dec. w jnz jmp r 15 delaylp 3 mainloop ; delay over? ; n ; y, toggle led ". reset" start ; reset vector ; start address . sect. word. end BYU CS/ECEn 124 Chapter 8 - Stacks 30

Recursive Subroutines Recursive Subroutine n n A subroutine that makes a call to itself

Recursive Subroutines Recursive Subroutine n n A subroutine that makes a call to itself is said to be a recursive subroutine. Recursion allows direct implementation of functions defined by mathematical induction and recursive divide and conquer algorithms n n n Factorial, Fibonacci, summation, data analysis Tree traversal, binary search Recursion solves a big problem by solving one or more smaller problems, and using the solutions of the smaller problems, to solve the bigger problem. Reduces duplication of code. MUST USE STACK! BYU CS/ECEn 124 Chapter 8 - Stacks 31

Interrupts n n n Execution of a program normally proceeds predictably, with interrupts being

Interrupts n n n Execution of a program normally proceeds predictably, with interrupts being the exception. An interrupt is an asynchronous signal indicating something needs attention. n Some event has occurred n Some event has completed The processing of an interrupt subroutine uses the stack. n Processor stops with it is doing, n stores enough information on the stack to later resume, n executes an interrupt service routine (ISR), n restores saved information from stack (RETI), n and then resumes execution at the point where the processor was executing before the interrupt. BYU CS/ECEn 124 Chapter 8 - Stacks 32

Interrupts Interrupt Stack Item 1 Item 2 SP Prior to Interrupt PC add. w

Interrupts Interrupt Stack Item 1 Item 2 SP Prior to Interrupt PC add. w jnc add. w r 4, r 7 $+4 #1, r 6 add. w r 5, r 6 xor. b reti #1, &P 1 OUT Interrupt (hardware) Item 1 Item 2 PC SR SP § § § Program Counter pushed on stack Status Register pushed on stack Interrupt vector moved to PC Further interrupts disabled Interrupt flag cleared PC Execute Interrupt Service Routine (ISR) Item 1 Item 2 PC SR BYU CS/ECEn 124 SP Return from Interrupt (reti) PC § Status Register popped from stack § Program Counter popped from stack Chapter 8 - Stacks add. w jnc add. w r 4, r 7 $+4 #1, r 6 add. w r 5, r 6 33

BYU CS/ECEn 124 Chapter 8 - Stacks 34

BYU CS/ECEn 124 Chapter 8 - Stacks 34