home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TBTREE.ZIP / MATH.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-07-25  |  7.1 KB  |  150 lines

  1. (* TBTree13             Copyright (c)  1988            Dean H. Farwell II    *)
  2.  
  3. unit Math;
  4.  
  5. (*****************************************************************************)
  6. (*                                                                           *)
  7. (*                  M A T H M A T I C A L  R O U T I N E S                   *)
  8. (*                                                                           *)
  9. (*****************************************************************************)
  10.  
  11.  
  12. (* These routines augment the Turbo Pascal routines provided.
  13.  
  14.  
  15. (* Version Information
  16.  
  17.    Version 1.1 - No Changes
  18.  
  19.    Version 1.2 - No Changes
  20.  
  21.    Version 1.3 - Modified 11 July 1988  by Jack Dolby
  22.                                            Gately Communication Company
  23.                                            501 Industry Drive
  24.                                            Hampton, VA  23661
  25.  
  26.        These are inline assembly language macros to perform the same function
  27.        as the Pascal versions in versions prior to 1.3.
  28.  
  29.        Note that the entire code for inline macros must be in the INTERFACE
  30.        section.  The code in these macros is actually placed in the code the
  31.        compiler emits at each place they are invoked. This results in larger
  32.        code but eliminates the time overhead for calling a routine and
  33.        returning from it.  Only the pushing of parameters and extracting the
  34.        functional result are done rather than also pushing the return address,
  35.        preserving registers in the code, setting up a stack frame, and
  36.        restoring all registers and the stack on return.  This results in
  37.        faster code at the expense of code size (if called from more than one
  38.        place) and in loosing the ability to hide the code in the
  39.        IMPLEMENTATION SECTION.     j. dolby
  40.  
  41.        My special thanks goes to j. dolby who took the time to make these
  42.        improvements and graciously allowed me to include them for
  43.        distribution!!      Dean H. Farwell II                                *)
  44.  
  45. (*\*)
  46. (*////////////////////////// I N T E R F A C E //////////////////////////////*)
  47.  
  48. interface
  49.  
  50. (* This function calculates and returns x ** y (x to the power of y) where
  51.    both x and y are Integers.  This function has not been tested with negative
  52.    parameters.
  53.  
  54.    The author of the assembly version (j dolby) has tested this
  55.    routine over the range of 0...32767.  All answers are numerically
  56.    the same as the original routine.  However, some answers that are
  57.    above the upper limit of 32767 will report that they are not the
  58.    same.  In these cases, the numeric value in the returned 16 bits
  59.    are the same but Turbo Pascal will report them to be different.  It
  60.    is apparently either the upper 16 bits in DX (which is discarded)
  61.    or the status flags that are causing this apparent error.  However,
  62.    the integer returned is the for these out of range answers and
  63.    there is an exact Turbo Pascal equivalence for all answers in the
  64.    range 0...32767.
  65.  
  66.    In the big picture of things this routine was only included
  67.    originally because I needed it for the other two routines.  Now,
  68.    with the assembly versions of the other two routines, this routine
  69.    is not required.  I am leaving it in just in case someone is using
  70.    it for something else.  -- Dean                                           *)
  71.  
  72. function PowerInt(x,y : Integer) : Integer;
  73.  
  74.     INLINE
  75.          (
  76.          $59/          { pop cx                                  get power y }
  77.          $5B/          { pop bx                                 get number x }
  78.          $B8/$01/$00/  { mov ax,1                       set up for first mul }
  79.          $E3/$04/      { jcxz exit         exit with answer = 1 if power = 0 }
  80.          $F7/$E3/      { mul bx               dx:ax := ax * bx               }
  81.          $E2/$FC       { loop                 dec cx and do mul until cx = 0 }
  82.          );
  83.  
  84. (*\*)
  85. (* the following type supports the byte handling routine(s)                  *)
  86.  
  87. type
  88.     BytePosition = 0 .. 7;
  89.     BitValue = 0 .. 1;
  90.  
  91.  
  92. (* This function will determine if a certain bit within a target byte is
  93.    toggled on (equal to 1).  The bit position is is the position within the
  94.    byte of the bit to be tested.  The least significant bit is 0 then most
  95.    significant bit is 7.  If the bit is 1 TRUE will be returned.  If the bit
  96.    is 0 FALSE will be returned.  Notice that the target byte can be of any
  97.    type.  in this way, the routine will handle any a bit byte.  In other
  98.    words a character could also be passed in.                                *)
  99.  
  100. (* Boolean functions return zero flag set and AL=0 for false,
  101.                             zero flag reset and AL=1 for true                *)
  102.  
  103. function BitOn(var targetByte;
  104.                bitNum : BytePosition) : Boolean;
  105.  
  106.   INLINE (
  107.            $59/                { pop cx       get bitnum                }
  108.            $5B/                { pop bx       get offset of targetbyte  }
  109.            $07/                { pop es       get segment of targetbyte }
  110.            $26/                { es           use extra seg for data    }
  111.            $8A/$07/            { mov al,[bx]  get targetbyte            }
  112.            $B2/$01/            { mov dl,01    start set up of mask      }
  113.            $D2/$E2/            { shl dl,cl    put 1 in position bitnum  }
  114.            $20/$D0/            { and al,dl    apply mask                }
  115.            $74/$03/            { jz exit      done if zero              }
  116.            $B8/$01/$00         { mov ax,01    set accumulator = 1       }
  117.          );
  118.  
  119. (*\*)
  120. (* This will set a given bit to a value of zero or one depending on what is
  121.    passed in as the last parameter.  See above for description of the other
  122.    parameters                                                                *)
  123.  
  124. procedure SetBit(var targetByte;
  125.                  bitNum : BytePosition;
  126.                  bit : BitValue);
  127.  
  128.   INLINE (
  129.            $58/            { pop ax                  get bitvalue             }
  130.            $59/            { pop cx                  get bitnum               }
  131.            $5B/            { pop bx                  get offset of targetbyte }
  132.            $07/            { pop es                  get seg of targetbyte    }
  133.            $B2/$01/        { mov dl,1                set bit for mask         }
  134.            $D2/$E2/        { shl dl,cl               move mask bit to proper  }
  135.                            {                         position                 }
  136.            $26/            { es                      use extra seg for data   }
  137.            $08/$17/$0A/    { or byte pointer[bx],dl  set bit regardless       }
  138.            $C0/            { or al,al                should it be a one?      }
  139.            $75/$03/        { jnz exit                yes-exit else reset bit  }
  140.            $26/            { es                      use extra seg for data   }
  141.            $30/$17         { xor byte pointer[bx],dl reset bit                }
  142.          );
  143.  
  144.  
  145. (*///////////////////// I M P L E M E N T A T I O N /////////////////////////*)
  146.  
  147. implementation
  148.  
  149. end.                                                     (* end of Math unit *)
  150.