Hardware Reference
In-Depth Information
The first part of the program prepares to call
vecmul
by saving
SP
in
BP
and
then pushing the addresses of
vec2
and
vec1
onto the stack so that
vecmul
will have
access to them. Then the length of the vector in bytes is loaded in
CX
on line 8.
By shifting this result right one bit, on line 9,
CX
now contains the number of
words in the vector, which is pushed onto the stack on line 10. The call to
vecmul
is made on line 11.
Once again, it is worth mentioning that the arguments of subroutines are, by
convention, pushed onto the stack in reverse order to be compatible with the C call-
ing convention. In this way,
vecmul
can also be called from C using
vecmul(count, vec1, vec2)
During the
CALL
instruction, the return address is pushed onto the stack. If the pro-
gram is traced, then this address turns out to be 0x0011.
The first instruction in the subroutine is a
PUSH
of the base pointer,
BP
, on line
22.
BP
is saved because we will need this register to address the arguments and the
local variables of the subroutine. Next, the stack pointer is copied to the
BP
regis-
ter on line 23, so that the new value of the base pointer is pointing to the old value.
Now everything is ready for loading the arguments into registers and for
reserving space for a local variable. In the next three lines, each of the arguments
is fetched from the stack and put in a register. Recall that the stack is word ori-
ented, so stack addresses should be even. The return address is next to the old base
pointer so it is addressed by
2
(
BP
). The
count
argument is next and addressed by
4
(
BP
). It is loaded into
CX
on line 24. In lines 25 and 26,
SI
is loaded with
vec1
and
DI
is loaded with
vec2
. This subroutine needs one local variable with initial
value 0 to save the intermediate result, so the value 0 is pushed on line 27.
The state of the processor just before the loop is entered on line 28 for the first
time is shown in Fig. C-15. The narrow window in the middle of the top row (to
the right of the registers) shows the stack. At the bottom of the stack is the address
of
vec2
(0x0022), with
vec1
(0x0018) above it and the third argument, the number
of items in each vector (0x0005) above that. Next comes the return address
(0x0011). The number 1 to the left of this address indicates it is a return address
one level from the main program. In the window below the registers, the same
number 1 is shown, this time giving its symbolic address. Above the return
address in the stack is the old value of
BP
(0x7fc0) and then the zero pushed on
line 27. The arrow pointing to this value indicates where
SP
points. The window
to the right of the stack shows a fragment of the program text, with the arrow indi-
cating the next instruction to be executed.
Now let us examine the loop starting at line 28. The instruction
LODS
loads a
memory word indirectly through the register
SI
from the data segment into
AX
.
Because the direction flag is set,
LODS
is in auto-increment mode, so after the
instruction
SI
will point to the next entry of
vec1
.
To see this effect graphically, start the tracer with the command
t88
vecprod