Hardware Reference
In-Depth Information
Example 4.7
Write a subroutine that implements the division algorithm using the repeated subtraction
method for a 32-bit unsigned dividend and divisor. The caller of this subroutine will pass the
dividend and divisor in the stack and will allocate space in the stack for this subroutine to
return the quotient and remainder. Also, write an instruction sequence to test this subroutine.
Solution: The stack frame of this subroutine is shown in Figure 4.11.
SP
buf
SP+4
SP+5
SP+9
i
R
Q
[y]
[x]
[D]
return address
SP+21
SP+25
SP+29
SP+33
divisor
dividend
quotient
remainder
Figure 4.11 Stack frame of Example 4.7
The divide subroutine and its test program are as follows:
buf
equ
0
; distance of buf from the top of the stack
i
equ
4
; distance of i from the top of the stack
R
equ
5
; distance of R from the top of the stack
Q
equ
9
; distance of Q from the top of the stack
divisor
equ
21
; distance of divisor from the top of the stack
dividend
equ
25
; distance of dividend from the top of the stack
quo
equ
29
; distance of quo from the top of the stack
rem
equ
33
; distance of rem from the top of the stack
locVar
equ
13
; number of bytes for local variables
dvdendHI
equ
$42
; dividend to be tested
dvdendLO
equ
$4c15
; ''
dvsorHI
equ
$0
; divisor to be tested
dvsorLO
equ
$64
; ''
org
$1000
quotient
ds.b
4
; memory locations to hold the quo
remain
ds.b
4
; memory locations to hold the remainder
org
$1500
; starting address of the program
start
lds
#$1500
; initialize stack pointer
leas
2 8,SP
; make a hole of 8 bytes to hold the result
Search WWH ::




Custom Search