1 / 11

ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs

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. Subroutines.

briceno
Download Presentation

ECE 3430 – Introduction to Microcomputer Systems University of Colorado at Colorado Springs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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 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). 0x03FC0x03FE0x03FF XX Return LOW SP after CALL completes Return HIGH ECE 3430 – Intro to Microcomputer Systems Fall 2014

  4. 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. 0x03FE0x03FF0x0400 Return LOW Return HIGH SP after RET completes XX ECE 3430 – Intro to Microcomputer Systems Fall 2014

  5. Subroutines Subroutine exampleORG 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? LabelAddrDataINSTMain: 0xF82E 0x12B0 call #MySub 0xF830 0xF834 -Done: 0xF832 0x3FFF jmp DoneMySub: 0xF834 0x4377 mov.b #0FFh,R7 0xF836 0x4130 ret Note use of CG with -1. Also that “ret” is actually a “mov” (emulated). ECE 3430 – Intro to Microcomputer Systems Fall 2014

  6. Subroutines Subroutine exampleORG 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 LabelAddrDataINSTMain: 0xF82E 0x12B0 call #MySub 0xF830 0xF834 -Done: 0xF832 0x3FFF jmp DoneMySub: 0xF834 0x4377 mov.b #0FFh,R7 0xF836 0x4130 ret Note use of CG with -1. Also that “ret” is actually a “mov” (emulated). 0x03FE0x03FF0x0400 SP after CALL instruction completes 0x32 0xF8 XX 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 subroutine. 3) Use global memory - Reserved memory locations provide additional info to the subroutine. 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! 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 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 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 top 0x03F8 R5 0x03FA R4 0x03FC Ret addr 0x03FE Input Val = 10 bottom 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.bOutval, &P1OUT ; fetch parameter (hard-coded address) <do pop operations> ; damage-no-do ret ECE 3430 – Intro to Microcomputer Systems Fall 2014

More Related