ECE 3430 – Intro to Microcomputer Systems

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

Code Composer Department of Electrical and Computer Engineering
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
9/20/6Lecture 3 - Instruction Set - Al Instruction Set.
Lecture 6 Machine Code: How the CPU is programmed.
CSC Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The.
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.
EECC250 - Shaaban #1 Lec # 5 Winter Stacks A stack is a First In Last Out (FILO) buffer containing a number of data items usually implemented.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Chapter 9 & 10 Subroutines and Interrupts. JSR Instruction: JSR offset (11 bit) xxxxxxxxxxx [PC ]  R7, JMP Offset Jump to Subroutine at offset.
ECE 382 Lesson 2 Readings Lesson Outline Admin Assembler Linker
Chapter 9 Trap Routines TRAP number (go to service routine) & RET (return from service routine) Subroutines (or Functions) JSR offset or JSRR rn (go to.
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
ARM Core Architecture. Common ARM Cortex Core In the case of ARM-based microcontrollers a company named ARM Holdings designs the core and licenses it.
9/20/6Lecture 21 -PIC Architecture1 PIC Architecture Programmers Model and Instruction Set.
CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack.
Stacks and Subroutines ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
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.
© 2009, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction  Purpose:  This course provides an overview of the SH-2 32-bit RISC.
Spring EE 437 Lillevik 437s06-l8 University of Portland School of Engineering Advanced Computer Architecture Lecture 8 Project 3: memory agent Programmed.
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
Chapter 2-4 Subroutines and Stacks Subroutines Parameter Passing Stack Frame Additional Instructions Next Lecture   Chapter 3: ARM Instruction Set Architecture.
3-1 EE 319K Introduction to Microcontrollers Lecture 3: Addressing modes, Memory Operations, Subroutines, I/O, Logical/Shift Operations.
Lecture Set 4 Programming the 8051.
Functions/Methods in Assembly
ECE 447 Fall 2009 Lecture 4: TI MSP430 Architecture and Instruction Set.
© 2010 Kettering University, All rights reserved..
EE/CS-352: Embedded Microcontroller Systems Part V The 8051 Assembly Language Interrupts.
Subroutines and Stacks. Stack The stack is a special area in memory used by the CPU to store register information or general data information during program.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
The Stack. ARMSim memory space: 0x Unused 0x x11400 Stack 0x x09400 Heap 0x?????-0x01400 Data 0x x????? Text 0x x01000.
Section 5: Procedures & Stacks
Chapter 14 Functions.
Programmable System on Chip
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
Prof. Hsien-Hsin Sean Lee
ECE 3430 – Intro to Microcomputer Systems
Format of Assembly language
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
Chapter 11 Instruction Sets
The Stack.
S04: MSP430 Microarchitecture
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
The Stack.
Subroutines and the Stack
Microprocessor and Assembly Language
ECE 3430 – Intro to Microcomputer Systems
Microcomputer Programming
CS 301 Fall 2002 Control Structures
ECE 3430 – Intro to Microcomputer Systems
LC-3 Details and Examples
Memory organization On- chip memory Off-chip memory
ECE 3430 – Intro to Microcomputer Systems
Subroutines and the Stack
ECE 3430 – Intro to Microcomputer Systems
68000 Architecture, Data Types and Addressing Modes
ECE 3430 – Intro to Microcomputer Systems
Chapter 10 And, Finally... The Stack
Chapter 9 TRAP Routines and Subroutines
ECE 3430 – Intro to Microcomputer Systems
Subroutines and the Stack
COMP3221: Microprocessors and Embedded Systems
Some Assembly (Part 2) set.html.
Computer Organization
Presentation transcript:

ECE 3430 – Intro to Microcomputer Systems ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs Lecture #12 Agenda Today 1) Subroutines - Parameter Passing Techniques - “Do No Damage” Paradigm Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2014

ECE 3430 – Intro to Microcomputer Systems Subroutines Subroutines - Sub-sections of code that perform a specific task. - Modular design flow. - The main program loop contains logical structure of the program. - The subroutines perform the details. - Program design can be divided between multiple engineers. - Subroutines can exist anywhere in memory—but typically they are defined after the main program loop. Memory 0xC000 : : : Main Subroutine 1 Subroutine 2 Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2014

Subroutines Subroutine Call - The MSP430 provides an instruction for subroutine calls: CALL #<label> ; or any other addressing mode - When this instruction is executed, the following happens: 1) The 16-bit return address is pushed onto the stack (little endian). The “return address” is the address of the instruction immediately following the CALL instruction. 2) The program counter (PC) is loaded with the address of the beginning of the subroutine (the address for the subroutine label). 0x03FC 0x03FE 0x03FF XX Return LOW SP after CALL completes Return HIGH Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2014

ECE 3430 – Intro to Microcomputer Systems Subroutines Subroutine Return - The MSP430 provides the following instruction to return from a subroutine: RET ; return from subroutine - When this instruction is executed, the following happens: 1) The return address is pulled off of the stack (little endian byte ordering). 2) The program counter (PC) is loaded with the address that was pulled off of the stack. 0x03FE 0x03FF 0x0400 Return LOW Return HIGH SP after RET completes XX Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2014

Subroutines Subroutine example ORG 0xF82E Main: call #MySub Done: jmp Done MySub: mov.b #0xFF,R7 ret END What address is pushed on the stack when CALL is called? Label Addr Data INST Main: 0xF82E 0x12B0 call #MySub 0xF830 0xF834 - Done: 0xF832 0x3FFF jmp Done MySub: 0xF834 0x4377 mov.b #0FFh,R7 0xF836 0x4130 ret Note use of CG with -1. Also that “ret” is actually a “mov” (emulated). Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2014

Subroutines Subroutine example ORG 0xF82E Main: call #MySub Done: jmp Done MySub: mov.b #0FFh,R7 ret END What address is pushed on the stack when CALL is called? 0xF832 Label Addr Data INST Main: 0xF82E 0x12B0 call #MySub 0xF830 0xF834 - Done: 0xF832 0x3FFF jmp Done MySub: 0xF834 0x4377 mov.b #0FFh,R7 0xF836 0x4130 ret Note use of CG with -1. Also that “ret” is actually a “mov” (emulated). 0x03FE 0x03FF 0x0400 SP after CALL instruction completes 0x32 0xF8 XX Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2014

Subroutines – Parameter Passing Passing Parameters to Subroutines Parameters can provide input info to a subroutine or receive output info from a subroutine. 1) Use registers - Registers provide additional info to the subroutine. 2) Use the stack - Values pushed on the stack provide additional info to the 3) Use global memory - Reserved memory locations provide additional info to the Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2014

Subroutines – Do No Damage Preserving the Programming Model (Do No Damage Paradigm) If not using registers to pass parameters, it is imperative that subroutines do not inadvertently alter the programming model. In other words, the contents of R4-R15 should be the same before and after calling a subroutine. Because R0-R3 have special purposes, we’ll ignore those. SUB1: push R4 ; do no damage: push everything this subroutine uses push R5 mov.b &P1IN,R4 mov.b &P1IN,R5 add.w R4,R5 mov.w R5,0x0200h pop R5 ; damage no do: pull everything that was previously pushed pop R4 ret This becomes particularly important when subroutines are calling other subroutines! Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2014

Subroutines – Register/Accumulator Parameter Passing Ex 1) Using registers: Use R4 for info exchange, output to port 1: ORG 0xC000 Main: mov.b #10,R4 call #Out1 Done: jmp Done ; Subroutine: Output value to port 1 Out1: <do push operations> ; do-no-damage mov.b R4,&P1OUT <do pop operations> ; damage-no-do ret Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2014

Subroutines – Stack Parameter Passing Ex 2) Using the stack: Exchange data via stack location (register use is arbitrary), output to port 1: ORG 0xC000 Main: mov.b #10,R4 push R4 ; place input parameter on stack call #Out1 pop R4 ; remove input parameter from stack Done: jmp Done ; Subroutine: Output value to port 1 Out1: push R4 ; do-no-damage push R5 mov.w #55AAh,R4 ; arbitrary op with R4 & R5 mov.w #4455h,R5 ; necessitates pushes/pops <do something with R4 and R5> mov.b 6(SP),&P1OUT ; fetch passed-in value (relative to SP) pop R5 ; damage-no-do pop R4 ret Stack 0x03F8 R5 top 0x03FA R4 0x03FC Ret addr 0x03FE Input Val = 10 bottom Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2014

Subroutines – Global Memory Parameter Passing Ex 3) Using global memory: Exchange info via specific RAM location, output to port 1: ORG 0x0200 Outval: DS 1 ORG 0xC000 Main: mov.b #10,Outval call #Out1 Done: jmp Done ; Subroutine: Output value to port 1 Out1: <do push operations> ; do-no-damage mov.b Outval, &P1OUT ; fetch parameter (hard-coded address) <do pop operations> ; damage-no-do ret Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2014