[Original] demonstrates the compilation source program for functions that determine whether a string is a substring of another string

Source: Internet
Author: User
; @@@@@@@@@@@@
Function: shows a function to determine whether a string is a substring of another string.
; Author: Huang zhibin at Hechi, Guangxi
; Capacity within the daily period
;----------------------------------------------------
; 2002.06.25 complete basic functions
; 2002.06.26 modify issubstr to reduce one byte
. An error occurred while modifying the algorithm used on the 26th. Modify the calculation string 1.
In string 2, the file length is reduced to 320 bytes.

Codeseg segment
; ========================================================== ================
Org 100 h
Assume ES: codeseg, SS: codeseg, CS: codeseg, DS: codeseg
;-----------------------------
Main proc
MoV dx, offset strprompt
MoV ah, 9
Int 21 h
MoV dx, offset str1maxlen
MoV ah, 0ah
Int 21 h

Call crline

MoV cstrname, '2'
MoV dx, offset strprompt
MoV ah, 9
Int 21 h
MoV dx, offset str2maxlen
MoV ah, 0ah
Int 21 h

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 cposition
MoV ax, BP
Call dec2ascii
MoV dx, offset stryes
@ Quit:
MoV ah, 9
Int 21 h
MoV ax, 4c00h
Int 21 h
Main endp
;----------------------
Conststrlen equ 15
Strprompt DB "input no"
Cstrname dB '1'
DB "strings: $"
Str1maxlen dB conststrlen
Str1len db 0
String1 dB conststrlen DUP (?)
Str2maxlen dB conststrlen
Str2len db 0
String2 dB conststrlen DUP (?)
Strtoolen DB "string 1 is longer than string 2! $"
Strisnull DB "the length of string 1 is 0 $"
Stryes DB "string 1 appears on the left of string 2"
Cposition DB "location $"
Strno dB 0dh, 0ah, "string 1 is not a substring of string 2 $"

; ========================================================== ====
Function: convert data in ax to a corresponding decimal number string
; Input:
; AX = data to be converted
; DI = the first buffer address for storing the converted string
; Output: None
;--------------------------------------------
Dec2ascii proc
MoV CX, 2
MoV DL and 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: determines whether string 1 is a substring of string 2.
; Entry:
; Cx = length of string 1
; BX = string 2 Length
; SI = start address of string 1
; DI = start address of string 2
Export:
; BP> = 0 string 1 is the substring of string 2, and BP = the position where string 1 first appears in string 2 (starting from 0)
; BP =-1 string 1 is not a substring of string 2
; BP =-2 string 1 is longer than string 2
; BP =-3 The length of string 1 is 0
;----------------------------------
Issubstr proc
Push di; Save the first address of string 2 for future computation of the position where string 1 appears in string 2
MoV bp,-3
Jcxz @ errresult
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 the first address of this comparison
INC di; adds 1 to the first address of string 2 to point to the next character for next comparison
Dec BX; string 2 length minus 1
Pop Si; restore the first address of string 1
Pop CX; restore string 1 Length
Cmp bx, CX
Jge @ loopcmp
MoV bp,-1
@ Errresult:
Pop CX
RET
@ Yesresult:
The following method calculates the position of string 1 in string 2.
Pop ax; these two items are the Di, Si
; Pop ax
; Pop CX; send string 1 to CX
; Pop bp; send the first address of the original string to BP
; Sub Di, CX; Calculate the first address of string 2 in this comparison
; Sub Di, BP; position of string 1 in string 2 = first address of string 2 in this comparison-first address of original string 2
; MoV bp, Di
; RET
Pop bp; pop the previously pushed Di, that is, the first address of string 2 in this comparison.
Pop ax; pop up Si, useless
Pop CX; send string 1 to CX
Pop di; send the first address of the original string 2 to Di
Sub bp, di; Calculate the position of string 1 in string 2
RET
Issubstr endp

====================================
Crline proc
MoV dx, offset strcrline
MoV ah, 9
Int 21 h
RET
Crline endp
;----------------------
Strcrline dB 0dh, 0ah, '$'
Codeseg ends
End main

; Error algorithm:
Because the address of string 2 is not restored, when string 1 is "123" and string 2 is "12345"
The running result is "string 1 is not a substring of string 2"
Issubstr proc
Push di; save string 2 length for later calculation of the position where string 1 appears in string 2
MoV bp,-3
Jcxz @ errresult
MoV bp,-2
Cmp cx, BX
JG @ errresult
ClD
@ Loopcmp:
Push CX
Push Si
Repe cmpsb
Je @ yesresult; jump if it is a substring
Pop Si

The following three commands are the latest remaining length of string 2.
; Number of bytes compared by the system = string 1-CX
; BX = string 2 Original Residual Length
; String 2 latest remaining length = string 2 Original remaining length-number of bytes that have been compared by the System
; = Bx-(string 1-cx)
; = Bx + cx-string 1 Length

Add Bx, CX; before execution BX = string 2 original length
Pop CX; restore the first address of string 1
Sub BX, CX; Cx = string 1 length, after execution BX = string 2 latest remaining length
Cmp bx, CX
Jge @ loopcmp
MoV bp,-1
@ Errresult:
Pop CX
RET
@ Yesresult:
Pop ax; this is the Si
Pop CX; send string 1 to CX
Pop bp; send string 2 to BP
Sub Di, CX; the following three commands calculate the position of string 1 in string 2 stored in BP
Sub Di, BP
MoV bp, Di
RET
Issubstr endp
; ========================================================== ====
Codeseg ends
End main

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.