Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 3430 – Intro to Microcomputer Systems

Similar presentations


Presentation on theme: "ECE 3430 – Intro to Microcomputer Systems"— Presentation transcript:

1 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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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


Download ppt "ECE 3430 – Intro to Microcomputer Systems"

Similar presentations


Ads by Google