📄 patterns.asm
字号:
MatchStr endp
; MatchiStr- Matches a string of characters against the source string.
; Returns success if all the characters in the string match
; the next set of characters in the source string, ignoring
; case in the source string (by converting to upper case).
;
; inputs:
; es:di- Source string
; ds:si- String to match, alphas must be upper case.
; cx- Maximum match position
;
; outputs:
; ax- failure position (or char position after match if
; success).
; carry- 0 if failure, 1 if success.
public MatchiStr
MatchiStr proc far
pushf
push di
cmp di, cx ;See if beyond allowable point already.
jae iFailure
cld
MatchiLp: lodsb
cmp al, 0
je MiSSuccess
mov ah, es:[di]
cmp ah, 'a'
jb NoLC
cmp ah, 'z'
ja NoLC
and ah, 5fh
NoLC: inc di ;Skip this char.
cmp al, ah
jne iFailure
cmp di, cx
jbe MatchiLp
inc di ;'cause we're about to dec it.
iFailure: dec di ;Point back at source of failure.
mov ax, di ;Return failure position in AX.
pop di
popf
clc ;Return failure.
ret
MiSSuccess: mov ax, di ;Return next position in AX.
pop di
popf
stc ;Return success.
ret
MatchiStr endp
; MatchToStr- Matches all characters in a string up to, and including, the
; specified parameter string.
;
; inputs:
; es:di- Source string
; ds:si- String to match
; cx- Maximum match position
;
; outputs:
; ax- Points at first character beyond the end of the matched
; string if success, contains the initial DI value if
; failure occurs.
; carry- 0 if failure, 1 if success.
public MatchToStr
MatchToStr proc far
pushf
push di
push si
cld
cmp di, cx ;See if beyond allowable point already.
jae MTSFailure2
ScanLoop: push si
lodsb ;Get first char of string
cmp al, 0 ;If empty string, always match
je MTSsuccess
push cx
sub cx, di
repne scasb ;Find it.
pop cx
push di ;Save restart point.
jne MTSFailure
CmpLoop: cmp di, cx
jae MTSFailure3
lodsb
cmp al, 0
je MTSsuccess2
scasb
je CmpLoop
pop di
pop si
jmp ScanLoop
MTSFailure: dec di ;Point back at source of failure.
MTSFailure3: add sp, 4 ;Remove si, di from stack.
MTSFailure2: pop si
pop di
mov ax, di ;Return failure position in AX.
popf
clc ;Return failure.
ret
MTSSuccess2: add sp, 2 ;Remove DI value from stack.
MTSSuccess: add sp, 2 ;Remove SI value from stack.
mov ax, di ;Return next position in AX.
pop si
pop di
popf
stc ;Return success.
ret
MatchToStr endp
; MatchChar- Matches a single character against the source string.
; Returns success if the next char matches, failure otherwise.
;
; inputs:
; es:di- Source string
; si- character to match (in L.O. byte)
; cx- Maximum match position
;
; outputs:
; ax- failure position (or char position after match if
; success).
; carry- 0 if failure, 1 if success.
public MatchChar
MatchChar proc far
push di
cmp di, cx ;See if beyond allowable point already.
jae MCFailure
mov ax, si ;To get at L.O. byte
cmp al, es:[di]
je MCSuccess
MCFailure: clc
pop di
ret
MCSuccess: cmp di, cx ;At EOS?
jae NoIncMCS
inc di
NoIncMCS: mov ax, di
pop di
stc ;Return success.
ret
MatchChar endp
; MatchToChar- Matches all characters in a string up to, and including, the
; specified parameter character.
;
; inputs:
; es:di- Source string
; si- Character to match
; cx- Maximum match position
;
; outputs:
; ax- Points at first character beyond the end of the matched
; char if success, contains the initial DI value if
; failure occurs.
; carry- 0 if failure, 1 if success.
public MatchToChar
MatchToChar proc far
pushf
push cx
push di
push si
cld
cmp di, cx ;See if beyond allowable point already.
jae MTCFailure2
mov ax, si ;Get char to match in AL
sub cx, di
repne scasb ;Find it.
je MTCsuccess
MTCFailure2: pop si
pop di
mov ax, di ;Return failure position in AX.
pop cx
popf
clc ;Return failure.
ret
MTCSuccess: mov ax, di ;Return next position in AX.
pop si
pop di
pop cx
popf
stc ;Return success.
ret
MatchToChar endp
; MatchChars- Matches a single character against the source string.
; This guy matches zero or more characters in the string
; (which must all be the same). Always returns success.
;
; inputs:
; es:di- Source string
; si- character to match (in L.O. byte)
; cx- Maximum match position
;
; outputs:
; ax- Current position plus one (unless EOS).
; carry- 1
public MatchChars
MatchChars proc far
push di
mov ax, si ;To get at L.O. byte
dec di
MCsLoop: inc di
cmp di, cx ;See if beyond allowable point already.
jae MCsDone
cmp al, es:[di]
je MCsLoop
MCsDone: mov ax, di
pop di
stc ;Return success.
ret
MatchChars endp
; Anycset- Matches a single character from the string using the specified
; character set.
;
; inputs:
; es:di- pointer to source string
; ds:si- pointer to cset
; cx- pointer just beyond the end of the string to compare
;
; Outputs- ax- di+1 if match, di if no match (failure position).
; carry- 1 if success, 0 if failure.
public Anycset
Anycset proc far
push bx
cmp cx, di
jbe ACFailure
mov bh, 0
mov bl, es:[di] ;Get next char to compare
cmp bl, 0 ;At end of string?
je ACFailure
mov al, [si] ;Get cset mask byte
and al, 8[bx][si] ;See if member of cset.
jz ACFailure
lea ax, 1[di] ;Return success position in AX.
pop bx
stc ;Return success in carry flag.
ret
ACFailure: mov ax, di ;Return failure position in AX.
pop bx
clc ;Return failure in carry flag.
ret
Anycset endp
; NotAnycset- Matches a single character from the string which is not in the
; specified character set.
;
; inputs:
; es:di- pointer to source string
; ds:si- pointer to cset
;
; Outputs- ax- di+1 if no match, di if match (failure position).
; carry- 1 if success, 0 if failure.
public NotAnycset
NotAnycset proc far
push bx
cmp cx, di
jbe NACFailure
mov bh, 0
mov bl, es:[di] ;Get next char to compare
cmp bl, 0 ;At end of string?
je NACFailure
mov al, [si] ;Get cset mask byte
and al, 8[bx][si] ;See if member of cset.
jnz NACFailure
lea ax, 1[di] ;Return success position in AX.
pop bx
stc ;Return success in carry flag.
ret
NACFailure: mov ax, di ;Return failure position in AX.
pop bx
clc ;Return failure in carry flag.
ret
NotAnycset endp
; EOS- Matches the end of the string (i.e., the terminating zero
; byte). Fails if any other value is present.
;
; inputs:
; es:di- pointer to source string
;
; Outputs- ax- di (match or failure position).
; carry- 1 if success, 0 if failure.
;
; Note that this code does not bump AX or DI to point beyond the matched
; character (since we don't want to go beyond the end of the string).
public EOS
EOS proc far
mov ax, di
cmp byte ptr es:[di], 0
je SetCarry
clc
ret
SetCarry: stc
ret
EOS endp
; ARBNUM- Matches an arbitrary number (zero or more) occurrences of
; the specified pattern.
;
; inputs:
; es:di- Source string
; ds:si- Pattern to match an arbitrary number of times.
; cx- Maximum match position
;
; outputs:
; ax- Points at first character beyond the end of the matched
; patterns if success.
;
; carry- 1 (this function always succeeds).
public ARBNUM
ARBNUM proc far
push dx
push di
push si
ARBLoop: cmp di, cx ;See if beyond allowable point already.
jae ARBSuccess
mov dx, ds
match2
mov di, ax ;Move to next position.
jc ARBLoop ;If we matched, try again.
ARBSuccess: pop si
pop di
pop dx
stc ;Always return success.
ret
ARBNUM endp
; ARB- Matches an arbitrary number of characters and always returns
; success.
;
; inputs:
; es:di- pointer to source string
;
; Outputs- ax- cx (since this matches any number of chars, we'll
; match them all).
; carry- 1.
;
public ARB
ARB proc far
mov ax, cx
stc
ret
ARB endp
; Skip- Skips over "n" characters in the string. Returns success if
; there were at least "n" characters in the string.
;
; inputs:
; es:di- pointer to source string
; si- number of characters to skip.
;
; Outputs- ax- points at first byte beyond skipped chars.
; carry- 1 if success, 0 if not enough chars in string.
;
public Skip
Skip proc far
or si, si ;Immediate success if skip nothing.
jz SkipSucceeds
mov ax, di ;See where we would be if we skipped
add ax, si ; SI chars.
cmp ax, cx ;See if too many chars
jbe SkipSucceeds ; and return success if not.
clc
ret
SkipSucceeds: stc ;Note: AX contains position after
ret ; the skip chars.
Skip endp
; POS- Returns success if we are at position SI in the string.
; Returns failure otherwise.
;
; inputs:
; es:di- pointer to source string
; si- offset into string we need to match.
;
; Outputs- carry- 1 if success, 0 if failure.
public POS
POS proc far
assume ds:StdGrp
push ds
mov ax, StdGrp
mov ds, ax
mov ax, di
sub ax, word ptr StdGrp:StringAddress
cmp ax, si
jne BadPosn
pop ds
mov ax, di
stc
ret
BadPosn: pop ds
mov ax, di
clc
ret
POS endp
assume ds:nothing
; RPOS- Returns success if we are at position SI from the end of the
; string. Returns failure otherwise.
;
; inputs:
; es:di- pointer to source string
; si- offset from end of string to match (note: a value of
; zero denotes the end of the string).
;
; Outputs- carry- 1 if success, 0 if failure.
public RPOS
RPOS proc far
assume ds:stdgrp
push ds
mov ax, StdGrp
mov ds, ax
mov ax, StdGrp:LastStringAdrs
sub ax, di
cmp ax, si
jne BadRposn
pop ds
mov ax, di
stc
ret
BadRposn: pop ds
mov ax, di
clc
ret
RPOS endp
assume ds:nothing
; GOTOpos- Moves the "cursor" to position SI in the string. Succeeds
; if this doesn't move the cursor beyond the end of the string.
; fails otherwise. Note that this command will not let you
; back up in a string.
;
; inputs:
; es:di- pointer to source string
; si- position in string to transfer to (zero denotes the
; first character in the string).
;
; Outputs- ax- new string position.
; carry- 1 if success, 0 if failure.
public GOTOpos
GOTOpos proc far
assume ds:stdgrp
push ds
mov ax, StdGrp
mov ds, ax
mov ax, word ptr StdGrp:StringAddress
add ax, si
cmp ax, StdGrp:LastStringAdrs
ja BadGOTO
cmp ax, word ptr StdGrp:StringAddress
jb BadGOTO
pop ds
stc
ret
BadGOTO: pop ds
clc
ret
GOTOpos endp
assume ds:nothing
; RGOTOpos- Moves the "cursor" to position SI from the end of the string.
; Succeeds if this doesn't move the cursor beyond the end of
; the string, fails otherwise. Note that this command will not
; let you back up in a string.
;
; inputs:
; es:di- pointer to source string
; si- position in string to transfer to (zero denotes the
; position just beyond the end of the string, i.e.,
; the zero terminating byte).
;
; Outputs- ax- New position in string.
; carry- 1 if success, 0 if failure.
public RGOTOpos
RGOTOpos proc far
assume ds:stdgrp
push ds
mov ax, StdGrp
mov ds, ax
mov ax, word ptr StdGrp:LastStringAdrs
sub ax, si
cmp ax, StdGrp:LastStringAdrs
ja BadRGOTO
cmp ax, word ptr StdGrp:StringAddress
jb BadRGOTO
pop ds
stc
ret
BadRGOTO: pop ds
clc
ret
RGOTOpos endp
assume ds:nothing
stdlib ends
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -