Subroutines … passing data Mechanisms: pass by value pass by result pass by value result/ value return/ copy restore pass by reference pass by name pass by lazy evaluation … Techniques: in registers in memory locations in a parameter block on stack in the code stream
Passing data in registers simple programs with few parameters complex programs, e.g. operating systems, with an established standard or ‘parameter convention’ usually, the low numbered registers, e.g. D0, D1, … etc, are used
Passing data in registers e.g. variable delay routine … move.w #5,D0 ; init delay count jsr v_delay ; and call delay stop #$2700 * Insert comments for subroutine here v_delay move.l D0,-(SP) ; save context d_loop nop dbra D0,d_loop ; kill some time move.l (SP)+,D0 ; restore context rts What registers should be saved? Who decides? Why not save all registers all the time?
e.g. find median v1 lea data,A0 clr.l D0 move.b data_l,D0 jsr find_median move.b D1,median stop #$2700 * Preconditions: * A0 – pointer to list * D0 – list count (byte) * Postconditions: * D1 – median (byte) find_median: movem.l D0/A0,-(sp) lsr.b #1,D0 scs flag add.l D0,A0 move.b (A0),D1 tst.b flag bne done add.b -(A0),D1 lsr.b #1,D1 done movem.l (sp)+,D0/A0 rts median ds.b 1 flag ds.b 1 data dc.b 1,3,5,7,8 data_l dc.b 5 data dc.b 1,3,5,7,7,8 data_l dc.b 6 Median is the middle value if odd number of values or the average of the 2 middle values
e.g. find median v2 lea data,A0 clr.l D0 move.b data_l,D0 jsr f_median move.b D1,median stop #$2700 * Same pre/post conditions * for all 3 subroutines * D1 is still output!! f_median: movem.l D0/A0,-(sp) btst #0,D0 bne odd_r jsr even bra done odd_r jsr odd done movem.l (sp)+,D0/A0 rts even lsr.b #1,D0 add.l D0,A0 move.b (A0),D1 add.b -(A0),D1 lsr.b #1,D1 rts odd lsr.b #1,D0 add.l D0,A0 move.b (A0),D1 rts data dc.b 1,3,5,7,7,8 data_l dc.b 6 median ds.b 1
e.g. find median v3 lea data,A0 clr.l D0 move.b data_l,D0 jsr f_median move.b D1,median stop #$2700 * Same pre/post conditions * for all 3 subroutines * D1 is still output!! f_median: movem.l D0/A0,-(sp) lsr.b #1,D0 bcs odd_r jsr even bra done odd_r jsr odd done movem.l (sp)+,D0/A0 rts even add.l D0,A0 move.b (A0),D1 add.b -(A0),D1 lsr.b #1,D1 rts odd add.l D0,A0 data dc.b 1,3,5,7,7,8 data_l dc.b 6 median ds.b 1
Passing data in memory OK for simple programs e.g. test programs, one-time use severely limits how a subroutine is called unusual in commercial code makes the procedure non-reentrant only one instance possible
Passing data in memory e.g. variable delay routine … move.w #5,delay_cnt ; init delay count jsr v_delay ; and call delay stop #$2700 * Comments for subroutine here v_delay move.l D0,-(SP) ; save context move.w delay_cnt,D0 ; init count d_loop nop dbra D0,d_loop ; kill some time move.l (SP)+,D0 ; restore context rts delay_cnt ds.w 1
Passing data in parameter blocks used for large amounts of data stored in arrays or data structures pass the address of the data structure for complex data, i.e. a table containing data and pointers pass the address of a parameter block typically, the address is passed in an address register usually, the low numbered registers, e.g. A0, A1, … etc, are used
Passing data in parameter blocks e.g. add increment to every element in an array … move.w #5,arr_inc ; init increment lea array,A0 ; init table pointer jsr inc_lst ; and do variable incr … stop #$2700 inc_lst movem.l ; save context move.w ; get increment move.w ; and length . inc_lp . ; increment element dbra ,inc_lp ; and loop til done movem.l ; restore context rts array equ * arr_inc ds.w 1 arr_l dc.w 100 arr_d ds.w 100
Mechanisms for Passing Data pass by value used in C, Java, Pascal, Ada (in param) copy of argument passed to S/R infamous swap example void swap (int x, int y) { int t = x; x = y; y = t; } int main(void) { int a = 5; int b = 7; swap( a, b ); printf("a= %d; b= %d\n", a, b);
Mechanisms for Passing Data pass by reference used in Fortran, Pascal (var param), Delphi, C++, C (simulates pass by reference with pointers) address of argument passed to S/R "reference" is not the same as Java "reference"; Java is passing references to objects by value void swap ( int *px, int *py) { int t = *px; *px = *py; *py = t; } int main(void) { int a = 5; int b = 7; swap( &a, &b ); printf("a= %d; b= %d\n", a, b);
Mechanisms for Passing Data pass by sharing / pass by object used in Python where all data are objects with an identity (essentially an address), a type and a value. immutable objects behave like pass by value; mutable objects behave like pass by reference pass by value result / value return / copy restore used in Ada (in/out param), Fortran address of argument passed to S/R S/R makes a copy of argument when S/R is finished, copies the argument copy back to original parameter
Reading, Expectations Reading: Expectations: M68000 Assembly Language [pdf, 92p; N. Znotinas] review operation of JSR (BSR) and RTS instructions covered in presentation Expectations: you can explain the operation of JSR/BSR, RTS you can read/write code that uses the above instructions you can read/write code that passes data to a subroutine via registers, via memory locations and using parameter blocks