Chapter 8 – Stacks 1996 1998 1982 1995.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CPU Review and Programming Models CT101 – Computing Systems.
MSP430 Assembly Paul Roper
Chapter 9 TRAP Routines and Subroutines. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 Subroutines.
S04: MSP430 Microarchitecture
Chapter 8 – Stacks BYU CS/ECEn 124Chapter 8 - Stacks2 Topics to Cover… The Stack Subroutines Subroutine Linkage Saving Registers.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Instruction Set Architecture
LC-3 Computer LC-3 Instructions
Execution of an instruction
Chapter 5 The LC-3 LC-3 Computer Architecture Memory Map
RapUp Dynamic Allocation of Memory in C Last HW Exercise Review for Final Final Exam Next Thursday – Same Time / Same Place.
State Machines Used to Design Sequential Circuits.
Blinky Lab 3: Blinky Lab Modify the blinky.asm assembly program to blink the LaunchPad red LED quickly on and off at exactly 10 second intervals. Calculate.
ECE Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
2.4 Interrupts Required:PM: Ch 9.1-7, pgs PM: Ch , pgs PM: Ch , pgs PM: Ch , pgs PM: Ch 11.5, pgs
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
Chapters 4 & 5: LC-3 Computer Architecture Machine Instructions Assembly language Programming in Machine and Assembly Language.
Tannenbaum Machine. Key Features Simple architecture Simple instruction set Focus on –Input -> Higher Level Language –Output -> Machine Language Addressing.
Chapter 6 – MSP430 Micro-Architecture
Architecture of the MSP430 Processor. Central Processing Unit Program Counter (PC) - Contains the address of the next instruction to be executed. The.
Embedded Systems 7763B Mt Druitt College of TAFE
Chapter 10 And, Finally... The Stack. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Stacks A LIFO.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
BYU CS 124Lab 0 - Warm-up Lab1 Lab 0 – Warm-up Lab 1.Acquire a Texas Instruments MSP430 LaunchPad Development Tool. 2.Setup a CS user account. 3.Install.
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Chapter 14 Functions.
Today… Homework #4 due today. Lab 4 – Microarchitecture due Friday. microArch430.exe – Rev 2.1a Download from website Report any problems Questions? BYU.
Objective At the conclusion of this chapter you will be able to:
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture,
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Execution of an instruction
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Lab 4: MSP430 Microarchitecture
The 8051 Microcontroller and Embedded Systems
V 1.01 Arrays and Pointers in C A pointer variable is a variable that contains the address of another variable. An array is a collection of like elements,
9/20/6Lecture 2 - Prog Model Architecture, Data Types and Addressing Modes.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
by Richard P. Paul, 2nd edition, 2000.
ECE 447 Fall 2009 Lecture 4: TI MSP430 Architecture and Instruction Set.
JUMP, LOOP, AND CALL INSTRUCTIONS
Chapter 3 – Instruction Set Architecture. Instruction Length and Cycles.
CPE 323 Introduction to Embedded Computer Systems: The MSP430X Architecture Instructor: Dr Aleksandar Milenkovic.
CPS 4150 Computer Organization Chapter 2-2 Fall 2006 Ching-Song Don Wei.
8085 INTERNAL ARCHITECTURE.  Upon completing this topic, you should be able to: State all the register available in the 8085 microprocessor and explain.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
8 085Microprocessor Temp Reg (8) Accumulator (A reg) Flag flip flops(8) Instruction Register (8) Arithmetic Logic Unit ALU Instruction Decoder and Machine.
F453 Module 8: Low Level Languages 8.1: Use of Computer Architecture.
ECE 3430 – Intro to Microcomputer Systems
ECE 382 Lesson 3 ECE 382Website:
ECE 3430 – Intro to Microcomputer Systems
S04: MSP430 Microarchitecture
Quiz 4.1 Four LEDs are attached to Port 4, bits 0 thru 3. Indicate which LEDs are ON/OFF after each instruction is executed. P4.7 P4.6 P4.5 P4.4 P4.3.
Subroutines and the Stack
8051 Addressing Modes The way, using which the data source or destination addresses are specified in the instruction mnemonic for moving the data, is.
Chapter 10 The Stack.
Arithmetic using a stack
Fundamentals of Computer Organisation & Architecture
ECE 3430 – Intro to Microcomputer Systems
Subroutines and the Stack
Chapter 8 Central Processing Unit
Introduction to Micro Controllers & Embedded System Design
Subroutines and the Stack
Where is all the knowledge we lost with information? T. S. Eliot
Computer Organization and Assembly Language
Computer Operation 6/22/2019.
Lecture 3 - Instruction Set - Al
Presentation transcript:

Chapter 8 – Stacks 1996 1998 1982 1995

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

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

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

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

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

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

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

The Call / Return Mechanism Subroutines The Call / Return Mechanism BYU CS/ECEn 124 Chapter 8 - Stacks

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-2SP, src@SP Push byte/word source on stack CALL dst SP-2SP, PC+2@SP dstPC Subroutine call to destination RETI TOSSR, SP+2SP TOSPC, SP+2SP Return from interrupt Mnemonic Operation Emulation Description RET @SPPC SP+2SP MOV @SP+,PC Return from subroutine POP(.B or .W) dst @SPtemp tempdst MOV(.B or .W) @SP+,dst Pop byte/word from stack to destination BYU CS/ECEn 124 Chapter 8 - Stacks

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

“Calling” a Subroutine Subroutine Linkage “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. CALL #TONI Op-code call b/w 16-bits Ad Immediate D-reg PC 0 0 0 1 0 0 1 0 1 1 1 0 0 0 0 16-bit Destination Address BYU CS/ECEn 124 Chapter 8 - Stacks

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 ; MOV @SP+,PC Op-code mov S-reg SR Ad Register b/w 16-bits As Indirect + D-reg PC 0 1 0 0 0 0 0 1 1 1 0 0 0 0 BYU CS/ECEn 124 Chapter 8 - Stacks

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

Cycles Per Instruction... 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 BYU CS/ECEn 124 Chapter 8 - Stacks

Cycles Per Instruction... Instruction Timing Cycles Per Instruction... Src Dst Cycles Length Example 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 BYU CS/ECEn 124 Chapter 8 - Stacks

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

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: BYU CS/ECEn 124 Chapter 8 - Stacks

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: 600000 – 3 DELAY = ---------- 3008 = 199.467 = 199 BYU CS/ECEn 124 Chapter 8 - Stacks

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

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,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 BYU CS/ECEn 124 Chapter 8 - Stacks

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,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 BYU CS/ECEn 124 Chapter 8 - Stacks

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

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

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

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: 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. PUSH R5 Op-code push b/w 16-bits Ad Register D-reg r5 0 0 0 1 0 0 1 0 0 0 0 0 1 0 1 BYU CS/ECEn 124 Chapter 8 - Stacks

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. MOV @SP+,R5 Op-code mov S-reg SP Ad Register b/w 16-bits As Indirect + D-reg r5 0 1 0 0 0 0 0 1 1 1 0 1 0 1 BYU CS/ECEn 124 Chapter 8 - Stacks

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

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

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

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 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 BYU CS/ECEn 124 Chapter 8 - Stacks

BYU CS/ECEn 124 Chapter 8 - Stacks