home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / SWAB.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  2.2 KB  |  63 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - swab.c
  3.  *
  4.  * function(s)
  5.  *      swab -  swaps bytes
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18. #pragma inline
  19. #include <asmrules.h>
  20.  
  21. #define I asm
  22.  
  23. /*---------------------------------------------------------------------*
  24. Name        swab -    swaps bytes
  25.  
  26. Usage        void swab(char *from, char *to, int nbytes);
  27.  
  28. Prototype in    stdlib.h
  29.  
  30. Description    swab copies n bytes from the from string to the to
  31.         string. Adjacent even- and odd-byte positions are swapped.
  32.         This is useful for moving data from one machine to another
  33.         machine with a different byte order. nbytes should be even.
  34.  
  35. Return value    There is no return value.
  36. *---------------------------------------------------------------------*/
  37. void swab(char *from, char *to, int nbytes)
  38. {
  39. I    mov    cx, nbytes        /* BX <- nbytes         */
  40. I    shr    cx, 1            /* Convert bytes -> words    */
  41. I    jcxz    exit_swab        /* If degenerate case, exit.    */
  42. #if (defined(__COMPACT__) || defined(__LARGE__))
  43. I    push    ds
  44. #endif
  45. #if (defined(__MEDIUM__) || defined(__SMALL__) || defined(__TINY__))
  46. I    push    ds
  47. I    pop    es
  48. #endif
  49. I    cld                /* Make string ops go forward    */
  50. I    LES_    di, to            /* ES:DI <- destination     */
  51. I    LDS_    si, from        /* DS:SI <- source        */
  52. next_word:
  53. I    lodsw                /* Load word from source string */
  54. I    xchg    ah, al            /* Swap the bytes        */
  55. I    stosw                /* Store result in destination    */
  56. I    loop    next_word        /* Do the next word        */
  57. #if (defined(__COMPACT__) || defined(__LARGE__))
  58. I    pop    ds
  59. #endif
  60. exit_swab:
  61.         return;
  62. }
  63.