home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / tasm / ifindrep.asm < prev    next >
Assembly Source File  |  1988-08-28  |  3KB  |  74 lines

  1.  
  2. ;   FILENAME: IFINDREP.ASM
  3. ;
  4. ;   Copyright (c) 1988 by Borland International, Inc.
  5. ;
  6. ;   DESCRIPTION: This module implements the routine FindAndReplace. The
  7. ;   routine will search through an array of bytes replacing all instances
  8. ;   of a particular value with an alternative value. This module uses
  9. ;   ideal mode syntax.
  10. ;
  11. ;   ASSEMBLY INSTRUCTIONS: To assemble this module use the following TASM
  12. ;   command line:
  13. ;
  14. ;       TASM /dMDL=memorymodel Ifindrep
  15. ;
  16. ;   'memorymodel' in the above command line may be replaced by TINY, SMALL,
  17. ;   MEDIUM, COMPACT, LARGE or HUGE.
  18.  
  19. %tabsize 4
  20.  
  21. ifndef  MDL
  22.     display "Error: This module requires that you provide a memory model"
  23.     display "       definition on the command line. I.E. /dMDL=SMALL."
  24.     err ; Force a fatal error
  25. else
  26.  
  27.     ideal           ; Use TASM's Ideal mode
  28.     model   MDL     ; Define the memory model
  29.  
  30.     codeseg
  31.  
  32.     global  FindAndReplace:proc ; Declare public proc
  33.  
  34.     proc    FindAndReplace
  35.  
  36.     ;   This routine searches through an array of byte values looking for
  37.     ;   the value indicated in al. Whenever it finds a match it replaces the
  38.     ;   byte with the value stored in ah. Note that this routine assumes that
  39.     ;   the pointer passed to it is normalized if the array being searched
  40.     ;   is longer than 0FFF0h bytes.
  41.     ;
  42.     ;   Input
  43.     ;       ArrayPtr - far pointer to array
  44.     ;       al - unsigned byte value to search for
  45.     ;       ah - unsigned byte value to replace with
  46.     ;       cx - maximum number of bytes to search.
  47.     ;   Output
  48.     ;       none
  49.     ;   Calling convention
  50.     ;       Pascal
  51.     ;   Registers modified
  52.     ;       cx, di, es, Flags
  53.  
  54.     arg ArrayPtr:dword=PARM_SIZE    ; Define parameter passed on stack
  55.  
  56.         push    bp                  ; Set up the stack to access parameters
  57.         mov     bp, sp
  58.         les     di, [ArrayPtr]      ; Get the address of the array
  59.         cld                         ; Search from low address to high address
  60.     ReplaceLoop:
  61.         repne   scasb               ; Find a matching byte
  62.         jcxz    short Exit          ; At end of array
  63.         mov     [byte es:di], ah    ; Replace byte
  64.         jmp     ReplaceLoop         ; Replace next byte
  65.     Exit:
  66.         pop     bp                  ; Restore stack pointer
  67.         ret     PARM_SIZE           ; Clean up stack since we're using Pascal
  68.                                     ; calling conventions
  69.     endp    FindAndReplace
  70.  
  71. endif   ; ifndef MDL
  72.  
  73. end
  74.