Chapter
11
I
Technical Information
When the CALL statement is executed, the operating system
loads the CS (code segment) register with the value specified in
the last DEF SEG statement. If you are CALLing a subroutine
within BASIC’s work area, and no DEF SEG is required, the CS
register is loaded with the address of BASIC’s work area. This
address
is
shifted left
4
bits;
in
other words, which
is
the same
as multiplying it by 16 decimal (10 hexadecimal). Then, the
off-
set of the subroutine is added to the segment address.
Example
17100
+
0020
=
17120
17120 is the absolute address of the first instruction in the
subroutine.
Technical Functions
The called routine may destroy the previous contents of all regis-
ters.
If
you want to save the contents of the registers, the first
instructions in the subroutine must be
a
PUSH
for
each register,
and the last instructions in the subroutine must be a POP to re-
store the registers to their original value. You must execute a
POP for every PUSH
to
maintain stack integrity.
The subroutine may refer to the passed parameters as positive
offsets to the Base Pointer (BPI. The CALLed routine must
PUSH BP on the stack and then move the current stack pointer
into BP. BP should be the first register you PUSH
so
that the
parameters may be referenced as an offset
to
BP. The first
4
bytes of the stack contain the IP and CS register values that
BASIC saves when the CALL
is
executed. To calculate the pa-
rameters offset from the BP, use this equation:
2
*
(total parameters
-
parameter position)
+
6
=
offset
For example, the address of parameter
1
is at 10(BP), parameter
2 is at S(BP), and parameter
3
is at 6(BP).
354