home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / pgmutil / val_link.zip / ASMSUBS.C next >
Text File  |  1989-02-18  |  14KB  |  435 lines

  1. /*                                ASMSUBS.C                                 */
  2. #include <stdio.h>
  3. #include <stdarg.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include <dos.h>
  7. #include <string.h>
  8.  
  9. #include "langext.h"
  10. #include "defines.h"
  11. #include "types.h"
  12. #include "subs.h"
  13.  
  14. #pragma inline
  15.  
  16. /*+-------------------------------------------------------------------------+
  17.   |                                                                         |
  18.   |                               checksum                                  |
  19.   |                                                                         |
  20.   +-------------------------------------------------------------------------+*/
  21. bit_16 checksum(bit_16 len, byte *sym)
  22. BeginDeclarations
  23. EndDeclarations
  24. BeginCode
  25. /* The following assembly code is the equivalent of the following C code:
  26.  For i=0; i LessThan len; i++
  27.   BeginFor
  28.    sum += sym[i];
  29.   EndFor;
  30.  return(sum); */
  31.  
  32.  asm            mov     si,sym
  33.  asm            mov     cx,len
  34.  asm            xor     ax,ax
  35.  asm            mov     bx,ax
  36. hash_loop:
  37.  asm            mov     bl,[si]
  38.  asm            add     ax,bx
  39.  asm            inc     si
  40.  asm            loop    hash_loop
  41.  return(_AX);
  42. EndCode
  43.  
  44. /*+-------------------------------------------------------------------------+
  45.   |                                                                         |
  46.   |                          far_compare                                    |
  47.   |                                                                         |
  48.   +-------------------------------------------------------------------------+*/
  49. int_16 far_compare (byte_ptr left, byte_ptr right, bit_16 len)
  50. BeginDeclarations
  51. EndDeclarations
  52. BeginCode
  53. /* The following assembly code is the equivalent of the following C code:
  54.  While len-- IsNotZero
  55.   BeginWhile
  56.    If *left IsNotEqualTo *right
  57.     Then
  58.      If *left LessThan *right
  59.       Then
  60.        return(-1);
  61.       Else
  62.        return(1);
  63.       EndIf;
  64.     EndIf;
  65.    left++;
  66.    right++;
  67.   EndWhile;
  68.  return(0); */
  69.  
  70.  asm            cld
  71.  asm            push    ds
  72.  asm            mov     cx,len
  73.  asm            les     di,right
  74.  asm            lds     si,left
  75.  asm    rep     cmpsb
  76.  asm            pop     ds
  77.  asm            jb      left_less_than_right
  78.  asm            ja      left_greater_than_right
  79.  
  80.  return(0);
  81. left_less_than_right:
  82.  return(-1);
  83. left_greater_than_right:
  84.  return(1);
  85. EndCode
  86.  
  87. /*+-------------------------------------------------------------------------+
  88.   |                                                                         |
  89.   |                            far_index                                    |
  90.   |                                                                         |
  91.   +-------------------------------------------------------------------------+*/
  92. bit_16 far_index(byte_ptr dest, byte c)
  93. BeginDeclarations
  94. bit_16                                 i;
  95. EndDeclarations
  96. BeginCode
  97. /* The following assembly code is the equivalent of the following C code:
  98.  For i=0; (*dest++ IsNotEqualTo c) AndIf (i IsNotEqualTo 0xFFFF); i++
  99.   BeginFor
  100.   EndFor;
  101.  return(i); */
  102.  
  103.  asm            xor     cx,cx
  104.  asm            mov     al,c
  105.  asm            les     di,dest
  106. search_loop:
  107.  asm            cmp     al,es:[di]
  108.  asm            je      search_done
  109.  asm            inc     di
  110.  asm            inc     cx
  111.  asm            jne     search_loop
  112.  asm            dec     cx
  113. search_done:
  114.  asm            mov     i,cx
  115.  
  116.  return(i);
  117. EndCode
  118.  
  119. /*+-------------------------------------------------------------------------+
  120.   |                                                                         |
  121.   |                              far_match                                  |
  122.   |                                                                         |
  123.   +-------------------------------------------------------------------------+*/
  124. bit_16 far_match (byte_ptr pattern, byte_ptr s, bit_16 len)
  125. BeginDeclarations
  126. EndDeclarations
  127. BeginCode
  128. /* The following assembly code is the equivalent of the following C code:
  129.  While len Exceeds 0
  130.   BeginWhile
  131.    If *pattern Is '*'
  132.     Then
  133.      return(True);
  134.     EndIf;
  135.    If (*pattern IsNot '?') AndIf (*pattern IsNot *source)
  136.     Then
  137.      return(False);
  138.     EndIf;
  139.    source++;
  140.    pattern++;
  141.   EndWhile;
  142.  return(True); */
  143.  
  144.  asm            mov     cx,len
  145.  asm            les     di,pattern
  146.  asm            push    ds
  147.  asm            lds     si,s
  148. pattern_loop:
  149.  asm            mov     al,es:[di]
  150.  asm            cmp     al,'?'
  151.  asm            je      a_match
  152.  asm            cmp     al,'*'
  153.  asm            je      succeeded
  154.  asm            cmp     al,[si]
  155.  asm            jne     failed
  156. a_match:
  157.  asm            inc     si
  158.  asm            inc     di
  159.  asm            loop    pattern_loop
  160. succeeded:
  161.  asm            pop     ds
  162.  return(True);
  163. failed:
  164.  asm            pop     ds
  165.  return(False);
  166. EndCode
  167.  
  168. /*+-------------------------------------------------------------------------+
  169.   |                                                                         |
  170.   |                          far_move                                       |
  171.   |                                                                         |
  172.   +-------------------------------------------------------------------------+*/
  173. byte_ptr far_move (byte_ptr dest, byte_ptr source, bit_16 len)
  174. BeginDeclarations
  175. EndDeclarations
  176. BeginCode
  177. /* The following assembly code is the equivalent of the following C code:
  178.  While len-- IsNotZero
  179.   BeginWhile
  180.    *dest++ = *source++;
  181.   EndWhile; */
  182.  
  183.  asm            cld
  184.  asm            push    ds
  185.  asm            mov     cx,len
  186.  asm            les     di,dest
  187.  asm            lds     si,source
  188.  asm    rep     movsb
  189.  asm            pop     ds
  190.  
  191.  return(dest);
  192. EndCode
  193.  
  194. /*+-------------------------------------------------------------------------+
  195.   |                                                                         |
  196.   |                          far_move_left                                  |
  197.   |                                                                         |
  198.   +-------------------------------------------------------------------------+*/
  199. byte_ptr far_move_left (byte_ptr dest, byte_ptr source, bit_16 len)
  200. BeginDeclarations
  201. EndDeclarations
  202. BeginCode
  203. /* The following assembly code is the equivalent of the following C code:
  204.  dest   += len - 1;
  205.  source += len - 1;
  206.  While len-- IsNotZero
  207.   BeginWhile
  208.    *dest-- = *source--;
  209.   EndWhile; */
  210.  
  211.  asm            std
  212.  asm            push    ds
  213.  asm            mov     cx,len
  214.  asm            les     di,dest
  215.  asm            lds     si,source
  216.  asm            add     di,cx
  217.  asm            add     si,cx
  218.  asm            dec     si
  219.  asm            dec     di
  220.  asm    rep     movsb
  221.  asm            pop     ds
  222.  
  223.  return(dest);
  224. EndCode
  225.  
  226. /*+-------------------------------------------------------------------------+
  227.   |                                                                         |
  228.   |                                far_set                                  |
  229.   |                                                                         |
  230.   +-------------------------------------------------------------------------+*/
  231. byte_ptr far_set (byte_ptr dest, byte source, bit_16 len)
  232. BeginDeclarations
  233. EndDeclarations
  234. BeginCode
  235. /* The following assembly code is the equivalent of the following C code:
  236.  While len-- IsNotZero
  237.   BeginWhile
  238.    *dest++ = source;
  239.   EndWhile; */
  240.  
  241.  asm            cld
  242.  asm            mov     cx,len
  243.  asm            les     di,dest
  244.  asm            mov     al,source
  245.  asm            stosb
  246.  asm            dec     cx
  247.  asm    rep     stosb
  248.  
  249.  return(dest);
  250. EndCode
  251.  
  252. /*+-------------------------------------------------------------------------+
  253.   |                                                                         |
  254.   |                          far_to_lower                                   |
  255.   |                                                                         |
  256.   +-------------------------------------------------------------------------+*/
  257. byte_ptr far_to_lower (byte_ptr dest, bit_16 len)
  258. BeginDeclarations
  259. EndDeclarations
  260. BeginCode
  261. /* The following assembly code is the equivalent of the following C code:
  262.  While len-- IsNotZero
  263.   BeginWhile
  264.    tolower(*dest++);
  265.   EndWhile; */
  266.  
  267.  asm            mov     cx,len
  268.  asm            les     di,dest
  269. lower_case_loop:
  270.  asm            mov     al,es:[di]
  271.  asm            cmp     al,'A'
  272.  asm            jb      next_byte
  273.  asm            cmp     al,'Z'
  274.  asm            ja      next_byte
  275.  asm            add     al,'a'-'A'
  276.  asm            mov     es:[di],al
  277. next_byte:
  278.  asm            inc     di
  279.  asm            loop    lower_case_loop
  280.  
  281.  return(dest);
  282. EndCode
  283.  
  284. /*+-------------------------------------------------------------------------+
  285.   |                                                                         |
  286.   |                         library_directory_hash                          |
  287.   |                                                                         |
  288.   +-------------------------------------------------------------------------+*/
  289. void library_directory_hash(byte_ptr      sym,
  290.                             bit_16        len,
  291.                             bit_16       *starting_block,
  292.                             bit_16       *delta_block,
  293.                             bit_16       *starting_entry,
  294.                             bit_16       *delta_entry)
  295. BeginDeclarations
  296. EndDeclarations
  297. BeginCode
  298. /* The following assembly code is the equivalent of the following C code:
  299. BeginDeclarations
  300. byte                                  *beg_str;
  301. byte                                  *end_str;
  302. bit_16                                 c;
  303. byte                                   temp[33];
  304. EndDeclarations
  305. BeginCode
  306.  beg_str = temp;
  307.  end_str = Addr(temp[len]);
  308.  temp[0] = Byte(len);
  309.  far_move(BytePtr(Addr(temp[1]), sym, len);
  310.  *starting_block =
  311.  *delta_block    =
  312.  *starting_entry =
  313.  *delta_entry    = 0;
  314.  While len-- Exceeds 0
  315.   BeginWhile
  316.    c = (Bit_16(*beg_str++) And 0xFF) Or 0x20;
  317.    *starting_block = c Xor ((*starting_block ShiftedLeft   2)  Or
  318.                             (*starting_block ShiftedRight 14));
  319.    *delta_entry    = c Xor ((*delta_entry    ShiftedRight  2)  Or
  320.                             (*delta_entry    ShiftedLeft  14));
  321.    c = (Bit_16(*end_str--) And 0xFF) Or 0x20;
  322.    *delta_block    = c Xor ((*delta_block    ShiftedLeft   2)  Or
  323.                             (*delta_block    ShiftedRight 14));
  324.    *starting_entry = c Xor ((*starting_entry ShiftedRight  2)  Or
  325.                             (*starting_entry ShiftedLeft  14));
  326.   EndWhile; */
  327.  asm            xor     ax,ax
  328.  asm            mov     cl,2
  329.  asm            xor     bx,bx
  330.  asm            xor     dx,dx
  331.  asm            mov     bl,len
  332.  asm            mov     dl,len
  333.  asm            or      bx,0x20
  334.  asm            or      dx,0x20
  335.  asm            les     si,sym
  336.  asm            mov     di,len
  337.  asm            dec     di
  338. up_loop:
  339.  asm            mov     al,es:[si]
  340.  asm            or      al,0x20
  341.  asm            rol     bx,cl
  342.  asm            ror     dx,cl
  343.  asm            xor     bx,ax
  344.  asm            xor     dx,ax
  345.  asm            inc     si
  346.  asm            dec     di
  347.  asm            jnz     up_loop
  348.  asm            mov     di,starting_block
  349.  asm            mov     [di],bx
  350.  asm            mov     di,delta_entry
  351.  asm            mov     [di],dx
  352.  asm            mov     di,len
  353.  asm            xor     bx,bx
  354.  asm            xor     dx,dx
  355. down_loop:
  356.  asm            mov     al,es:[si]
  357.  asm            or      al,0x20
  358.  asm            rol     bx,cl
  359.  asm            ror     dx,cl
  360.  asm            xor     bx,ax
  361.  asm            xor     dx,ax
  362.  asm            dec     si
  363.  asm            dec     di
  364.  asm            jnz     down_loop
  365.  asm            mov     di,delta_block
  366.  asm            mov     [di],bx
  367.  asm            mov     di,starting_entry
  368.  asm            mov     [di],dx
  369.  return;
  370. EndCode
  371.  
  372. /*+-------------------------------------------------------------------------+
  373.   |                                                                         |
  374.   |                              word_checksum                              |
  375.   |                                                                         |
  376.   +-------------------------------------------------------------------------+*/
  377. bit_16 word_checksum(bit_16 len, bit_16 address, byte_ptr data)
  378. BeginDeclarations
  379. EndDeclarations
  380. BeginCode
  381. /* The following assembly code is the equivalent of the following C code:
  382. BeginDeclarations
  383. Structure word_struct
  384.  BeginStructure
  385.   bit_8                                low_byte;
  386.   bit_8                                high_byte;
  387.  EndStructure;
  388. bit_16                                 sum;
  389. Structure word_struct                  word;
  390. bit_16                                *word_ptr;
  391. EndDeclarations
  392.  word = (bit_16 *) Addr(word);
  393.  For i=0; i LessThan len; i++
  394.   BeginFor
  395.    *word_ptr = 0;
  396.    If (address-- And 1)
  397.     Then
  398.      word.high_byte = *data++;
  399.     Else
  400.      word.low_byte = *data++;
  401.     EndIf;
  402.    sum += *word_ptr;
  403.   EndFor;
  404.  return(sum); */
  405.  
  406.  asm            xor     ax,ax
  407.  asm            les     di,data
  408.  asm            mov     bx,address
  409.  asm            mov     cx,len
  410. /* Handle case where we are starting at an odd location */
  411.  asm            and     bx,1
  412.  asm            je      at_even_location
  413.  asm            mov     ah,es:[di]
  414.  asm            inc     di
  415.  asm            dec     cx
  416. at_even_location:
  417. /* Loop while we have at least two bytes left */
  418.  asm            cmp     cx,2
  419.  asm            jb      at_end_of_sum
  420.  asm            add     ax,es:[di]
  421.  asm            inc     di
  422.  asm            inc     di
  423.  asm            dec     cx
  424.  asm            loop    at_even_location
  425. at_end_of_sum:
  426.  asm            cmp     cx,1
  427.  asm            jne     checksum_done
  428.  asm            add     al,es:[di]
  429.  asm            adc     ah,0
  430. checksum_done:
  431.  return(_AX);
  432. EndCode
  433.  
  434.  
  435.