1 / 32

Chapter 8 – Stacks

1996. 1998. 1982. 1995. Chapter 8 – Stacks. Topics to Cover…. The Stack Subroutines Subroutine Linkage Instruction Timing Stoplight Example Saving Registers Stack Operations Recursive Subroutines Activation Records. Levels of Transformation. Problems. Algorithms.

darci
Download Presentation

Chapter 8 – Stacks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1996 1998 1982 1995 Chapter 8 – Stacks

  2. Topics to Cover… • The Stack • Subroutines • Subroutine Linkage • Instruction Timing • Stoplight Example • Saving Registers • Stack Operations • Recursive Subroutines • Activation Records Chapter 8 - Stacks

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

  4. The Stack Stacks • Stacks are the fundamental data structure of computers today • A stack is a last in, first out (LIFO) abstract data type • A 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 Chapter 8 - Stacks

  5. The Stack MSP430 Stack • Hardware support for stack • Register R1 – stack pointer • Initialized to highest address of RAM • MSP430F2013  0x0280 (128 bytes) • MSP430F2274  0x0600 (1k bytes) • Stack grows down towards low addresses • Initialize stack at beginning of program STACK .set 0x0600 ; top of stack mov.w #STACK,SP ; Initialize stack pointer Chapter 8 - Stacks

  6. The Stack MSP430 Stack • Stack pointer holds the address of the top of the stack • The lsb of the stack pointer is always 0 • PUSH, POP, CALL, RET, and RETI instructions use the stack pointer • The stack is used for saving return addresses, local variable storage, and interrupts Chapter 8 - Stacks

  7. Down Up Up Down The Stack Computer Memory – Up or Down? x0000 xFFFF xFFFF x0000 Chapter 8 - Stacks

  8. Subroutines Subroutines • A subroutine is a program fragment that performs some useful function. • Specific functions that only apply to one program • Provides a way to organize the program. • Performs a specific task. • Relatively independent of the remaining code. • Keeps the program smaller (no need to repeat code). • Smaller programs are easier to maintain. • Reduces development costs while increasing reliability. • Fewer bugs – copying code repeats bugs. • Often collected into libraries. Chapter 8 - Stacks

  9. Subroutines The Call / Return Mechanism Chapter 8 - Stacks

  10. Stack Operations Single operand instructions: Emulated instructions: Subroutine Linkage Chapter 8 - Stacks

  11. Subroutine Linkage Subroutine linkage • A subroutine is “called” in assembly with a CALL instruction. • The address of the next instruction after the subroutine call is saved by the processor onto the stack. • Local variables are pushed/popped from the stack. • At the end of a subroutine, a RET instruction “pops” the top value from the stack into the program counter. Chapter 8 - Stacks

  12. “Calling” a Subroutine Push PC+2 on the stack and move the source to the PC: Instruction code: 0x000100101 This instruction uses 2 words Decrement the Stack Pointer (R1) by 2, store the Program Counter (PC) + 2 indirectly through the Stack Pointer, and then store the 16-bit, immediate value in the Program Counter (PC) The 16-bit address is stored in the word following the instruction. Subroutine Linkage CALL #TONI Chapter 8 - Stacks

  13. Return from Subroutine Pop the stack into the Program Counter: Instruction code: 0x4130 This emulated instruction uses 1 word The instruction directs the CPU to move the value indirectly pointed to by the Stack Pointer (R1) into the Program Counter (PC) and then increment the Stack Pointer by 2. Subroutine Linkage RET ; MOV @SP+,PC Chapter 8 - Stacks

  14. Subroutine Linkage Caution… • 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: • CALL MAIN ; Subroutine’s address is stored in MAIN • CALL #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: • 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. Chapter 8 - Stacks

  15. Instruction Timing Cycles Per Instruction... • Instruction timing: • 1 cycle to fetch instruction word • +1 cycle if source is @Rn, @Rn+, or #Imm • +2 cycles if source uses indexed mode • 1st to fetch base address • 2nd to fetch source • Includes absolute and symbolic modes • +2 cycles if destination uses indexed mode • +1 cycle if writing destination back to memory Chapter 8 - Stacks

  16. Instruction Timing Cycles Per Instruction... SrcDstCyclesLengthExample Rn Rm 1 1 MOV R5,R8 @Rm 2 1 MOV R5,@R6 x(Rm) 4 2 ADD R5,4(R6) EDE 4 2 XOR R8,EDE &EDE 4 2 MOV R5,&EDE #n x(Rm) 5 3 MOV #100,TAB(R8) &TONI &EDE 6 3 MOV &TONI,&EDE Chapter 8 - Stacks

  17. Instruction Timing Quiz • Given a 1.2 mhz processor, what value for DELAY would result in a 1/2 second delay? DELAY .equ ??? mov.w #DELAY,r12 delay1: dec.w r12 jn delay3 mov.w #1000,r15 delay2: dec.w r15 jne delay2 jmp delay1 delay3: Chapter 8 - Stacks

  18. Instruction Timing Quiz (cycles) • Given a 1.2 mhz processor, what value for DELAY would result in a 1/2 second delay? DELAY .equ ??? mov.w #DELAY,r12 ; 2 cycles delay1: dec.w r12 ; 1 cycle jn delay3 ; 2 cycles mov.w #1000,r15 ; 3 cycles delay2: dec.w r15 ; 3 x 1000 jne delay2 ; = 3000 cycles jmp delay1 ; 2 cycles delay3: Chapter 8 - Stacks

  19. Instruction Timing Quiz (answer) • Known equates: • 1 second = 1,200,000 cycles • 0.5 seconds = 1,200,000 / 2 = 600,000 cycles • Delay time in cycles: • ((DELAY x 3008) + 3) cycles • = 600,000 cycles • Hence: 600000 – 3 DELAY = ---------- 3008 = 199.467 = 199 Chapter 8 - Stacks

  20. Stoplight Example Stoplight Lab • Determine the clock speed of your MSP430F2013 (or F2274) processor. • Use these calculations to program software timing loops for you traffic light. • Turn on the green LED for 5 seconds. • Blink the green LED on and off at 1 second intervals for 6 seconds (3 off’s and 3 on’s). • Blink the green LED on and off at 0.25 second intervals for 4 seconds (8 off’s and 8 on’s). • And finally, turn the green LED off for 10 seconds. The total traffic light cycle time should be 25 seconds. • Repeat the stoplight cycle indefinitely. Chapter 8 - Stacks

  21. Stoplight Example Stoplight Lab ;*********************************************************************** ; CS/ECEn 124 Lab 1 - blinky.asm: Software Toggle P1.0 ; ; Description: Toggle P1.0 by xor'ing P1.0 inside of a software loop. ;*********************************************************************** .cdecls C,LIST, "msp430x20x3.h" ; MSP430F2013 ; .cdecls C,LIST, "msp430x22x4.h" ; MSP430F2274 ;---------------------------------------------------------------------- .text ; beginning of code RESET: mov.w #0x0280,SP ; init stack pointer mov.w #WDTPW+WDTHOLD,&WDTCTL ; stop WDT bis.b #0x01,&P1DIR ; set P1.0 as output mainloop: xor.b #0x01,&P1OUT ; toggle P1.0 mov.w #0,r15 ; use R15 as delay counter delayloop: dec.w r15 ; delay over? jnz delayloop ; n jmp mainloop ; y .sect ".reset" ; MSP430 RESET Vector .short RESET ; start address .end Chapter 8 - Stacks

  22. Stoplight Example Stoplight Lab .cdecls C,LIST, "msp430x20x3.h" ; MSP430F2013 DELAY .equ (50/8) .text ; beginning of code RESET: mov.w #0x0280,SP ; init stack pointer mov.w #WDTPW+WDTHOLD,&WDTCTL ; stop WDT bis.b #0x01,&P1DIR ; set P1.0 as output mainloop: xor.b #0x01,&P1OUT ; toggle P1.0 call #delay1sec jmp mainloop delay1sec: mov.w #DELAY,r12 ; get delay count delay02: dec.w r12 ; done? jeq delay06 ; y, return mov.w #0,r15 ; n, use R15 as delay counter delay04: dec.w r15 ; delay over? jnz delay04 ; n jmp delay02 ; y delay06: ret .sect ".reset" ; MSP430 RESET Vector .short RESET ; start address .end Chapter 8 - Stacks

  23. Saving Registers Saving and Restoring Registers • Called routine -- “callee-save” • At beginning of routine, save all registers that will be altered (unless altered value is desired by calling program!) • Before returning, restore those same registers in reverse order • Calling routine -- “caller-save” • If registers need to be preserved across subroutine calls, save before calling routine and restore upon returning from routine • Or, avoid using those registers altogether • Values are saved by storing them in memory, preferably on the stack. Chapter 8 - Stacks

  24. Save Registers Save Registers call subroutine call subroutine subroutine subroutine Restore Registers Restore Registers Saving Registers Caller-Save vs. Callee-Save Chapter 8 - Stacks

  25. Stack Operations Single operand instructions: Emulated instructions: Stack Operations Chapter 8 - Stacks

  26. Push Operand Push the contents of register R5 on the stack: Instruction code: 0x000100100 This instruction uses 1 word Decrement the Stack Pointer (R1) by 2 and store the destination value indirectly through the Stack Pointer Note: The stack pointer is always decremented by 2, whether a byte or word instruction. Stack Operations PUSH R5 Chapter 8 - Stacks

  27. Pop Operand Pop the stack into the destination: Instruction code: 0x4135 This emulated instruction uses 1 word The instruction directs the CPU to move the value indirectly pointed to by the Stack Pointer (R1) into the destination and then increment the Stack Pointer by 2. Stack Operations MOV @SP+,R5 Chapter 8 - Stacks

  28. Recursive Subroutine A subroutine that calls itself is said to be a recursive subroutine Recursion allows direct implementation of functions defined by mathematical induction and recursive divide and conquer algorithms Factorial, Fibonacci, summation Binary search Reduces duplication of code. MUST USE STACK! Recursive Subroutines Chapter 8 - Stacks

  29. Activation Records Activation Records • A function is activated when called • Save return address and call-ee registers • Allocate memory for local variables • An activation record is a template of the relative positions of its local variables in memory as defined by the function • Activation records are allocated on the stack • A frame pointer is used to indicate the start of the activation record • Could use a dedicated register • Or, more commonly, use the stack pointer Chapter 8 - Stacks

  30. Activation Records Activation Records • A new activation record is created on the stack for each invocation of a function • When the function completes and returns control to the caller, the activation record is discarded. • An activation record may contain: • Memory for local function variables • Bookkeeping information • Parameters passed to function from caller • Saved registers Chapter 8 - Stacks

  31. Activation Records Example • Example activation record (6 bytes): 4(SP)  Return address 2(SP)  Outer loop counter 0(SP)  Inner loop counter delay1sec: sub.w #4,SP ; activate 2 variables mov.w #DELAY,2(SP) ; set delay count delay02: dec.w 2(SP) ; done? jeq delay06 ; y, return mov.w #0,(SP) ; n, init delay counter delay04: dec.w 0(SP) ; delay over? jnz delay04 ; n jmp delay02 ; y delay06: add.w #4,SP ; pop AR variables ret ; return from function Chapter 8 - Stacks

  32. Chapter 8 - Stacks

More Related