Hardware Reference
In-Depth Information
xgdx
; swap quotient to D
cpd
#0
; if quotient is 0, then prepare to pull out
beq
revloop
; the decimal digit characters
bra
divloop
; quotient is not 0, continue to perform divide-by-10
revloop
pula
staa
1, y+
; save ASCII string in the buffer
cmpa
#0
; reach the NULL pushed previously?
beq
done
; if yes, then done
bra
revloop
; continue to pup
done
pulx
; restore the index register X
rts
Before calling this subroutine, the caller must place the number to be converted in D and
place the starting address of the buffer to hold the resultant string in Y. The following instruc-
tion sequence is needed to call the above subroutine to convert the value 23,456 to an ASCII
string:
ldd
#23456
ldy
#buffer
; use Y as the pointer to the buffer that holds the string
jsr
bin2dec
This subroutine can be modified to convert a signed 16-bit number into the ASCII string
that represents its decimal value. A negative value would have a minus sign (2) in the front af-
ter the conversion. The modification to the subroutine to convert signed numbers is minor and
hence will be left as an exercise problem.
This example points out several issues involved in the subroutine call.
Passing parameters
Returning results
Saving registers used in the subroutine
A more complicated subroutine may have variables that are local to the subroutine but
invisible to the caller. These variables are referred to as local variables . Examples of local vari-
ables include loop indices and temporal results. These variables do not exist if the subroutine is
not entered. The next section deals with all of these issues.
4.5 Issues Related to Subroutine Calls
The program unit that makes the subroutine call is referred to as a caller, and the subrou-
tine called by other program units is referred to as a callee . As was pointed out in the previ-
ous section, there are four major issues in a subroutine call. These issues are explored in the
following subsections:
4.5.1 Parameter Passing
The caller usually wants the subroutine to perform a computation using the parameters
passed to it. The caller may use the following methods to pass parameters to the subroutine:
Use registers. In this method, parameters are placed in CPU registers before the
subroutine is called. This method is very convenient when there are only a few
parameters to be passed.
 
Search WWH ::




Custom Search