Logo 
Search:

Assembly Language Forum

Ask Question   UnAnswered
Home » Forum » Assembly Language       RSS Feeds

please help - why this is not working ? (sub str in str)

  Asked By: Sahar    Date: Mar 28    Category: Assembly Language    Views: 1642
  

DATA SEGMENT
STR1 db "ENTER YOUR STRING HERE = $"
STR2 db 0ah,0dh,"ENTER THE SHORT STRING = $"
FOUND db 0ah,0dh,"FOUND STRING$"
NOTFOUND db 0ah,0dh,"NOT FOUND ANY STRING$"
M DB 15 DUP("$")
N DB 3 DUP("$")
MONE EQU 15
COUNT DB 1
DATA ENDS
sseg segment stack
dw 100h dup(?)
sseg ends

CODE SEGMENT
ASSUME DS:DATA,CS:CODE
START:

MOV AX,DATA
MOV DS,AX

LEA SI,M
LEA DI,N
MOV COUNT,0h

;----GET STRING
MOV AH,09H
LEA DX,STR1
INT 21H

MOV AH,0AH
MOV DX,SI
INT 21H

;----GET SHORT STRING
MOV AH,09
LEA DX,STR2
INT 21H

MOV AH,0AH
MOV DX,DI
INT 21H

mov ah,00
int 16h

;START TO CMP STRINGS
MOV CL,MONE
l1: MOV AX,M[SI]
MOV BX,N[DI]
CMP AX,BX
JE L2
JMP L3

;----EQUAL------------
L2: INC COUNT
CMP COUNT,3
JZ PRINTSTR
INC SI
INC DI
DEC CL
CMP CL,0
JNZ L1
JMP L4
;-------NOT EQUAL------
L3: MOV COUNT,0
INC AX
DEC CL,1
CMP CL,0
JNZ L1
JMP L4
;------PRINT THE STRING-----

PRINTSTR:MOV AH,09H
LEA DX,FOUND
INT 21H

MOV COUNT,0
INC SI
MOV BX,COUNT
DEC CL
JNZ L1
JMP L4

mov ah,00
int 16h

L4: MOV AH,09H
LEA DX,NOTFOUND
INT 21H

SOF:
MOV AH,4CH
INT 21H
CODE ENDS
END START

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Abhishek Singh     Answered On: May 22

2 down vote accepted


For 8-bit characters it's broadly like this, there are many ways to implement it:

Set si to point to the first character of the string.

mov al,[si]

repnz scasb to find the first match of the first character.

Store the address somewhere.

Set di to point to the first character of the replacement string ('dog' in this case).

Set cx/ecx/rcx to string length.

repz cmpsb

Check that cx/ecx/rcx is zero and last characters match.

If yes, it's a match, so copy 'dog' to the address stored with rep movsb (set pointers si and di first). Do note that this approach only works if the replace string is no longer than the original string. If it's longer, you may need to reserve a new block of memory to avoid a buffer overflow. If it's not a match, backtrack si to the stored address, increment si by 1 (by 2 for 16-bit characters), and jump to 2. (mov al,[si]). You need to also check here when you have reached the end of the string.

Ready. Or, if you want to replace all, as in sed s/cat/dog/g, loop from 1, set pointer (si) first (depending on how you want your regex engine to work).

For UTF-8 (16-bit characters) replace the following: scasb -> scasw, cmpsb -> cmpsw, movsb -> movsw, al -> ax.

For 32-bit code, replace all references to si with esi and all references to di with edi.

For 64-bit code, replace all references to si with rsi and all references to di with rdi.

 
Didn't find what you were looking for? Find more on please help - why this is not working ? (sub str in str) Or get search suggestion and latest updates.




Tagged: