Regions 2
Volume Number: 4
Issue Number: 9
Column Tag: Pascal Procedures
Fun With Regions, Part II
By Stephen Dubin, V.M.D., Ph.D.,, Thomas W. Moore, PH.D., Drexel
University, Sheel Kishore, MS
In our previous paper (Fun with Regions Part I: High Level Language
Implementation), we showed how it was possible to estimate the area of an arbitrarily
drawn region from high level languages such as Pascal and C using repeated application
of the ROM subroutine, PtInRgn. Although this approach is simple and intuitive,
execution time is excessive for large or complex regions. Whenever part of a high
level language routine consumes an inconveniently long execution time, the possibility
of using assembly language to achieve better efficiency should be considered. Two
fundamental approaches may be applied. A study of the code generated by the compiler
may reveal unnecessary looping, inefficient use of registers or other complexities
which can be streamlined. If savings can be made within loops executed many times,
the resulting speedup can be significant. The second approach applies when the task at
hand is relatively simple and straightforward. In this case, determination of a specific
efficient algorithm is the key, with translation to assembly code following directly. In
dealing with the area computation part of our regions manipulation, we have explored
both of these approaches and found the latter approach to be clearly superior.
In the first article, we provided the C Language code for the area calculation
program at the end of the article with major subroutines interpolated into the text as
implemented in Pascal. In this installment, the “tables are turned”; the interpolated
routines are implemented for the Megamax C development system and the Consulair
(MDS) assembly language system [This is included in the source code due to space
considerations-ED]. At the end of the article, a program using the most useful of the
assembly language optimizations is shown for Turbo Pascal with explanation of minor
changes needed for the TML Pascal Development system. We recognize that our
program may not be the most elegant or efficient approach to the problems; but even
where an attempt at optimization yielded poor or marginal results, an interesting and
- hopefully - useful technique is explored.
Figure 1.
Several authors ( Morton, M.: Reduce Your Time in the Traps! MacTutor October
1986 pp 21-24. and Knaster, S.: “How to Write Macintosh Software.” Hayden,
Hasbrouck Heights NJ, 1986, p 368 ) have advocated bypassing the trap dispatcher as
a means of speeding routines in which ROM calls are made repeatedly. Certainly our
CountPix routine, since it calls PtInRgn for every point within the region bounding