home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / SWAG9605.DDD / 0119_Copy SubString.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-31  |  2.5 KB  |  50 lines

  1. CSUBSTR.PAS  in MISC.SWG     0123 01/01 37/47   79%
  2.  
  3. Bug: if nrchars specifies more characters than remain starting at the
  4.      start position, Str2 will contain nrchars characters. This is fixed.
  5. }
  6. procedure CopySubStr( Str1: string; start, nrchars: byte; var Str2: string );
  7. assembler;
  8.   { copy part of Str1 (beginning at start for nrchars) to Str2
  9.     if start > length of Str1, Str2 will contain a empty string.
  10.     if nrchars specifies more characters than remain starting at the
  11.     start position, Str2 will contain just that remainder of Str1. }
  12. asm     { setup }
  13.         push  ds           { save DS                       }
  14.         cld                { string operations forward     }
  15.         lds   si, str1     { load in DS:SI pointer to str1 }
  16.         les   di, str2     { load in ES:DI pointer to str2 }
  17.         mov   ah, [si]     { length str1 --> AH            }
  18.         and   ah, ah       { length str1 = 0?              }
  19.         je    @null        { yes, empty string in Str2     }
  20.         mov   bl, [start]  { starting position --> BL      }
  21.         cmp   ah, bl       { start > length str1?          }
  22.         jb    @null        { yes, empty string in Str2     }
  23.  
  24.         { start + nrchars - 1 > length str1?               }
  25.         mov   al, [nrchars]{ nrchars --> AL                }
  26.         mov   dh, al       { nrchars                       }
  27.         add   dh, bl       { + start                       }
  28.         jc    @rest        { if overflow copy rest of str1 }
  29.         dec   dh           { - 1                           }
  30.         cmp   ah, dh       { nrchars > rest of str1?       }
  31.         jb    @rest        { yes, copy rest of str1        }
  32.         jmp   @copy
  33. @null:  xor   ax, ax       { return a empty string         }
  34.         jmp   @done
  35. @rest:  sub   ah, bl       { length str1 - start           }
  36.         inc   ah
  37.         mov   al, ah
  38. @copy:  mov   cl, al       { how many chars to copy        }
  39.         xor   ch, ch       { clear CH                      }
  40.         xor   bh, bh       { clear BH                      }
  41.         add   si, bx       { starting position             }
  42.         mov   dx, di       { save pointer to str2          }
  43.         inc   di           { don't overwrite length str2   }
  44.     rep movsb              { copy part str1 to str2        }
  45.         mov   di, dx       { restore pointer to str2       }
  46. @done:  mov   [di], al     { overwrite length byte of str2 }
  47. @exit:  pop   ds           { restore DS                    }
  48. end  { CopySubStr };
  49.  
  50.