ppt version

Subroutines
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
 …
… passing data
Techniques:
1. in registers
2. in memory
locations
3. in a parameter
block
4. on stack
5. 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
jsr
…
stop
#5,D0
v_delay
; init delay count
; and call delay
#$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
clr.l
move.b
jsr
move.b
stop
data,A0
D0
data_l,D0
find_median
D1,median
#$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
done
tst.b
bne
add.b
lsr.b
movem.l
rts
median ds.b
flag
ds.b
flag
done
-(A0),D1
#1,D1
(sp)+,D0/A0
1
1
data
dc.b
data_l dc.b
1,3,5,7,8
5
data
dc.b
data_l dc.b
1,3,5,7,7,8
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
add.l
move.b
add.b
lsr.b
rts
#1,D0
D0,A0
(A0),D1
-(A0),D1
#1,D1
odd
lsr.b
add.l
move.b
rts
#1,D0
D0,A0
(A0),D1
data
dc.b
data_l dc.b
median ds.b
1,3,5,7,7,8
6
1
e.g. find median v3
lea
clr.l
move.b
jsr
move.b
stop
data,A0
D0
data_l,D0
f_median
D1,median
#$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
move.b
add.b
lsr.b
rts
D0,A0
(A0),D1
-(A0),D1
#1,D1
odd
add.l
move.b
rts
D0,A0
(A0),D1
data
dc.b
data_l dc.b
median ds.b
1,3,5,7,7,8
6
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
jsr
v_delay
…
stop
#$2700
* Comments for subroutine here
v_delay
move.l D0,-(SP)
move.w delay_cnt,D0
d_loop
nop
dbra
D0,d_loop
move.l (SP)+,D0
rts
delay_cnt ds.w
1
; init delay count
; and call delay
; save context
; init count
; kill some time
; restore context
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
…
move.w
lea
jsr
…
stop
inc_lst movem.l
move.w
move.w
.
inc_lp .
dbra
movem.l
rts
to every element in an array
array
arr_inc
arr_l
arr_d
*
1
100
100
equ
ds.w
dc.w
ds.w
#5,arr_inc
array,A0
inc_lst
; init increment
; init table pointer
; and do variable incr
#$2700
; save context
; get increment
; and length
,inc_lp
; increment element
; and loop til done
; restore context
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:

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