Download presentation
1
Chapter 8 – Stacks 1996 1998 1982 1995
2
Topics to Cover… The Stack Subroutines Subroutine Linkage
Instruction Timing Stoplight Example Saving Registers Stack Operations Recursive Subroutines Activation Records BYU CS/ECEn 124 Chapter 8 - Stacks
3
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
4
Stacks Stacks are the fundamental data structure of computers today
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 BYU CS/ECEn 124 Chapter 8 - Stacks
5
MSP430 Stack Hardware support for stack
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 BYU CS/ECEn 124 Chapter 8 - Stacks
6
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 BYU CS/ECEn 124 Chapter 8 - Stacks
7
Computer Memory – Up or Down?
The Stack Computer Memory – Up or Down? x0000 xFFFF Up Down Down Up xFFFF x0000 BYU CS/ECEn 124 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. BYU CS/ECEn 124 Chapter 8 - Stacks
9
The Call / Return Mechanism
Subroutines The Call / Return Mechanism BYU CS/ECEn 124 Chapter 8 - Stacks
10
Stack Operations Single operand instructions: Emulated instructions:
Subroutine Linkage Stack Operations Single operand instructions: Emulated instructions: Mnemonic Operation Description PUSH(.B or .W) src SP-2SP, Push byte/word source on stack CALL dst SP-2SP, dstPC Subroutine call to destination RETI TOSSR, SP+2SP TOSPC, SP+2SP Return from interrupt Mnemonic Operation Emulation Description RET @SPPC SP+2SP Return from subroutine POP(.B or .W) dst @SPtemp tempdst MOV(.B or Pop byte/word from stack to destination BYU CS/ECEn 124 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. BYU CS/ECEn 124 Chapter 8 - Stacks
12
“Calling” a Subroutine
Subroutine Linkage “Calling” a Subroutine Push PC+2 on the stack and move the source to the PC: Instruction code: 0x 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. CALL #TONI Op-code call b/w 16-bits Ad Immediate D-reg PC 1 1 16-bit Destination Address BYU CS/ECEn 124 Chapter 8 - Stacks
13
Return from Subroutine
Subroutine Linkage 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. RET ; Op-code mov S-reg SR Ad Register b/w 16-bits As Indirect + D-reg PC 1 1 BYU CS/ECEn 124 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. BYU CS/ECEn 124 Chapter 8 - Stacks
15
Cycles Per Instruction...
Instruction Timing Cycles Per Instruction... Instruction timing: 1 cycle to fetch instruction word +1 cycle if 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 BYU CS/ECEn 124 Chapter 8 - Stacks
16
Cycles Per Instruction...
Instruction Timing Cycles Per Instruction... Src Dst Cycles Length Example Rn Rm MOV R5,R8 @Rm MOV x(Rm) ADD R5,4(R6) EDE XOR R8,EDE &EDE MOV R5,&EDE #n x(Rm) MOV #100,TAB(R8) &TONI &EDE MOV &TONI,&EDE BYU CS/ECEn 124 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: BYU CS/ECEn 124 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 r ; 1 cycle jn delay ; 2 cycles mov.w #1000,r15 ; 3 cycles delay2: dec.w r ; 3 x 1000 jne delay ; = 3000 cycles jmp delay ; 2 cycles delay3: BYU CS/ECEn 124 Chapter 8 - Stacks
19
Quiz (answer) Known equates: Delay time in cycles: Hence:
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: – 3 DELAY = 3008 = = 199 BYU CS/ECEn 124 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. BYU CS/ECEn 124 Chapter 8 - Stacks
21
Stoplight Lab Stoplight Example
;*********************************************************************** ; 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,r ; use R15 as delay counter delayloop: dec.w r ; delay over? jnz delayloop ; n jmp mainloop ; y .sect ".reset" ; MSP430 RESET Vector .short RESET ; start address .end BYU CS/ECEn 124 Chapter 8 - Stacks
22
Stoplight Lab Stoplight Example
.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,r ; get delay count delay02: dec.w r ; done? jeq delay ; y, return mov.w #0,r ; n, use R15 as delay counter delay04: dec.w r ; delay over? jnz delay ; n jmp delay ; y delay06: ret .sect ".reset" ; MSP430 RESET Vector .short RESET ; start address .end BYU CS/ECEn 124 Chapter 8 - Stacks
23
Saving and Restoring Registers
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. BYU CS/ECEn 124 Chapter 8 - Stacks
24
Caller-Save vs. Callee-Save
Saving Registers Caller-Save vs. Callee-Save call subroutine subroutine Save Registers Restore Registers call subroutine Save Registers subroutine Restore Registers BYU CS/ECEn 124 Chapter 8 - Stacks
25
Stack Operations Single operand instructions: Emulated instructions:
Mnemonic Operation Description PUSH(.B or .W) src SP-2SP, Push byte/word source on stack CALL dst SP-2SP, dstPC Subroutine call to destination RETI TOSSR, SP+2SP TOSPC, SP+2SP Return from interrupt Mnemonic Operation Emulation Description RET @SPPC SP+2SP Return from subroutine POP(.B or .W) dst @SPtemp tempdst MOV(.B or Pop byte/word from stack to destination BYU CS/ECEn 124 Chapter 8 - Stacks
26
Push Operand Push the contents of register R5 on the stack:
Stack Operations Push Operand Push the contents of register R5 on the stack: Instruction code: 0x 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. PUSH R5 Op-code push b/w 16-bits Ad Register D-reg r5 0 0 BYU CS/ECEn 124 Chapter 8 - Stacks
27
Pop Operand Pop the stack into the destination:
Stack Operations 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. Op-code mov S-reg SP Ad Register b/w 16-bits As Indirect + D-reg r5 1 1 BYU CS/ECEn 124 Chapter 8 - Stacks
28
Recursive Subroutines
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! BYU CS/ECEn 124 Chapter 8 - Stacks
29
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 BYU CS/ECEn 124 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 BYU CS/ECEn 124 Chapter 8 - Stacks
31
Example Example activation record (6 bytes): 4(SP) Return address
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 delay ; y, return mov.w #0,(SP) ; n, init delay counter delay04: dec.w 0(SP) ; delay over? jnz delay ; n jmp delay ; y delay06: add.w #4,SP ; pop AR variables ret ; return from function BYU CS/ECEn 124 Chapter 8 - Stacks
32
BYU CS/ECEn 124 Chapter 8 - Stacks
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.