home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / library / psp_io / charstr.asm next >
Assembly Source File  |  1994-05-28  |  5KB  |  120 lines

  1. ;****************************************************************************
  2. ;                         C H A R S T R . A S M
  3. ;============================================================================
  4. ; Functions to manipulate ascii chars and strings. 
  5. ;---------------------------------------------------------------------------
  6. ; Copyright (c) Simon Groes, 1994
  7. ;---------------------------------------------------------------------------
  8. ; Assemble with Borland Turbo Assembler v3.0
  9. ;****************************************************************************
  10.  
  11.         IDEAL
  12.         DOSSEG
  13.         MODEL   small
  14.  
  15.         LOCALS
  16.  
  17. ;-----  Insert INCLUDE "filename" directives here
  18.         INCLUDE "macro.inc"
  19.         INCLUDE "common.inc"
  20.  
  21. ;-----  Insert EQU and = equates here
  22.  
  23.  
  24.         DATASEG
  25.  
  26. ;-----  Declare other variables with DB, DW, etc., here
  27.  
  28. ;-----  Specify any EXTRN variables here
  29.  
  30.         CODESEG
  31.  
  32. ;-----  Declare PUBLIC procedures here
  33.         PUBLIC  charToUpper
  34.         PUBLIC  strLength, strCopy
  35.  
  36.  
  37. ;===============================================================
  38. ; Procedure:    =*=strLength=*=
  39. ;---------------------------------------------------------------
  40. ; Usage:        Public - May be used in this & other asm files.
  41. ; Task:         Return length of string.
  42. ; Input:        di = Offset address of string.
  43. ; Output:       Length of string in cx.
  44. ; Registers:    cx changed
  45. ;===============================================================
  46. PROC    strLength
  47.  
  48.         SaveRegs <es,di,ax>     ; Save modified register(s).
  49.  
  50.         smove   es, ds          ; es = data segment.
  51.         mov     al, NULL        ; End of string marker.
  52.         mov     cx, 0FFFFh      ; cl <- maximum value (FFFFh = -1).
  53.         cld                     ; Auto increment di
  54.         or      cx, cx          ; Reset zero flag
  55.         repnz   scasb           ; cmp di with al, inc di, dec cl
  56.         neg     cx              ; cx = 1 + stringLength + NULL
  57.         dec     cx              ; cx = stringLength + NULL
  58.         dec     cx              ; cx = stringLength
  59.  
  60.         RestoreRegs <ax,di,es>  ; Restore modified register(s).
  61.         ret                     ; Return to caller
  62. ENDP    strLength
  63.  
  64. ;===============================================================
  65. ; Procedure:    =*=charToUpper=*=
  66. ;---------------------------------------------------------------
  67. ; Usage:        Public - may be used by other asm files.
  68. ; Task:         Convert lowercase char to uppercase.
  69. ; Input:        al = character.
  70. ; Output:       al = character in uppercase.
  71. ; Registers:    ax may be changed.
  72. ;===============================================================
  73. PROC    charToUpper
  74.  
  75.         cmp     al, 'a'         ; Is char a lowercase letter?
  76.         jb      @@Return
  77.         cmp     al, 'z'
  78.         ja      @@Return        ; NO -> End routine.
  79.         sub     al, 'a'-'A'     ; YES-> Convert to uppercase.
  80. @@Return:
  81.         ret                     ; Return to caller.
  82. ENDP    charToUpper
  83.  
  84. ;===============================================================
  85. ; Procedure:    =*=strCopy=*=
  86. ;---------------------------------------------------------------
  87. ; Usage:        Public - may be used by other files.
  88. ; Task:         Copy string from one memory location to another.
  89. ; Input:        si = Address of source string (s1).
  90. ;               di = Address of destination (s2).
  91. ; Output:       String copied to new location.
  92. ; Registers:    none
  93. ; Note:         s2 must be large enough to hold s1+NULL.
  94. ;===============================================================
  95. PROC    strCopy
  96.  
  97.         SaveRegs <es,si,di,cx>
  98.  
  99.         smove   es, ds          ; es = data segment.
  100.         xchg    si, di          ; di = s1; Save di in si.
  101.         call    strLength       ; cx = length of s1.
  102.         xchg    si, di          ; Restore si & di.
  103.         cmp     si, di          ; Is si at a lower address than di?
  104.         jb      @@Reverse       ; Yes: Start copying at end of string.
  105.         cld                     ; No:  Auto increment,
  106.         jmp     NEAR    @@Copy  ;  and goto copy routine.
  107. @@Reverse:
  108.         add     si, cx          ; Move si to end of string. si -> NULL.
  109.         add     di, cx          ; Move di to end of string buffer.
  110.         std                     ; Auto-decrement.
  111. @@Copy:
  112.         inc     cx              ; cx = stringlength + 1.
  113.         repnz   movsb           ; Copy string to new location.
  114.  
  115.         RestoreRegs <cx,di,si,es>
  116.         ret
  117. ENDP    strCopy
  118.  
  119.         END                     ; End of module.
  120.