home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 9.ddi / CHAPXMPL.ZIP / STRINGUP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  3.2 KB  |  84 lines

  1. // Turbo Assembler    Copyright (c) 1988, 1991 By Borland International, Inc.
  2.  
  3. /* STRINGUP.C
  4.    Program to demonstrate the use of StringToUpper().
  5.    Calls StringToUpper to convert TestString to uppercase in
  6.    UpperCaseString, then prints UpperCaseString and its length.
  7. */
  8.  
  9. // From the Turbo Assembler Users Guide - Interfacing Turbo Assembler
  10. //                                         with Borland C++
  11.  
  12.  
  13. #pragma inline
  14. #include <stdio.h>
  15.  
  16. /* Function prototype for StringToUpper() */
  17. extern unsigned int StringToUpper(
  18. unsigned char far * DestFarString,
  19. unsigned char far * SourceFarString);
  20.  
  21. #define MAX_STRING_LENGTH 100
  22.  
  23. char *TestString = "This Started Out As Lowercase!";
  24.  
  25. char UpperCaseString[MAX_STRING_LENGTH];
  26.  
  27. main()
  28. {
  29.    unsigned int StringLength;
  30.  
  31.    /* Copy an uppercase version of TestString to UpperCaseString*/
  32.    StringLength = StringToUpper(UpperCaseString, TestString);
  33.  
  34.    /* Display the results of the conversion */
  35.    printf("Original string:\n%s\n\n", TestString);
  36.    printf("Uppercase string:\n%s\n\n", UpperCaseString);
  37.    printf("Number of characters: %d\n\n", StringLength);
  38. }
  39.  
  40. /* Function to perform high-speed translation to uppercase
  41.    from one far string to another
  42.  
  43.    Input:
  44.         DestFarString   - array in which to store uppercased
  45.                           string (will be zero-terminated)
  46.         SourceFarString - string containing characters to be
  47.                           converted to all uppercase (must be
  48.                           zero-terminated)
  49.  
  50.    Returns:
  51.         The length of the source string in characters, not
  52.         counting the terminating zero. */
  53.  
  54. unsigned int StringToUpper(unsigned char far * DestFarString,
  55.                            unsigned char far * SourceFarString)
  56. {
  57.    unsigned int  CharacterCount;
  58.  
  59.    #define LOWER_CASE_A 'a'
  60.    #define LOWER_CASE_Z 'z'
  61.       asm ADJUST_VALUE  EQU  20h;    /* amount to subtract from lowercase 
  62.                                         letters to make them uppercase */
  63.       asm  cld;
  64.       asm  push ds;                  /* save C's data segment */
  65.       asm  lds  si,SourceFarString;  /* load far pointer to source string */
  66.       asm  les  di,DestFarString;    /* load far pointer to destination string */
  67.       CharacterCount = 0;            /* count of characters */
  68.    StringToUpperLoop:
  69.       asm  lodsb;                    /* get the next character */
  70.       asm  cmp  al,LOWER_CASE_A;     /* if < a then it's not a lowercase letter */
  71.       asm  jb   SaveCharacter;
  72.       asm  cmp  al,LOWER_CASE_Z;     /* if > z then it's not a lowercase letter */
  73.       asm  ja   SaveCharacter;
  74.       asm  sub  al,ADJUST_VALUE;     /* it's lowercase; make it uppercase */
  75.    SaveCharacter:
  76.       asm  stosb;                    /* save the character */
  77.       CharacterCount++;              /* count this character */
  78.       asm  and  al,al;               /* is this the ending zero? */
  79.       asm  jnz  StringToUpperLoop;   /* no, process the next character, if any */
  80.       CharacterCount--;              /* don't count the terminating zero */
  81.       asm  pop  ds;                  /* restore C's data segment */
  82.       return(CharacterCount);
  83. }
  84.