2.4 Interrupts Required:PM: Ch 9.1-7, pgs 129-139 PM: Ch 10.1-2, pgs 151-156 PM: Ch 10.4-5, pgs 159-166 PM: Ch 10.7-8, pgs 169-188 PM: Ch 11.5, pgs 227-229.

Slides:



Advertisements
Similar presentations
Computer Architecture
Advertisements

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.
1 System Calls (TRAPS) and Subroutines Patt and Patel Ch. 9.
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.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Interrupts Chapter 8 – pp Chapter 10 – pp Appendix A – pp 537 &
A look at interrupts What are interrupts and why are they needed.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
COMP3221: Microprocessors and Embedded Systems--Lecture 8 1 COMP3221: Microprocessors and Embedded Systems Lecture 8: Program Control Instructions
CSS 372 Lecture 1 Course Overview: CSS 372 Web page Syllabus Lab Ettiquette Lab Report Format Review of CSS 371: Simple Computer Architecture Traps Interrupts.
Computer Organization and Architecture The CPU Structure.
Chapter 5 The LC-3 LC-3 Computer Architecture Memory Map
Run time vs. Compile time
RapUp Dynamic Allocation of Memory in C Last HW Exercise Review for Final Final Exam Next Thursday – Same Time / Same Place.
A look at interrupts What are interrupts and why are they needed.
CSS 372 Oct 2 nd - Lecture 2 Review of CSS 371: Simple Computer Architecture Chapter 3 – Connecting Computer Components with Buses Typical Bus Structure.
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.
Chapter 6 – MSP430 Micro-Architecture
Required:PM: Ch 8.1-3, pgs Recommended:Wiki: Microarchitecture Wiki: Addressing_mode Wiki: Three-state logicWiki: Microarchitecture Wiki: Addressing_mode.
Embedded Systems 7763B Mt Druitt College of TAFE
System Calls 1.
CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack.
Chapter 8 – Stacks
Chapter 10 And, Finally... The Stack. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Stacks A LIFO.
A Simple Tour of the MSP430. Light LEDs in C LEDs can be connected in two standard ways. Active high circuit, the LED illuminates if the pin is driven.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
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.
Chapter 5 The LC Instruction Set Architecture ISA = All of the programmer-visible components and operations of the computer memory organization.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Lab 4: MSP430 Microarchitecture
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,
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.
Functions/Methods in Assembly
ECE 447 Fall 2009 Lecture 4: TI MSP430 Architecture and Instruction Set.
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.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Chapter 14 Functions.
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
S04: MSP430 Microarchitecture
Chapter 10 The Stack.
Chapter 5 The LC-3.
ECE 3430 – Intro to Microcomputer Systems
Fundamentals of Computer Organisation & Architecture
ECE 3430 – Intro to Microcomputer Systems
Subroutines and the Stack
Chapter 8 Central Processing Unit
Chapter 9 TRAP Routines and Subroutines
COMP3221: Microprocessors and Embedded Systems
Where is all the knowledge we lost with information? T. S. Eliot
Computer Organization and Assembly Language
Lecture 3 - Instruction Set - Al
Presentation transcript:

2.4 Interrupts Required:PM: Ch 9.1-7, pgs PM: Ch , pgs PM: Ch , pgs PM: Ch , pgs PM: Ch 11.5, pgs Recommended:Intro to PWM FUG: Watchdog Timer+Intro to PWM FUG: Watchdog Timer+

BYU CS 124Stacks2 Topics to Cover… The Stack Subroutines Subroutine Linkage Saving Registers Stack Operations Activation Records Recursive Subroutines Interrupt Stack Usage

BYU CS 124Stacks3 Terms… Activation Record – parameters activated on the stack by a subroutine call Callee-Safe – subroutine saves registers used. Caller-Safe – caller saves registers needing to be saved. FIFO – First In First Out, a stack. Interrupt – asynchronous subroutine call. Loosely Coupled – all parameters passed as arguments. Pop – removing top element of stack. Push – putting an element on a stack Stack – first in, first out abstract storage data structure. Stack Pointer – address of stack. Stong Cohesion – subroutine performs one specific task. Subroutine – synchronous task or unit.

BYU CS 124Stacks4 Levels of Transformation Problems Algorithms Language (Program) Machine (ISA) Architecture Microarchitecture Circuits Devices Programmable Computer Specific Manufacturer Specific

BYU CS 124Stacks5 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. The Stack

BYU CS 124Stacks6 MSP430 Stack Hardware support for stack Register R1 – Stack Pointer (SP) Initialized to highest address of available RAM MSP430G2553  0x0400 (512 bytes) MSP430F2274  0x0600 (1k bytes) Stack grows down towards lower memory addresses. Initialize stack pointer at beginning of program STACK.equ 0x0400 ; top of stack start: mov.w #STACK,SP ; init stack pointer The Stack

BYU CS 124Stacks7 MSP430 Stack The MSP430 stack is a word structure 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 PUSH – push a value on the stack POP – pop a value off the stack CALL – push a return address on the stack RET – pop a return address off the stack RETI –pop a return address and status register off the stack (Chapter 6) Interrupts – push a return address and status register on the stack (Chapter 6) The Stack

BYU CS 124Stacks8 Computer Memory – Up or Down? x0000 xFFFF Up Down xFFFF x0000 Down Up The Stack Unlike a coin stack, a memory stack DOES NOT move the Data in memory, just the pointer to the top of stack.  Stack  TOS

BYU CS 124Stacks9 Quiz List the values found in the stack and the value of the stack pointer after each instruction. Push #0x0018Push #0x0025 Push #0x0058 Pop R15Push #0036 0x0282 0x0280 0x027E 0x027C 0x027A 0x0278 x0280 R1 TOP Instructions:

BYU CS 124Stacks10 Subroutines A subroutine is a program fragment that performs some useful function. 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 Smaller programs are easier to maintain. Reduces development costs while increasing reliability. Fewer bugs – copying code repeats bugs. Subroutines are often collected into libraries. Subroutines

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

BYU CS 124Stacks12 Subroutine Linkage A subroutine is “called” in assembly using the MSP430 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. Subroutine Linkage

BYU CS 124Stacks13 Quiz 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#0x0400,SP mov.w#WDTPW+WDTHOLD,&WDTCTL bis.b#0x01,&P1DIR; P1.0 as output mainloop:bis.b#0x01,&P1OUT; turn on LED push#1000 call#delay bic.b#0x01,&P1OUT; turn off led call#delay jmpmainloop delay:mov.w2(sp),r15; get delay counter delaylp2:sub.w#1,r15; delay over? jnzdelaylp2; n ret; y.sect".reset"; reset Vector.wordstart; start address.end

BYU CS 124Stacks14 Stack Operations Single operand instructions: Emulated instructions: MnemonicOperationDescription PUSH(.B or.W ) src SP-2  SP, src Push byte/word source on stack CALL dst dst  tmp,SP-2  SP, PC tmp  PC Subroutine call to destination RETI TOS  SR, SP+2  SP TOS  PC, SP+2  SP Return from interrupt MnemonicOperationEmulationDescription  PC SP+2  SP from subroutine POP(.B or.W )  temp SP+2  SP temp  dst MOV(.B or.W Pop byte/word from stack to destination Subroutine Linkage

func Memory BYU CS 124 Stacks15 Call Registers CPU Call Instruction call #func ; M(--sp) = PC; PC = M(func) PC IR Data Bus (1 cycle) 0x12b0 fffe (+1 cycle) SP Address Bus SP 0x4130 Data Bus (+1 cycle) (+1 cycle) PC ALU ADDER PC SP 0x801e Address Bus +2

BYU CS 124Stacks16 Subroutine Call CALLSubroutine SyntaxCALL dst Operationdst  tmp (SP−2)  SP PC tmp  PC DescriptionA subroutine call is made to an address anywhere in the 64K 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 BitsStatus bits are not affected. Example Subroutine Linkage

BYU CS 124Stacks17 CALL Examples CALL #EXEC; Call on label EXEC or immediate address (e.g. #0A4h) → tmp, SP−2 → SP, PC tmp → PC CALL EXEC; Call on the address contained in EXEC ; X(PC)→tmp, PC+2→PC, SP−2→SP, tmp→PC CALL &EXEC; Call on the address contained in absolute address EXEC ; X(0)→tmp, PC+2→PC, SP−2→SP, tmp→PC CALL R5; Call on the address contained in R5 ; R5→tmp, SP−2→SP, tmp→PC Call on the address contained in the word pointed to by R5 SP−2→SP, tmp→PC Call on the address contained in the word pointed to by R5 ; and increment pointer in R5. SP−2→SP, tmp→PC CALL X(R5); Call on the address contained in the address pointed to by ; R5 + X (e.g. table with address starting at X) ; X can be an address or a label ; X(R5)→tmp, PC+2→PC, SP−2→SP, tmp→PC Subroutine Linkage

BYU CS 124Stacks18 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. Subroutine Linkage

Memory BYU CS 124 Stacks19 Return Registers ALU CPU ADDER Return Instruction ret ; 0x801e PC IR 0x12b SP Address Bus (+1 cycle) Data Bus (+1 cycle) 0x4130 Data Bus (+1 cycle) 0x4130 PC Address Bus SP PC SP PC +2

BYU CS 124Stacks20 Return from Subroutine RETReturn from subroutine SyntaxRET PC SP + 2 → SP DescriptionThe 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 BitsStatus bits are not affected. Example Subroutine Linkage

BYU CS 124Stacks21 Quiz What is wrong (if anything) with the following code? 2.How many times will delay be called for each loop? 3.How long will myDelay delay? loop: call #myDelay jmp loop myDelay: mov.w #0,r15 call #delay delay: sub.w #1,r15 jne delay ret

BYU CS 124Stacks22 Saving and Restoring Registers Called routine -- “callee-save” 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. Calling routine -- “caller-save” 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. Saving Registers

BYU CS 124Stacks23 Caller-Save vs. Callee-Save call subroutine Save Registers subroutine Restore Registers call subroutine subroutine Save Registers Restore Registers Saving Registers

BYU CS 124Stacks24 Stack Operations Single operand instructions: Emulated instructions: MnemonicOperationDescription PUSH(.B or.W ) src SP-2  SP, src Push byte/word source on stack CALL dst dst  tmp,SP-2  SP, PC tmp  PC Subroutine call to destination RETI TOS  SR, SP+2  SP TOS  PC, SP+2  SP Return from interrupt MnemonicOperationEmulationDescription  PC SP+2  SP from subroutine POP(.B or.W )  temp SP+2  SP temp  dst MOV(.B or.W Pop byte/word from stack to destination Stack Operations

SP r15 r14 0xf826 SP BYU CS 124Stacks25 Stack Operations Subroutine Linkage 0xf820:... 0xf822: call #subroutine 0xf826:... subroutine: 0xf852: push r15 0xf854: push r xf882: pop r14 0xf884: pop r15 0xf886: ret 0400: 03fe: 03fc: 03fa: 03f8: 03f6: 03f4: 03f2: Unprotected! 0xf826 SP r15 0xf826 SP r15 r14 0xf826 SP r15 r14 0xf826 SP r14 r15 0xf826 SP r14 r15 0xf826 SP

BYU CS 124Stacks26 Activation Records A subroutine is activated when called and 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. 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). Activation Records

BYU CS 124Stacks27.cdecls C,"msp430.h" ; MSP430 DELAY.equ (50/8).text ; beginning of code reset: mov.w #0x0400,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 push.w #DELAY ; pass delay count on stack call #delay ; call delay subroutine jmp mainloop ; delay subroutine: stack usage 4| DELAY | \ ; 2| ret | subroutine frame (6 bytes) ; (SP) => 0| r15 | / delay: push.w r15 ; callee-save mov.w #0,r15 ; use R15 as inner counter delay02: sub.w #1,r15 ; inner delay over? jne delay02 ; n sub.w #1,4(SP) ; y, outer done? jne delay02 ; n pop.w r15 ; y, restore register(s) ; pop input delay count ret ; return from subroutine.sect ".reset" ; MSP430 reset Vector.word reset ; start address.end Activation Record Example Delay Activation Record: 4(SP) = delay count 2(SP) = return address 0(SP) = r15 Activation Records Stack: 2(SP) = delay count 0(SP) = return address Stack: 0(SP) = return address Stack: (emply)

BYU CS 124Stacks28 Quiz Change the following code to use a callee-save, loosely coupled, cohesive subroutine..cdeclsC,"msp430.h".text start:mov.w#0x0400,SP mov.w#WDTPW+WDTHOLD,&WDTCTL bis.b#0x01,&P1DIR; P1.0 as output mainloop:bis.b#0x01,&P1OUT; turn on LED mov.w#10000,r15; delay counter delaylp1:sub.w#1,r15; delay over? jnzdelaylp1; n bic.b#0x01,&P1OUT; turn off led mov.w#0,r15; delay counter delaylp2:sub.w#1,r15; delay over? jnzdelaylp2; n mov.w#0,r15; delay counter delaylp3:sub.w#1,r15; delay over? jnzdelaylp3; n jmpmainloop; y, toggle led.sect".reset"; reset vector.wordstart; start address.end

BYU CS 124Stacks29 Recursive Subroutine 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 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! Recursive Subroutines

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

BYU CS 124Stacks31