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 2014

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

3 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

4 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

5 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 0xF 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

6 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 0xF 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

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 2014

8 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

9 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

10 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 R ; place input parameter on stack call #Out1 pop R ; 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,R ; 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 R ; 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

11 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


Download ppt "ECE 3430 – Intro to Microcomputer Systems"

Similar presentations


Ads by Google