Part11-Subroutines - Subroutines Thorne : Chapter 8 (Irvine...

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

View Full Document Right Arrow Icon
Subroutines horne : Chapter 8 Thorne : Chapter 8 (Irvine Edition IV : Section 5.5) SYSC3006 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
ubroutines Subroutines - A sequence of instructions that can be called from various places in the program pp g - Allows the same operation to be performed with different parameters - implifies the design of a complex program by using the Simplifies the design of a complex program by using the divide-and-conquer approach - Simplifies testing and maintenance: separation of concerns - ata structures handled by different subroutines: information Data structures handled by different subroutines: information hiding a high vel language they are called : nction procedure -In a high-level language, they are called : function , procedure, method -In assembly languages, they are called : subroutine SYSC3006 2
Background image of page 2
ubroutine Processing Subroutine Processing rogram start Subroutines are a form of control flow • Control is passed to the activity • The activity is executed ontrol is returned to the invocation point . . . program start • Control is returned to the invocation point < call > subroutine_x next instruction subroutine_x . . . . . . . . < return > SYSC3006 3 Program flow during a subroutine call
Background image of page 3

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

View Full DocumentRight Arrow Icon
Multiple Subroutine Calls program first t i multiple activity: invocation p invocation points ??? done second invocation how can the activity know which one to “return” to? During invocation, the invocation point must be saved. SYSC3006 4 During return, the invocation point must be restored.
Background image of page 4
Machine Level Implementation of Subroutines CALL target ; invoke target subroutine t i S t i Execution Semantics: 1. Save the return address (address of next instruction) on run-time stack PUSH IP IP value AFTER fetching CALL instruction! 2. Transfer control to activity JMP target RET ; return from subroutine Execution Semantics: 1. Return control to address currently saved on top of stack SYSC3006 5 POP IP
Background image of page 5

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

View Full DocumentRight Arrow Icon
Subroutine Processing program activity: CALL activity after fetch = = Next CALL activity Next: RET IP = = Next PUSH IP JMP activity ext RET POP IP after execution IP == activity P ext SP Next old top after execution IP = = Next SP Next old top SP Next old top Stack N.B. Only works if turn address is on top responsibility of Stack SYSC3006 6 return address is on top of stack when RET is executed !!! subroutine !!
Background image of page 6
ested Subroutine Calls Nested Subroutine Calls program CALL Next1: activity1: CALL Next2: activity2: RET RET Next1 Next2 Next1 Runtime Stack SYSC3006 7 old top old top old top
Background image of page 7

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

View Full DocumentRight Arrow Icon
Assembly Support : PROC Directive Informally , a subroutine is any named sequence of instructions that end in a return statement •I n t el Assembly has additional directives that provide more e sse by s dd o d ec ves povde oe structure for encapsulation of the subroutine ain ROC ubr ROC main PROC subr PROC …. MOV AX, 4C00 RET INT 21h subr ENDP main ENDP END END main SYSC3006 8
Background image of page 8
Issues in Subroutine Calls We shall define subroutines using C-like prototypes Return TYPE name Argument list : type name void display (word number, byte base) // Display the given number // base = 0 for binary, =1 for HEX byte absoluteValue (word number)
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 36

Part11-Subroutines - Subroutines Thorne : Chapter 8 (Irvine...

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

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