Lecture15 - 0306-250 Assembly Language Programming Lecture...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 0306-250 Assembly Language Programming Lecture Fifteen: Subroutine Parameter Passing via Registers Subroutine Register Parameter Passing Real programs use memory for data • Most available storage is in memory – ROM may be used to load program constants – Significantly slower than registers • Relatively small number of registers – – – – Contain no program data at start of program Significantly faster than memory Provide working set of fast data Used for passing parameters to subroutines 2 Subroutine Register Parameter Passing Two ways to pass a parameter • Passed by value (A, B, or D) – Value copied into data register – Source could be literal or variable – Source unchanged by subroutine— works with copy • Passed by reference (X or Y) – Address loaded into address register – Source must be variable—no literals – Source may be changed by subroutine— works directly with source 3 Parameter Classification by Data Flow Three types of parameters by usage • Input (in) parameter – Passed into the subroutine – Subroutine operation depends on parameter value • Output (out) parameter – Passed out of the subroutine – Subroutine operation generates parameter value • Input/Output (in/out) parameter – Passed in and out of the subroutine – Subroutine operation depends on parameter value and either modifies it or replaces it 4 Parameter Usage Affects Passing Choice Input vs. Output Þ Pass Value vs. Pass Reference • Input parameters: passed by value – Subroutine works with copy – Original value will not be changed – Exception: large data structure parameter • Output and input/output parameters: passed by reference – Subroutine works with actual parameter – Original value can be changed 5 Example Subroutine: Sum of Integers 1 through N Input: Output: N Sum Sum ß 0 repeat Sum ß Sum + N NßN–1 until (N = 0) return (Sum) 6 Example Subroutine: All parameters passed by value MyCode: SECTION main: Entry: ;Initialize system stack pointer lds #__SEG_END_SSTACK ;Sum integers 1 through N ldab N bsr SumN staa Sum Done: bra * SumN: ;********************************************************************** ; Computes sum of integers 1 to N. ; Input: B: N ; Output: A: Sum ; Modify: A, B, CCR ; Initialize Sum to 0 clra ; Loop to compute sum = N + (N-1) + . . . + 2 + 1 SumNLoop: aba ; Add N dbne b,SumNLoop ; Continue if --N <> 0 rts ;********************************************************************** ; Variables MyData: SECTION N: DS.B 1 Sum: DS.B 1 END 7 Example Subroutine: Parameters passed in by reference and out by value MyCode: SECTION main: Entry: ;Initialize system stack pointer lds #__SEG_END_SSTACK ;Sum integers 1 through N ldx #N bsr SumN staa Sum Done: bra * SumN: ;********************************************************************** ; Computes sum of integers 1 to N. ; Input: X: Address of N ; Output: A: Sum ; Modify: A, CCR ; Initialize Sum to 0 clra ; Loop to compute sum = N + (N-1) + . . . + 2 + 1 SumNLoop: adda 0,x ; Add N dec 0,x ; N <-- N - 1 bne SumNLoop ; Continue if N <> 0 rts ;********************************************************************** ; Variables MyData: SECTION N: DS.B 1 Sum: DS.B 1 END 8 Example Subroutine: Parameters passed in by value and out by reference MyCode: SECTION main: Entry: ;Initialize system stack pointer lds #__SEG_END_SSTACK ;Sum integers 1 through N ldab N ldx #Sum bsr SumN Done: bra * SumN: ;********************************************************************** ; Computes sum of integers 1 to N. ; Input: B: N ; Output: [X]: Sum ; Modify: A, B, CCR ; Initialize Sum to 0 clr 0,x ; Loop to compute sum = N + (N-1) + . . . + 2 + 1 SumNLoop: tba ; A <-- N adda 0,x ; A <-- N + Sum staa 0,x ; Sum = N + Sum dbne b,SumNLoop ; Continue if --N <> 0 rts ;********************************************************************** ; Variables MyData: SECTION N: DS.B 1 Sum: DS.B 1 END 9 Example Subroutine: All parameters passed by reference MyCode: SECTION main: Entry: ;Initialize system stack pointer lds #__SEG_END_SSTACK ;Sum integers 1 through N ldx #N ldy #Sum bsr SumN Done: bra * SumN: ;********************************************************************** ; Computes sum of integers 1 to N. ; Input: [X]: N ; Output: [Y]: Sum ; Modify: A, CCR ; Initialize Sum to 0 clr 0,y ; Loop to compute sum = N + (N-1) + . . . + 2 + 1 SumNLoop: ldab 0,y ; B <-- Sum addb 0,x ; B <-- Sum + N stab 0,y ; Sum = Sum + N dec 0,x ; N <-- N - 1 bne SumNLoop ; Continue if N <> 0 rts ;********************************************************************** ; Variables MyData: SECTION N: DS.B 1 Sum: DS.B 1 END 10 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online