Hardware Reference
In-Depth Information
we see that the error is on line 7, which contains the SHR instruction. Comparing
this code with the instruction table in Fig. C-4 shows the problem: the shift count
has been omitted. The corrected line 7 should read
SHR CX,1
It is very important to note that the error must be corrected in the original source
file, arrayprt.s , and not in the combined source, arrayprt.$ , as the latter is automat-
ically regenerated every time the assembler is called.
The next attempt to assemble the source code file should succeed.
Then the
tracer can be started by the command:
t88 arrayprt
During the tracing process, we can see that the output is not consistent with the
vector in the data segment. The vector contains: 3, 4, 7, 11, 3, but the values dis-
played start with: 3, 1024, ... . Clearly, something is wrong.
To find the error, the tracer can be run again, step by step, examining the state
of the machine just before the incorrect value is printed. The value to be printed is
stored in memory on lines 32 and 33. Since the wrong value is being printed, this
is a good place to see what is wrong. The second time through the loop, we see
that SI is an odd number, when clearly it should be an even number, as it is index-
ing through words, not bytes. The problem is on line 35. It increments SI by 1; it
should increment it by 2. To fix the bug, this line should be changed to
ADD SI,2
When this correction is made, the printed list of numbers is correct.
However, there is one more error waiting for us. When vecprint is finished and
returns, the tracer complains about the stack pointer. The obvious thing to check
for now is whether the value pushed onto the stack when vecprint is called is the
value on top of the stack when the RET on line 41 is executed. It is not. The solu-
tion is to replace line 40 with two lines:
ADD SP,10
POP BP
The first instruction removes the 5 words pushed onto the stack during vecprint ,
thus exposing the value of BP saved on line 22. By popping this value to BP ,we
restore BP to its precall value and expose the correct return address. Now the pro-
gram terminates correctly. Debugging assembly code is definitely more of an art
than a science, but the tracer makes it much easier than running on the bare metal.
C.8.5 String Manipulation and String Instructions
The main purpose of this section is to show how to handle repeatable string
instructions. In Fig. C-17, there are two simple string manipulation programs,
strngcpy.s and reverspr.s , both present in the examples directory.
The one in
 
Search WWH ::




Custom Search