Title: ECE3120: Computer Systems Subroutines
1ECE3120 Computer SystemsSubroutines
- Dr. Xubin He
- http//iweb.tntech.edu/hexb
- Email hexb_at_tntech.edu
- Tel 931-3723462, Brown Hall 319
2Subroutines - A sequence of instructions that
can be called from various places in the
program - Allows the same operation to be
performed with different parameters - Simplifies
the design of a complex program by using the
divide-and-conquer approach
3Subroutine Processing
4Instructions Related to Subroutine
Calls ltlabelgt BSR ltrelgt ltcommentgt branch
to subroutine ltlabelgt JSR ltoprgt ltcommentgt
jump to subroutine ltlabelgt RTS ltcommentgt
return from subroutine ltlabelgt CALL ltoprgt ltcomm
entgt to be used in expanded memory RTC
return from CALL where ltrelgt is the offset to
the subroutine ltoprgt is the address of the
subroutine and is specified in the direct,
extended, Indexed, or indexed indirect addressing
modes.
bsr bubble jsr ff jsr sq_root jsr 0,x
5Issues in Subroutine Calls 1. Parameter passing
- Use registers - Use the stack - Use global
memory 2. Returning results - Use
registers - Use the stack (caller created a hole
in which the result will be placed) - Use global
memory 3. Local variable allocation - Allocated
by the callee - The following instruction is the
most efficient method of local variable allocati
on. leas -n,sp allocate n bytes in the stack
for local variables 4. Local variable
deallocation - performed by the subroutine -
The following instruction is the most efficient
method of local variable deallocation. leas n,sp
deallocate n bytes from the stack
6- Stack Frame
- - The region in the stack that holds incoming
parameters, the subroutine return - address, local variables, and saved registers is
referred to as stack frame. - The stack frame is also called activation
record.
7Example 4.10 Draw the stack frame for the
following program segment after the leas 10,sp
instruction is executed org
1800 ldd 1234 pshd ldx 4000 pshx jsr sub
_xyz ldaa 3000 swi org 2000 sub_xyz pshd
pshx pshy leas -10,sp rts Solution The
stack frame is shown in Figure 4.10.
8- If a subroutine saves registers when it is
entered, it must restore them in the reverse
order (last-in-first-out) before returning to the
caller.
9Example of Subroutines 4.9.1. Finding the
Greatest Common Divisor of Integers m and
n Algorithm Step 1 If m n then gcd ?
m return Step 2 if n lt m then swap m and
n. Step 3 gcd ? 1 if m 1 then return. Step
4 For i 2 to m do if (m i 0 and n i
0) gcd ? i
10Example 4.11 Write a subroutine to compute the
greatest common divisor of two 16-bit unsigned
integers. Solution The two 16-bit incoming
parameters are passed in index register X and
double accumulator D. The stack frame of this
subroutine is shown in Figure 4.11.
11gcd_local equ 0 the distance of variable
gcd_local from the stack top i_local equ 2 the
distance of variable i_local from the stack
top m_local equ 4 the distance of variable
m_local from the stack top n_local equ 6 the
distance of variable n_local from the stack
top local_var equ 8 number of bytes to be
allocated to local variables org 800 m dw 375
the first operand n dw 1250 the second
operand gcd rmw 1 org 1000 ldx m ldd n jsr
find_gcd std gcd save the gcd in
memory swi find_gcd pshy leas -local_var,sp
allocate space for local variables stx n_local,sp
std m_local,sp ldy 1 sty gcd_local,sp
initialize gcd to 1 cpd n_local,sp compare m
with n
12 beq m_equ_n gcd m if m n blo m_less_n
it is fine if m lt n exg d,x swap m and
n std n_local,sp also make sure the stack
frame copy of stx m_local,sp m and n are
swapped m_less_n cpd 1 beq done if m 1,
then gcd 1 ldx 2 stx i_local,sp initialize
i to 2 loop ldx i_local,sp cpx m_local,sp bhi do
ne ldd m_local,sp idiv divide m by
i cpd 0 bne next_i ldd n_local,sp ldx i_local
,sp idiv divide n by i cpd 0 bne next_i ld
d i_local,sp
13 std gcd_local,sp set i as the current
gcd next_i ldx i_local,sp inx stx i_local,sp
increment i jmp loop m_equ_n ldd m_local,sp b
ra exit done ldd gcd_local,sp exit leas local_var
,sp deallocate local variables puly rts en
d
14Bubble Sort - Sorting is useful for improving the
searching speed when an array or a file need to
be searched many times. - Bubble sort is a simple
but inefficient sorting method. Example 2.13
Write a subroutine to implement the bubble sort
algorithm and a sequence of instructions
for testing the subroutine. - Pass the base
address of the array and the array count in the
stack - Four bytes are needed for local
variables.
15(No Transcript)
16arr equ 13 distance of the variable arr from
stack top arcnt equ 12 distance of the variable
arcnt from stack top buf equ 3 distance of
local variable buf from stack top in_order equ 2
distance of local variable in_order from stack
top inner equ 1 distance of local variable
inner from stack top iteration equ 0 distance
of local variable iteration from stack
top true equ 1 false equ 0 n equ 30 array
count local equ 4 number of bytes used by local
variables org 800 array_x db 3,29,10,98,54,9,100
,104,200,92,87,48,27,22,71 db 1,62,67,83,89,101,1
90,187,167,134,121,20,31,34,54 org 1000 lds 8
000 initialize stack pointer ldx array_x pshx
ldaa n psha jsr bubble leas 3,sp
deallocate space used by outgoing
parameters swi break to D-Bug12 monitor
17bubble pshd pshy pshx leas -local,sp
allocate space for local variables ldaa arcnt,sp
compute the number of iterations to be
performed deca " staa iteration,sp " ploop
ldaa true set array in_order flag to true
before any iteration staa in_order,sp " ldx ar
r,sp use index register X as the array
pointer ldaa iteration,sp initialize inner
loop count for each iteration staa inner,sp " c
loop ldaa 0,x compare two adjacent
elements cmpa 1,x " bls looptest the
following five instructions swap the two adjacent
elements staa buf,sp swap two adjacent
elements ldaa 1,x " staa 0,x " ldaa buf,sp
" staa 1,x " ldaa false reset the
in-order flag staa in_order,sp "
18looptest inx dec inner,sp bne cloop tst in_orde
r,sp test array in_order flag after each
iteration bne done dec iteration,sp bne ploop d
one leas local,sp deallocate local
variables pulx puly puld rts end
19Finding the Square Root - One of the methods for
finding the square root of an integer is based on
the following equation
- Equation 4.1 can be transformed into
- The algorithm for finding the square root of an
integer based on equation 4.2 is illustrated in
the flowchart shown in Figure 4.16.
20(No Transcript)
21Example 4.14 Write a subroutine to implement the
square root algorithm. This subroutine should be
able to find the square root of a 32-bit unsigned
integer. The parameter is pushed onto the stack
and the square root is returned in accumulator
D. Solution The stack frame is shown in Figure
4.17. The subroutine and the instruction sequence
for testing the subroutine is shown in the
following pages.
22q_hi equ 000F upper word of q q_lo equ 4240
lower word of q i_local equ 0 distance of local
variable i from the top of the stack sum equ 2
distance of local variable sum from the top of
the stack q_val equ 10 distance of incoming
parameter q from the top of stack local equ 6
number of bytes allocated to local
variables org 800 sq_root rmb 2 to hold the
square root of q org 1000 ldd q_lo pshd ldd
q_hi pshd jsr find_sq_root std sq_root leas 4
,sp swi find_sq_root pshy save y in the
stack leas -local,sp allocate local
variables ldd 0 initialize local variable i
to 0
23 std i_local,sp " std sum,sp initialize
local variable sum to 0 std sum2,sp " loop ldd
i_local,sp ldy 2 emul compute 2i add 2i
to sum addd sum2,sp std sum2,sp tfr y,d adcb
sum1,sp stab sum1,sp adca sum,sp staa sum,sp
add 1 to sum (need to propagate carry to the
most significant byte of sum) ldaa 1 adda sum3
,sp staa sum3,sp ldaa sum2,sp adca 0 staa s
um2,sp ldaa sum1,sp adca 0
24 staa sum1,sp ldaa sum,sp adca 0 staa sum,sp
increment i by 1 ldd i_local,sp addd 1 std i
_local,sp compare sum to q_val by performing
subtraction (need consider borrow) ldd sum2,sp
subd q_val2,sp ldaa sum1,sp sbca q_val1,sp l
daa sum,sp sbca q_val,sp lblo loop ldd i_local,
sp place sq_root in D before return
deallocate space used by local variables exit leas
local,sp puly rts end
25Next
- Using D-Bug12 to perform I/O operations
- Read Chapter 4.10-11