home *** CD-ROM | disk | FTP | other *** search
- ;void replace(strg,sub_strg,position,chars);
- ; char *strg,*sub_strg;
- ; unsigned short position,chars;
-
- EXTRN _memory_model:byte
- EXTRN _error_code:byte
-
- _TEXT SEGMENT BYTE PUBLIC 'CODE'
- ASSUME CS:_TEXT
- PUBLIC _replace
- _replace proc near
- push bp ;
- mov bp,sp ;set up stack frame
- push di ;
- push si ;
- push ds ;
- cmp _memory_model,0 ;near or far?
- jle begin ;jump if near
- inc bp ;else add 2 to BP
- inc bp ;
- begin: mov _error_code,1 ;1 = error has occured
- cmp _memory_model,2 ;data near or far?
- jb A1 ;jump if near
- les di,dword ptr[bp+4] ;ES:DI pts to strg
- lds si,dword ptr[bp+8] ;DS:SI pts to substrg
- add bp,4 ;add 4 to BP since 2 dword ptrs
- jmp short B1 ;
- A1: mov di,[bp+4] ;NEAR case
- mov si,[bp+6] ;
- mov ax,ds ;ES = DS
- mov es,ax ;
- B1: cmp word ptr[bp+10],0 ;test for zero-width field
- je C1 ;quit if zero
- cmp byte ptr es:[di],0 ;strg null?
- jne D1 ;
- cmp byte ptr [si],0 ;substrg null?
- jne D1 ;
- C1: jmp M1 ;
- D1: cmp byte ptr[si],0 ;substrg null?
- je C1 ;
- push di ;find strg length
- sub dx,dx ;
- E1: cmp byte ptr es:[di],0 ;null?
- je F1 ;
- inc di ;
- inc dx ;tally substrg length in DX
- jmp short E1 ;
- F1: pop di ;ptr back to start of string
- push si ;find substrg length
- sub bx,bx ;
- G1: cmp byte ptr[si],0 ;null?
- je H1 ;
- inc si ;
- inc bx ;tally substrg length in BX
- jmp short G1 ;
- H1: pop si ;ptr back to start of string
- mov cx,[bp+8] ;fetch position
- cmp cx,dx ;position in string?
- jae M1 ;quit if not
- mov ax,dx ;string length to ax
- sub ax,cx ;distance from startpt to end of string
- cmp ax,[bp+10] ;compare to number chars to replace
- jb M1 ;quit if out of range
- ;--find out if insertion is larger or smaller than replacement
- cmp [bp+10],bx ;compare substring length to Chars
- jne I1 ;jump ahead if not equal
- add di,cx ;add start point to string ptr
- jmp short L1 ;go write the chars
- I1: push di ;prepare to shift part of string
- push si ;point DS:SI to string
- push ds ;
- mov ax,es ;
- mov ds,ax ;
- mov si,di ;
- cmp bx,[bp+10] ;comp substrg to number chars
- jb J1 ;jump if substring shorter than chars replaced
- ;---insertion is longer than replaced chars:
- add si,dx ;point si to end of string
- mov di,si ;copy to di
- add di,bx ;add substring length
- sub di,[bp+10] ;minus field width
- sub dx,[bp+10] ;plus field width
- sub dx,[bp+8] ;minus start point
- inc dx ;plus one for null terminator
- mov cx,dx ;number chars to transfer
- std ;direction backwards
- rep movsb ;transfer the characters
- cld ;reset flag
- jmp short K1 ;go transfer the substring
- ;----insertion is shorter than replaced chars:
- J1: add di,[bp+8] ;add start point to di
- add di,bx ;plus substring length
- mov si,di ;copy to si
- add si,[bp+10] ;add field length to si
- sub si,bx ;minus substrg length
- mov cx,dx ;string length
- add cx,bx ;
- sub cx,[bp+8] ;minus start point
- sub cx,[bp+10] ;minus field width
- inc cx ;plus one for null terminator
- cld ;direction forward
- rep movsb ;mov the characters
- K1: pop ds ;
- pop si ;
- pop di ;
- add di,[bp+8] ;point di back to field start
- ;--write the substring:
- L1: cld ;direction forward
- mov cx,bx ;number chars to write
- rep movsb ;write the chars
- pop ds ;restore DS
- dec _error_code ;0 = no error
- jmp short N1 ;jump ahead
- M1: pop ds ;terminate with error
- N1: pop si ;
- pop di ;
- pop bp ;
- cmp _memory_model,0 ;quit
- jle quit ;
- db 0CBh ;RET far
- quit: ret ;RET near
- _replace ENDP
- _TEXT ENDS
- END