[Original] Demonstration to determine if a string is a compiler of a string of a string of another string

xiaoxiao2021-03-06  42

; @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@

(Function: Demonstration to determine if a string is a function of a substring of another string

; Author: Huang Zhibin in Hechi

Daily period

; ------------------------------------------------- ---

2002.06.25 Complete basic functions

2002.06.26 Modify Issubstr to reduce one byte

2002.06.29 Discover the algorithm error used in the 26th to modify, modify the calculation string 1

; Algorithm in the skewer 2, the file length is reduced to 320 bytes

Codeseg segment

; ================================================== ===

ORG 100H

Assume ES: Codeseg, SS: CODESEG, CS: CODESEG, DS: CODESEG

; -----------------------------

Main Proc

MOV DX, Offset StrPt

MOV AH, 9

Int 21h

MOV DX, Offset str1maxlen

Mov Ah, 0ah

Int 21h

Call CRLINE

MOV CSTRNAME, '2'

MOV DX, Offset StrPt

MOV AH, 9

Int 21h

Mov DX, Offset Str2maxlen

Mov Ah, 0ah

Int 21h

Call CRLINE

XOR CH, CH

MOV CL, STR1LEN

XOR BH, BH

MOV BL, STR2LEN

Mov Si, Offset String1

Mov Di, Offset String2

Call Issubstr

MOV DX, Offset Strisnull

CMP BP, -3

Je @quit

MOV DX, Offset Strtoolen

CMP BP, -2

Je @quit

MOV DX, Offset Strno

CMP BP, -1

Je @quit

Mov Di, Offset CPSITION

MOV AX, BP

Call Dec2ASCII

MOV DX, Offset Stryes

@Quit:

MOV AH, 9

Int 21h

MOV AX, 4C00H

Int 21h

Main endp

; ----------------------

Conststrlen EQU 15

STRPROMPT DB "Enter"

CSTRNAME DB '1'

DB "string: $"

STR1MAXLEN DB Conststrlenstr1len DB 0

String1 DB Conststrlen Dup (?)

STR2MAXLEN DB Conststrlen

Str2len DB 0

String2 DB Conststrlen Dup (?)

STRTOOLEN DB "String 1 is greater than the string 2! $"

Strisnull DB "String 1 length is 0 $"

Stryes DB "String 1 appears in string 2 left"

CPSITION DB "Location $"

Strno DB 0DH, 0AH, "String 1 is not string of string 2 $"

; ============================================

; Function: Convert data in AX into a corresponding decimal number string

;enter:

; AX = data to be converted

; Di = buffer first address of the string of the converted string

; Output: None

; --------------------------------------------

Dec2ascii proc

MOV CX, 2

MOV DL, 100

@Loopdiv:

Div DL

Add Al, '0'

MOV [DI], Al

INC DI

Mov Al, AH

XOR AH, AH

SHR DL, 1

Loop @loopdiv

Add Al, '0'

MOV [DI], Al

RET

DEC2ASCII ENDP

; ===================================

; Function: Judging whether the string 1 is a string of string 2

;Entrance:

; Cx = length of string 1

; Bx = length of string 2

; Si = start address of string 1

; Di = start address of string 2

;Exit:

BP> = 0 string 1 is a string of string 2, and BP = string 1 in series 2 in series (starting from 0)

BP = -1 string 1 is not a string of string 2

; BP = -2 string 1 length is greater than string 2

; BP = -3 string 1 length is 0

; ----------------------------------

Issubstr Proc

PUSH DI; Save String 2 Addition Location

MOV BP, -3

JCXZ @ERRRRESULT

MOV BP, -2

CMP CX, BX

Jg @errresult

CLD

@Loopcmp:

PUSH CX

Push Si

Push di

REPE CMPSB

Je @YESRESULT

POP DI; Restore String 2 Preferred in this comparison

INC DI; add 1 to 1, point to the next character so that the next time

Dec BX; string 2 length minus 1

POP Si; Restore String 1 Address

POP CX; restore string 1 length

CMP BX, CX

JGE @loopcmp

MOV BP, -1 @ errRRRRESULT:

POP CX

RET

@YESRESRESRESRESULT:

The following is a method of initially calculating the position of string 1 in series 2

Pop AX; these 2 are Di, Si that pops up previously pressed

; POP AX

; POP CX; send the string 1 length to CX

; POP BP; send the original string 2 to the first spot

; SUB DI, CX; Calculate the first address of this comparison time

SUB DI, BP; String 1 in series 2 = Preferred in this comparison - the first address of the original string 2

; MOV BP, DI

RET

POP BP; popped up previously pressed DI, that is, the first address of the string 2 at this time

POP AX; pops up Si, useless

POP CX; send CX 1 length

POP DI; send the original string 2 to send DI

SUB BP, DI; Calculation String 1 in the string 2

RET

Issubstr ENDP

; ======================

CRLINE PROC

MOV DX, Offset strcrline

MOV AH, 9

Int 21h

RET

CRLINE ENDP

; ----------------------

Strcrline DB 0DH, 0AH, '$'

Codeseg Ends

End main

Error algorithm:

Since there is no address of the string 2, it is "123", the string 2 is "12345" when the string 1 is "12345".

The result of the operation is "String 1 is not a string of string 2"

Issubstr Proc

PUSH DI; Save String 2 length After calculation string 1 appearance position

MOV BP, -3

JCXZ @ERRRRESULT

MOV BP, -2

CMP CX, BX

Jg @errresult

CLD

@Loopcmp:

PUSH CX

Push Si

REPE CMPSB

JE @YesResult; is a substring to jump

POP Si

The following three instructions are the latest residual length of the string 2

The number of bytes compared to the system = string 1 length - CX

; bx = string 2 original remaining length

; String 2 latest remaining length = string 2 original remaining length - the number of bytes that have been compared

; = BX - (String 1 length - CX)

; = BX CX - String 1 length

Add BX, CX; Performance BX = String 2 Length

POP CX; Restore String 1 Address

SUB BX, CX; CX = String 1 length, after execution BX = string 2 latest remainder

CMP BX, CX

JGE @loopcmp

MOV BP, -1

@ERRRESULT:

POP CX

RET

@YESRESRESRESRESULT:

POP AX; this is Si that pops up the previously pressed

POP CX; send CX 1 length

POP BP; Send BP 2 length

SUB DI, CX; The following three instructions calculation string 1 in series 2 is stored in BP

SUB DI, BP

MOV BP, DI

RET

Issubstr ENDP

; ==================================================== Codeseg Ends

End main

转载请注明原文地址:https://www.9cbs.com/read-75398.html

New Post(0)