home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / cexpress / strings / replace.asm < prev    next >
Encoding:
Assembly Source File  |  1989-05-04  |  3.6 KB  |  125 lines

  1. ;void  replace(strg,sub_strg,position,chars);
  2. ;  char  *strg,*sub_strg;
  3. ;  unsigned short  position,chars;
  4.  
  5.     EXTRN  _memory_model:byte
  6.     EXTRN  _error_code:byte
  7.  
  8. _TEXT    SEGMENT BYTE PUBLIC 'CODE'
  9.     ASSUME CS:_TEXT
  10.     PUBLIC _replace
  11. _replace proc near
  12.     push bp            ;
  13.     mov  bp,sp        ;set up stack frame
  14.     push di            ;
  15.     push si            ;
  16.     push ds            ;
  17.     cmp  _memory_model,0    ;near or far?
  18.     jle  begin        ;jump if near
  19.     inc  bp            ;else add 2 to BP
  20.     inc  bp            ;
  21. begin:    mov  _error_code,1    ;1 = error has occured
  22.     cmp  _memory_model,2    ;data near or far?
  23.     jb   A1            ;jump if near    
  24.     les  di,dword ptr[bp+4] ;ES:DI pts to strg
  25.     lds  si,dword ptr[bp+8] ;DS:SI pts to substrg
  26.     add  bp,4        ;add 4 to BP since 2 dword ptrs
  27.     jmp  short B1        ;
  28. A1:    mov  di,[bp+4]        ;NEAR case
  29.     mov  si,[bp+6]        ;
  30.     mov  ax,ds        ;ES = DS
  31.     mov  es,ax        ;
  32. B1:    cmp  word ptr[bp+10],0    ;test for zero-width field
  33.     je   C1            ;quit if zero
  34.     cmp  byte ptr es:[di],0    ;strg null?
  35.     jne  D1            ;
  36.     cmp  byte ptr [si],0    ;substrg null?
  37.     jne  D1            ;
  38. C1:    jmp  M1            ;
  39. D1:    cmp  byte ptr[si],0    ;substrg null?
  40.     je   C1            ;
  41.     push di            ;find strg length
  42.     sub  dx,dx        ;
  43. E1:    cmp  byte ptr es:[di],0    ;null?
  44.     je   F1            ;
  45.     inc  di            ;
  46.     inc  dx         ;tally substrg length in DX
  47.     jmp  short E1        ;
  48. F1:    pop  di            ;ptr back to start of string
  49.     push si            ;find substrg length
  50.     sub  bx,bx        ;
  51. G1:    cmp  byte ptr[si],0    ;null?
  52.     je   H1            ;
  53.     inc  si            ;
  54.     inc  bx            ;tally substrg length in BX
  55.     jmp  short G1        ;
  56. H1:    pop  si            ;ptr back to start of string
  57.     mov  cx,[bp+8]        ;fetch position
  58.     cmp  cx,dx        ;position in string?
  59.     jae  M1            ;quit if not
  60.     mov  ax,dx        ;string length to ax
  61.     sub  ax,cx        ;distance from startpt to end of string
  62.     cmp  ax,[bp+10]        ;compare to number chars to replace
  63.     jb   M1            ;quit if out of range       
  64. ;--find out if insertion is larger or smaller than replacement
  65.     cmp  [bp+10],bx        ;compare substring length to Chars
  66.     jne  I1            ;jump ahead if not equal
  67.     add  di,cx        ;add start point to string ptr
  68.     jmp  short L1        ;go write the chars
  69. I1:    push di            ;prepare to shift part of string
  70.     push si            ;point DS:SI to string
  71.     push ds            ;
  72.     mov  ax,es        ;
  73.     mov  ds,ax        ;
  74.     mov  si,di        ;
  75.     cmp  bx,[bp+10]        ;comp substrg to number chars
  76.     jb   J1            ;jump if substring shorter than chars replaced
  77. ;---insertion is longer than replaced chars:
  78.     add  si,dx        ;point si to end of string
  79.     mov  di,si        ;copy to di
  80.     add  di,bx        ;add substring length
  81.     sub  di,[bp+10]        ;minus field width
  82.     sub  dx,[bp+10]        ;plus field width
  83.     sub  dx,[bp+8]        ;minus start point
  84.     inc  dx            ;plus one for null terminator
  85.     mov  cx,dx        ;number chars to transfer
  86.     std            ;direction backwards
  87.     rep  movsb        ;transfer the characters
  88.     cld            ;reset flag
  89.     jmp  short K1        ;go transfer the substring    
  90. ;----insertion is shorter than replaced chars:
  91. J1:    add  di,[bp+8]        ;add start point to di
  92.     add  di,bx        ;plus substring length
  93.     mov  si,di        ;copy to si
  94.     add  si,[bp+10]        ;add field length to si
  95.     sub  si,bx        ;minus substrg length
  96.     mov  cx,dx        ;string length
  97.     add  cx,bx        ;
  98.     sub  cx,[bp+8]        ;minus start point
  99.     sub  cx,[bp+10]        ;minus field width
  100.     inc  cx            ;plus one for null terminator
  101.     cld            ;direction forward
  102.     rep  movsb        ;mov the characters
  103. K1:    pop  ds            ;
  104.     pop  si            ;
  105.     pop  di            ;
  106.     add  di,[bp+8]        ;point di back to field start
  107. ;--write the substring:
  108. L1:    cld            ;direction forward
  109.     mov  cx,bx        ;number chars to write
  110.     rep  movsb        ;write the chars
  111.     pop  ds            ;restore DS
  112.     dec  _error_code    ;0 = no error
  113.     jmp  short N1        ;jump ahead
  114. M1:    pop  ds            ;terminate with error
  115. N1:    pop  si            ;
  116.     pop  di            ;
  117.     pop  bp            ;
  118.     cmp  _memory_model,0    ;quit
  119.     jle  quit        ;
  120.     db   0CBh        ;RET far
  121. quit:    ret            ;RET near
  122. _replace ENDP
  123. _TEXT    ENDS
  124.     END
  125.