ECE 3430 – Intro to Microcomputer Systems

Slides:



Advertisements
Similar presentations
Passing by-value vs. by-reference in ARM by value C code equivalent assembly code int a;.section since a is not assigned an a:.skip initial.
Advertisements

Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Instruction Set Architecture Classification According to the type of internal storage in a processor the basic types are Stack Accumulator General Purpose.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Utilizing the GDB debugger to analyze programs Background and application.
TK 2633 Microprocessor & Interfacing
ARM C Language & Assembler. Using C instead of Java (or Python, or your other favorite language)? C is the de facto standard for embedded systems because.
Cortex-M3 Programming. Chapter 10 in the reference book.
Introduction to Embedded Systems
Stacks and Subroutines ELEC 330 Digital Systems Engineering Dr. Ron Hayne Images Courtesy of Ramesh Gaonkar and Delmar Learning.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
Lecture 4: Advanced Instructions, Control, and Branching cont. EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
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,
Chapter 2-4 Subroutines and Stacks Subroutines Parameter Passing Stack Frame Additional Instructions Next Lecture   Chapter 3: ARM Instruction Set Architecture.
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
CPS 4150 Computer Organization Chapter 2-2 Fall 2006 Ching-Song Don Wei.
The Stack. ARMSim memory space: 0x Unused 0x x11400 Stack 0x x09400 Heap 0x?????-0x01400 Data 0x x????? Text 0x x01000.
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.
Writing Functions in Assembly
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.
Computer Science 210 Computer Organization
Lecture 5: Procedure Calls
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
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
ARM Registers Register – internal CPU hardware device that stores binary data; can be accessed much more rapidly than a location in RAM ARM has.
© Craig Zilles (adapted from slides by Howard Huang)
ECE 3430 – Intro to Microcomputer Systems
The Cortex-M3/m4 Embedded Systems: Cortex-M3/M4 Instruction Sets
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
The Stack.
Advanced Topic: Alternative Architectures Chapter 9 Objectives
Lab 3 - Branching & Subroutines
Microprocessor and Assembly Language
Chapter 4 Addressing modes
Chapter 10 And, Finally... The Stack
Introduction to Compilers Tim Teitelbaum
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
Writing Functions in Assembly
Chapter 10 The Stack.
Chapter 9 :: Subroutines and Control Abstraction
ECE 3430 – Intro to Microcomputer Systems
Chap. 8 :: Subroutines and Control Abstraction
Passing by-value vs. by-reference in ARM
Stack Frame Linkage.
ECE 3430 – Intro to Microcomputer Systems
Chapter 8 Central Processing Unit
by Richard P. Paul, 2nd edition, 2000.
Md. Mojahidul Islam Lecturer Dept. of Computer Science & Engineering
Chapter 10 And, Finally... The Stack
Md. Mojahidul Islam Lecturer Dept. of Computer Science & Engineering
Multi-modules programming
8051 ASSEMBLY LANGUAGE PROGRAMMING
Process.
Where is all the knowledge we lost with information? T. S. Eliot
Multiply Instructions
© Craig Zilles (adapted from slides by Howard Huang)
Introduction to Assembly Chapter 2
An Introduction to the ARM CORTEX M0+ Instructions
(The Stack and Procedures)
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 2015

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 Main Subroutine 1 Subroutine 2 Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2015

ECE 3430 – Intro to Microcomputer Systems Subroutines Subroutine Call - The ARM CPU provides an instruction for subroutine calls: BL <label> ; where label is the subroutine name - When this instruction is executed, the following happens: 1) The 32-bit return address is latched into the link register (LR). The least-significant bit is set to reflect the current state of the CPU (Thumb vs ARM mode). 2) The program counter (PC) is loaded with the address of the beginning of the subroutine (the address for the subroutine label). Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2015

ECE 3430 – Intro to Microcomputer Systems Subroutines Subroutine Return - The ARM CPU provides the following instruction to return from a subroutine: BX LR ; return from subroutine - BX stands for “branch indirect”. LR holds the address to branch (return) to. - When this instruction is executed, the following happens: 1) The return address in LR is copied to the PC (forcing bit 0 to 0) 2) The least-significant bit of LR indicates the state of the ‘T’ bit in the PSR. Note that the stack is not used in the ARM CPU for single-level subroutine calls. The stack is only possibly required if a subroutine needs to call another subroutine. In the latter, the LR must be pushed prior to the next BL instruction. Upon return, the old LR value must be restored. The LR can be preserved using other CPU registers too. Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2015

ECE 3430 – Intro to Microcomputer Systems Subroutines Subroutine example Startup …. BL MySub Done B Done MySub … BX LR END What would happen if the main code above was using registers R0 and R1 and the subroutine decided to use R0 and R1 too? CRASH/BURN! Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2015

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 R0-R12 should be the same before and after calling a subroutine. Because R13-R15 have special purposes, we’ll ignore those. MySub PUSH {R4, R5} ; do no damage: push everything this subroutine uses MOV R4, #0 MOV R5, #0xAB ADD R4, R5 … POP {R4,R5} ; damage no do: pull everything that was previously pushed BX LR This becomes particularly important when subroutines are calling other subroutines! Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2015

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 2015

Subroutines – Register/Accumulator Parameter Passing Ex 1) Using registers: Use R4 for info exchange, output to port 1: Startup LDR R4, =0x10 ; hand the value 0x10 to the subroutine BL Out1 Done B Done ; Subroutine: Output value to port 1 Out1 <do push operations> ; do-no-damage LDR R5,=P1OUT STRB R4, [R5] <do pop operations> ; damage-no-do BX LR Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2015

Subroutines – Stack Parameter Passing Ex 2) Using the stack: Exchange data via stack location (register use is arbitrary), output to port 1: Startup LDR R4, #0x10 PUSH {R4} ; place input parameter on stack BL Out1 POP {R4} ; remove input parameter from stack Done B Done ; Subroutine: Output value to port 1 Out1: PUSH {R4} ; do-no-damage PUSH {R5} LDR R4, [SP, #8] ; get input param LDR R5, =P1OUT ; load P1OUT address STRB R4, [R5] ; store input param to port 1 POP {R5} ; damage-no-do POP {R4} BX LR Stack X R5 top X+4 R4 Input Val = 0x10 X+8 bottom Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2015

Subroutines – Global Memory Parameter Passing Ex 3) Using global memory: Exchange info via specific RAM location, output to port 1: ; Global memory Outval SPACE 1 ; Code space Startup LDR R5,=Outval MOV R4, #0x10 STRB R4, [R5] BL Out1 Done B Done ; Subroutine: Output value to port 1 Out1: <do push operations> ; do-no-damage LDR R6,=Outval ; load address of global LDRB R4,[R6] ; load the value of the global LDR R5,=P1OUT ; load destination address STRB R4,[R5] ; store the global to port 1 <do pop operations> ; damage-no-do BX LR Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2015

Subroutines – Parameter Passing Each approach has pros and cons. Register parameter passing is the fastest—but this puts restrictions on what registers can be used to pass a series of parameters. To use a subroutine, you may have to shuffle data around. Stack parameter passing is the most flexible—as any available registers in the programming model can be used. Subroutines using this approach don’t care what registers are used. But parameter access involves external memory access—so slow. Global memory parameter passing is intuitive and easy—but this approach locks you into fixed addresses which may not be available on a different implementation. Global memory is not thread-safe. In operating systems where multiple threads are running, use of global memory between the caller and callee prevents thread parallelism. Register and stack parameter passing, on the other hand, are inherently thread-safe. Your C/C++ compiler will always use register or stack parameter passing to pass function parameters and return values. Never global. However, global variables are critically important for certain other applications (like interrupts which we’ll discuss later). Lecture #12 ECE 3430 – Intro to Microcomputer Systems Fall 2015