6 - BME 303 Lecture 17 Subroutines Begin I/O Remember:...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
1 BME 303 Lecture 17 • Subroutines • Begin I/O • Remember: Return papers with grading issues (exam #1 bonus points, HW #2 issues mentioned last time).
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Subroutines • A subroutine (also called, depending on context, a procedure or a function) is a program fragment that: – performs a well-defined task – is invoked (called) by another user program – returns control to the calling program when finished • Reasons for subroutines: – reuse useful (and debugged!) code without having to keep typing it in – divide task among multiple programmers – use vendor-supplied library of useful routines (e.g. common arithmetic functions) moves up another layer of abstraction – black box
Background image of page 2
3 Top-Down Design or Systematic Decomposition • To reduce complexity, the use of (separable) routines gets back to systematic decomposition: – Break a task down into smaller, more precisely stated subtasks – Apply systematic decomposition or top-down design to subtasks • This approach is clearly facilitated if subtasks can be accomplished using separately programmed modules (subroutines)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Bottom Up Design • Useful at the very lowest levels of detail sometimes • If you can’t immediately see how to break task up into sub-task – try writing instructions you know you’ll need – try handling just one simple case – try writing initialization, termination parts
Background image of page 4
5 No Subroutine Task 1 Task R Task 2 Task R Task R Task 3 Task 4 Task 1 Task 2 Task 3 Task 4 Task R To task R To task R To task R Subroutine Return
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 General Purpose Registers • R0 and R7 are often used (and overwritten) – TRAP routines – Subroutines (JSR/JSRR) • R6 also sometimes special purpose (stack pointer) • Need to save their contents sometimes
Background image of page 6
7 Example LEA R3, Binary LD R6, ASCII ; char->digit diff. LD R7, COUNT ; initialize to 10 AGAIN TRAP x23 ; Get char ADD R0, R0, R6 ; convert to number STR R0, R3, #0 ; store number ADD R3, R3, #1 ; incr pointer ADD R7, R7, -1 ; decr counter BRp AGAIN ; more? BR NEXT ASCII .FILL xFFD0 COUNT .FILL #10 Binary .BLKW #10 What’s wrong with this program?
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Saving and Restoring Registers • Saving: The contents of (value stored in) a GPR should be saved in memory if – it might be altered by some subsequent action and – it is needed after that action. • Restoring: This is the process of retrieving the saved value(s) and loading them back to the registers they came from
Background image of page 8
9 Who Does the Saving/Restoring? • Caller of subroutine? – knows what it needs later, but may not know what gets altered by called routine • Called subroutine? – knows what it alters, but does not know what will be needed later by calling routine
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Saving and Restoring Registers • Called routine – “ callee-save – Before start, save any registers that will be altered (unless altered value is desired by calling program!) – Before return, restore those same registers • Calling routine – “ caller-save – Save registers destroyed by own instructions or
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 44

6 - BME 303 Lecture 17 Subroutines Begin I/O Remember:...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online