Presentation is loading. Please wait.

Presentation is loading. Please wait.

94201.lecture19-22-subroutines

Similar presentations


Presentation on theme: "94201.lecture19-22-subroutines"— Presentation transcript:

1 94201.lecture19-22-subroutines
Often need to perform the same program activity many times High-level language: function, procedure, method Assembly language: subroutine Want ability to: encapsulate program activity in program text invoke the activity from elsewhere in program Control flow pass control to the activity execute the activity return control to the invocation point 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

2 Subroutines as Encapsulated Actvities
program control flow activity: done encapsulated activity invocation point 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

3 94201.lecture19-22-subroutines
Subroutines (contd) If only used once, imbed inline activity: done 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

4 What if invoked more than once?
Subroutines (contd.) What if invoked more than once? encapsulated activity program activity: done invocation points how can the activity know which one to “return” to? 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

5 Subroutine CALL and RET
During invocation: must save invocation point information During return: must return to saved invocation point Machine-level implementation mechanisms: CALL target (invoke target subroutine) PUSH IP save return address on run-time stack JMP target pass control to activity RET (return from subroutine) POP IP return to pushed address IP value AFTER fetching CALL instruction! 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

6 --responsibility of subroutine!! old top Next SP old top
activity: RET CALL activity (after fetch IP = = Next) PUSH IP JMP activity after execution IP = = activity Stack program CALL activity Next: RET POP IP after execution IP = = Next Stack SP Next N.B. only works if return address is on top of stack when RET is executed --responsibility of subroutine!! old top Next SP old top 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

7 Nested Subroutine Calls
program CALL Next1: activity1: CALL Next2: RET activity2: RET old top Next1 old top Next2 Next1 old top Run- Time Stack 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

8 Subroutine Parameters
increase the generality of an activity allow invocations to behave differently allows caller (the code that invokes) and callee (the invoked activity) to communicate information Example: could have a dedicated subroutine that displays the value 245 void Display245( ); nice, but ..... could generalize by adding a parameter allow a 16-bit value to be displayed to be communicated at invocation void DisplaySigned( word Value ); 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

9 Subroutine Parameters (contd)
could further generalize by adding another parameter to communicate the base for displaying Value void Display16( word Value, byte Base); (Base- 0 : binary, 1 : hex, 2 : decimal) text of function/subroutine is static – write in terms of parameters behaviour of each invocation is dynamic – depends on arguments – the actual parameter values passed to specific invocations parameters can be communicated in various ways: global variables   registers on the stack  POLICY for ,  30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

10 94201.lecture19-22-subroutines
Global Variable A shared (static) memory variable to communicate invocation argument: caller: puts argument value in variable, then calls callee: reads argument from variable Example: MOV WORD PTR [Value], 245 CALL activity . . . activity: MOV AX, [Value] RET Value: DW Caller Callee Global variable 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

11 94201.lecture19-22-subroutines
Global Variables not widely used in practice problems in large programs – software engineering issues in : turns out to be essential only way to communicate in some cases 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

12 Subroutine Parameter Passing
Via Registers: could exchange arguments in registers caller: load registers with values callee: read values from registers used in assignments 2 and 3 OK, but ... finite number of registers what to do if more parameters than registers? Via the Run-Time Stack caller: push invocation arguments onto stack callee: indexes into stack to access arguments POLICY for 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

13 94201.lecture19-22-subroutines
Subroutines (contd) Many more subroutine issues to deal with: register save/restore parameters order and type of invocation arguments in stack indexing into stack – standard entry code pass by value vs. pass by reference return-value argument prototype/documentation clearing invocation arguments off of stack 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

14 Register Save / Restore
subroutines need to use registers what to do if registers that contain values that are useful to caller, but subroutine wants to use? One solution: caller responsible for saving all useful values before calling callee free to use any register caller responsible for restoring values saved before call an alternate solution: callee (subroutine) must save and restore all registers used an alternate alternate solution: hybrid of above caller responsible for some, callee responsible for some !? POLICY in this course 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

15 Parameter Passing on Stack
Design decisions Issues in Passing Parameters on the Stack: order and type of invocation arguments in stack Common specs needed for programmers writing: 1. implementation of subroutine 2. code that calls subroutine Document using C++-like prototype: return-value and type name of subroutine parameters: number, type, name & order Policy in ! 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

16 C++-like Subroutine Prototypes
Example: void Display16( word Value, byte Base); Order in stack? push right-to-left according to prototype in above example: push Base 1st, then Value no return-value subroutine name 2 parameters: 16-bit Value 8-bit Base Policy in 94.201 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

17 Subroutine Calling Policies
Implications of POLICIES (so far) caller: push arguments, then CALL callee: save all registers to be used must index into stack to access arguments must restore saved registers before return pushes return address 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

18 Parameter Access in Called Subroutine
Want consistent way for callee to deal with stack ( … need another POLICY  ) Callee will use BP to index to arguments Callee will set-up stack frame: PUSH BP ; save BP MOV BP, SP ; BP points to top of stack Callee now saves (PUSHes) any other registers it uses When callee is finished: restore (POP) saved registers (BP too!), then RET Policy: standard entry code Policy: standard exit code 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

19 94201.lecture19-22-subroutines
Stack Frame indexing into stack – use BP based or indexed every subroutine invocation results in a new stack frame on top of the stack SP saved registers BP – constant Standard Stack Frame old BP BP + 0 BP return address BP + 2 arguments High level languages save this and more: BP + 4 or more stack 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

20 94201.lecture19-22-subroutines
Stack Frame Example Recall Previous Example: void Display16( word Value, byte Base); call set up: MOV AL, 0 ; Base = binary PUSH AX PUSH [BX + SI] ; Value to display CALL Display16 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

21 94201.lecture19-22-subroutines
Stack Frame Example stack frame after standard entry code: in body of Display16: MOV AX, [BP + 4] ; get Value . . . CMP BYTE PTR [BP + 6], 0 ; binary ? SP saved registers BP old BP return address BP + 2 BP + 4 Value BP + 6 Base could WORD PTR be used? 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

22 Call by Value / Call by Address
Or “pass by value vs. pass by reference” Pass by value: argument is a copy of the value of interest in high-level language like C++, default way to pass simple variables e.g. int example: int MyValue; MyValue = 245; Display16( MyValue, 0 ); 2. A copy of the data value of the variable MyValue is pushed onto stack 1. Immediate value 0 is pushed onto stack 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

23 94201.lecture19-22-subroutines
Call by Value Example Stack frame after entry code in Display16: SP saved registers old BP BP return address BP + 2 245 BP + 4 BP + 6 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

24 94201.lecture19-22-subroutines
Call by Value (contd) NOTE: in subroutine, arguments passed in the stack can be treated like local variables: value can be read & modified but variable is local – exists ONLY during subroutine execution no persistent changes! (any modifications cannot be seen by caller) in Previous Display16 Example: can change the received copy of MyValue MOV [BP + 4], AX changes local variable but does not change original variable ! 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

25 94201.lecture19-22-subroutines
Call by Reference sometimes need access to caller’s variables, e.g. 1. purpose of subroutine is to modify caller’s variables 2. passing large composite structures on stack can require too much time/space Pass/Call by reference: argument is the address of a memory variable need prototype syntax to differentiate value vs. reference parameters default will be pass by value: type name e.g. int Value For pass by reference use type & name e.g. int & Value pass reference to integer variable 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

26 Call by Reference Example
void SortArray ( int & SortMe[ ], int Size ); Array declaration X: DW DW . . . SizeOfX: DW Call to sort X PUSH WORD PTR [ SizeOfX ] MOV AX, X PUSH AX CALL SortArray 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

27 Pass by Reference Example (contd)
after entry code in SortArray: SP saved registers BP old BP BP + 2 return address BP + 4 address of X BP + 6 copy of SizeOfX 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

28 Call by Reference Example (contd)
In SortArray after stack frame init code: . . . MOV BX, [ BP + 4 ] ; get array address MOV SI, 0 ; array index = 0 MOV AX, [ BX + SI ] ; get array element 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

29 94201.lecture19-22-subroutines
Return Values functions can return values two ways: 1. return value in variable passed by reference 2. return value in some other state variable global variable?  (NO!) stack ? Maybe caller could allocate an extra word in stack before call eg. SUB SP, 2 CALL . . . callee could return value there register (high-level languages do this!) 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

30 94201.lecture19-22-subroutines
Return Values POLICY in this course (and ) return 8-bit value in AL (AH is undefined) return 16-bit value in AX return 32-bit value in DX:AX as with 32-bit values for DIV instruction Implications of Return-Value Policy  do not save/restore register(s) used for return-value (the purpose of the subroutine is to return a value in the register(s)) if 8-bit value (returned in AL) – subroutine is not responsible for persistence of AH value 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

31 Returning Values - Example
void ChkAbsValue ( int & X, int Y ); returns (X == | Y |) in first argument any potential problems? Possible solution: boolean ChkAbsValue( int & X, int Y ); Boolean return value: byte or word 0  false non-zero  true Do we still need to pass X by reference? 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

32 Stack Cleanup after Subroutine Call
Must remove arguments from stack caller or callee ? POLICY: caller ! Could POP values – but not good idea - subroutine might have modified values passed ! Typical stack cleanup method used by caller ADD SP, #ofBytes Policy for stack cleanup in 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

33 Stack Cleanup after Subroutine Call
After RET: ADD SP, 4 ; clear arguments from stack saved registers old values from CALL old BP return address SP arg1 arg2 rest of stack 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

34 Summary of Subroutine Call Mechanism
Call set up: push arguments onto stack CALL subroutine Subroutine entry code: set up stack frame , BP access into stack save registers used  Subroutine body: perform application code if return-value needed – calculate and put in appropriate register(s) Subroutine exit code: clean up stack – restore registers used RET Caller clean up: remove arguments from stack save/use return-value See details of Policies for these mechanisms 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

35 94201.lecture19-22-subroutines
Database Example Suppose that a database keeps records consisting of: birth date (day, month year) name student number Record data structures are implemented as: Day: DB ; range: Month: DB ; range: Year: DB ; range: Name: DB ‘ $’ ; max 20 chars StudentNum: DW ; unsigned double DW ; word – range? The size of each record is: 3 (Date) + 21 (Name) + 4 (StudentNum) = 28 bytes little endian words! 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

36 Database Example (contd)
In each record: offset to day = 0 offset to month = 1 offset to year = 2 offset to name = 3 offset to student number = 24 Suppose database is implemented as an array of 28 byte records Limitations? Y2K? Name size? Student Number range? 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

37 Database Example (contd)
Program to manage database might include subroutine: void ReportDB ( db & MyDB, // ref to database unsigned int DBSize // number of records ); // purpose: writes contents of db to text file Assume following subroutine exists: boolean WriteText ( char & Text[ ] ); // write ‘$’ terminated text string to text file // returns true iff write was successful 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

38 Database Example (contd)
Requirements: records are to be reported as: name student number birth date sort by: name, student number, birth date Other support subroutines needed? depends on approach to solution 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

39 Database Example (contd)
Will use approach similar to assignment 3: RecordText: DB ‘ ’ NameText: DB ‘ ’ StudentNumText: DB ‘123456’ BirthDateText: DB ‘ dd/mm/yy’ DB ‘$’ ; terminator  fill in RecordText for a record pass RecordText address to WriteText 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

40 Database Example (contd)
Could have subroutines to handle the conversion of database info into text: void ReportName( char & dbName[ ], char & NameTxt[ ] ); where: dbName is ptr to $-term. name text in db record NameTxt is pointer to display buffer void ReportNum( unsigned longint Num, char & NumTxt[ ] ); where: Num is 32-bit student number (push most signif word first) NumTxt is pointer to display buffer void ReportDate( byte & Date[ ], char & DateTxt[ ] ); where: Date is ptr to 3-byte array [DAY, MONTH, YEAR] DateTxt is pointer to display buffer 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

41 ; set up to access database records:
ReportDB: PUSH BP MOV BP, SP PUSH ; save regs ; set up to access database records: MOV BX, [ BP + 4 ] ; BX=start of db aray MOV SI, 0 ; SI = offset to rec ; process records one at a time ; for ( CX = DBSize; CX > 0; CX – – ) ; { clear display buffer; ; put record text into display buffer; ; output text to file then advance to next rec; } MOV CX, [ BP + 6 ] ; CX = DBSize Standard entry code Algorithm description 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

42 94201.lecture19-22-subroutines
Note that documentation uses a combination of C++-style pseudo-code and line-by-line comments! DoRec: ; process a record CMP CX, 0 ; loop test: done? JE DoneReport ; loop: clear display buffer ; for ( DI = RecordText; [DI] != ‘$’; DI++ ) {[DI] = ‘ ’; } MOV DI, RecordText CheckChar: CMP BYTE PTR [DI], ‘$’ ; loop test: DI ? ‘$’ JE DoText ; yes – exit loop MOV BYTE PTR [DI], ‘ ’ ; blank the char ADD DI, 1 ; loop inc: DI ++ JMP CheckChar 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

43 Database Example (contd)
<continuing with Reportdb subroutine> DoText: ; put name in display buffer – use ReportName MOV AX, NameText PUSH AX MOV AX, BX ; get start address ADD AX, SI ; add offset to this record ADD AX, 3 ; add offset to name CALL ReportName ADD SP, 4 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

44 Aside: Pushing Address in a Structure
Above: explicit calculation of an address that is automatically generated due to addressing mode Some processors support LEA “Load Effective Address” instruction: calculates address specified by addressing mode loads destination with address, does not go to memory at address  replace: MOV AX, BX ; get start address ADD AX, SI ; add offset to this record ADD AX, 3 ; add offset to name with: LEA AX, [BX + SI + 3 ] LEA not supported in p86 yet 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

45 94201.lecture19-22-subroutines
Other Issues in ReportNum? how to convert 32-bit value for output? order of generating digits? problems with DIV? <Returning to Reportdb example:> ; put student number name into ; display buffer – use ReportNum ; recall: ; void ReportNum( unsigned longint Num, char & NumTxt[ ] ); ; Num is 32-bit student number (push most signif word first) ; NumTxt is pointer to display buffer MOV AX, StudentNumText PUSH AX PUSH [ BX + SI + 26 ] ; most signif word PUSH [ BX + SI + 24 ] ; least signif word CALL ReportNum ADD SP, 6 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

46 94201.lecture19-22-subroutines
;put birth date in display buffer -use ReportDate; recall ; void ReportDate( byte & Date[ ], char & DateTxt[] ); ; Date is ptr to 3-byte array [ DAY, MONTH, YEAR ] ; DateTxt is pointer to display buffer MOV AX, BirthDateText PUSH AX MOV AX, BX ; get start address ADD AX, SI ; add offset to this record ; offset to [DATE, MONTH, YEAR ] = 0 CALL ReportDate ADD SP, 4 Issues in ReportDate? 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

47 94201.lecture19-22-subroutines
; output record text to file ; use: boolean WriteText ( char & Text[ ] ); MOV AX, RecordText PUSH AX CALL WriteText ADD SP, 2 ; check for OK write to file: CMP AL, 0 JNE WriteOK ; do file write error handling here . . . WriteOK: ; advance to next record: 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

48 94201.lecture19-22-subroutines
WriteOK: ; advance to next record: ADD SI, 2 ; loop decrement: SUB CX, 1 JMP DoRec DoneReport: ; exit code: restore saved registers (including BP) POP RET <end of Reportdb subroutine!> 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

49 94201.lecture19-22-subroutines
Further Example ;void ReportName( char & dbName[ ], char & NameTxt[ ]); ; where: dbName is pointer to $-terminated ; name text in database record ; NameTxt is pointer to display buffer ; for interest – suppose that text is to be right justified in 20- ; char display buffer ReportName: ; standard entry code: PUSH BP MOV BP, SP ; save registers used PUSH 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

50 94201.lecture19-22-subroutines
Example (contd) ; set up for access: BX = address of name text MOV BX, [BP + 4 ] ; loop to find end of name ; for ( SI = 0; [BX + SI] != ‘$’; SI ++ ) { empty body } MOV SI, 0 ; char index = 0 CharCheck: CMP BYTE PTR [BX + SI ], ‘$’ JE FoundEnd ADD SI, 1 JMP CharCheck FoundEnd: 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

51 94201.lecture19-22-subroutines
Example (contd) FoundEnd: ; at this point, SI = number of char’s in name Want: (name text) $ BX + SI (display buffer) DI 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

52 94201.lecture19-22-subroutines
MOV DI, [ BP + 6 ] ; DI = address of dest buffer ADD DI, 19 ; DI = address of last char ; adjust SI to point to char, not ‘$’ SUB SI, 1 ; SI = offset to last char in name ; Now copy the name into the buffer: ; for ( ; SI > 0; SI – – ) { [DI] = [BX + SI] } CheckCopyDone: CMP SI, 0 JLE CopyDone MOV AL, [BX + SI] MOV [DI], AL SUB SI, 1 JMP CheckCopyDone CopyDone: ; standard exit code – ; restore registers, etc POP . . . RET 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

53 94201.lecture19-22-subroutines
Example (contd) Could accomplish same thing without SI! ; loop to find end of name ; for ( BX = [BP + 4]; [BX] != ‘$’; BX ++ ) ; { empty body } ; loop to copy name backwards ; for (DI = [BP + 6] + 19 ; BX > [BP + 4] ; BX – –) ; { [DI] = [BX – 1] } 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines

54 94201.lecture19-22-subroutines
Example (contd) -1 [BP+4] (name text) $ BX (display buffer) DI 30 Oct-01 Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept. 2001 94201.lecture19-22-subroutines


Download ppt "94201.lecture19-22-subroutines"

Similar presentations


Ads by Google