现在的位置: 首页 > 综合 > 正文

【原创】演示判断一个字符串是否为另一字符串的子串的函数的汇编源程序

2013年08月27日 ⁄ 综合 ⁄ 共 3984字 ⁄ 字号 评论关闭
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
;功能: 演示判断一个字符串是否为另一字符串的子串的函数
;作者: 黄志斌 于 广西河池
;日      期    内    容
;----------------------------------------------------
;2002.06.25    完成基本功能
;2002.06.26    修改IsSubStr, 减少一个字节
;2002.06.29    发现26日修改所用算法错误, 修改计算串1
;              在串2位置的算法, 文件长度减为320字节

codeseg segment
;====================================================
org 100h
assume es: codeseg, ss: codeseg, cs: codeseg, ds: codeseg
;-----------------------------
main  proc
       mov  dx, offset strPrompt
       mov  ah, 9
       int  21h
       mov  dx, offset Str1MaxLen
       mov  ah, 0ah
       int  21h

       call CRLine

       mov  cStrName, '2'
       mov  dx, offset strPrompt
       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 cPosition
       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 "输入第"
cStrName   db '1'
            db "个字符串:$"
Str1MaxLen db constStrLen
Str1Len    db 0
String1    db constStrLen dup(?)
Str2MaxLen db constStrLen
Str2Len    db 0
String2    db constStrLen dup(?)
strTooLen db "字符串1的长度大于字符串2!$"
strIsNULL db "字符串1的长度为0$"
strYes    db "串1出现在串2左起"
cPosition db "   位置$"
strNo     db 0dh, 0ah,  "串1不是串2的子串$"

;============================================
;功能: 将AX中的数据转换成对应的十进制数字符串
;输入:
; ax = 待转换的数据
; di = 存放转换出来的字符串的缓冲区首地址
;输出: 无
;--------------------------------------------
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

;===================================
;功能:判断串1是否为串2的子串
;入口:
;  cx = 串1的长度
;  bx = 串2的长度
;  si = 串1的起始地址
;  di = 串2的起始地址
;出口:
;  bp >= 0 串1是串2的子串, 且 bp=串1在串2中首次出现的位置(从0开始)
;  bp = -1 串1不是串2的子串
;  bp = -2 串1的长度大于串2
;  bp = -3 串1的长度为0
;----------------------------------
IsSubStr proc
       push di  ;保存串2首址供以后计算串1在串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   ;恢复串2本次比较的首址
       inc  di   ;使串2首址加1,指向下一个字符, 以便下次比较
       dec  bx   ;串2长度减1
       pop  si   ;恢复串1首址
       pop  cx   ;恢复串1长度
       cmp  bx, cx
       jge  @LoopCmp
       mov  bp, -1
@ErrResult:
        pop  cx
        ret
@YesResult:
       ;下面是最初计算串1在串2中的位置的方法
       ;pop  ax  ;这2条是弹出先前压入的di, si
       ;pop  ax
       ;pop  cx  ;将串1长度送cx
       ;pop  bp  ;将原始串2首址送bp
       ;sub  di, cx ;计算本次比较时串2的首址
       ;sub  di, bp ;串1在串2中的位置 = 本次比较中串2的首址 - 原始串2首址
       ;mov  bp, di
       ;ret
       pop  bp    ;弹出先前压入的di, 即本次比较时的串2的首址
       pop  ax    ;弹出si, 无用
       pop  cx    ;将串1长度送cx
       pop  di    ;将原始串2首址送di
       sub  bp, di;计算串1在串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

;错误算法:
;因为没有恢复串2的地址, 因此当串1为"123", 串2为"12345"时
;运行结果为"串1不是串2的子串"
IsSubStr proc
       push di  ;保存串2长度供以后计算串1在串2的出现位置
       mov  bp, -3
       jcxz @ErrResult
       mov  bp, -2
       cmp  cx, bx
       jg   @ErrResult
       cld
@LoopCmp:
       push cx
       push si
       repe cmpsb
       je   @YesResult  ;是子串则跳转
       pop  si

       ; 以下3条指令是计算串2最新剩余长度
       ; 系统比较过的字节数 = 串1长度 - cx
       ; bx = 串2原剩余长度
       ; 串2最新剩余长度 = 串2原剩余长度 - 系统已经比较过的字节数
       ;                 = bx - (串1长度 - cx )
       ;                 = bx + cx - 串1长度

       add bx, cx  ; 执行前 bx = 串2原长度
       pop  cx     ; 恢复串1首址
       sub  bx, cx ; cx = 串1长度, 执行后 bx = 串2最新剩余长度
       cmp  bx, cx
       jge  @LoopCmp
       mov  bp, -1
@ErrResult:
        pop  cx
        ret
@YesResult:
       pop  ax  ;这是弹出先前压入的si
       pop  cx  ;将串1长度送cx
       pop  bp  ;将串2长度送bp
       sub  di, cx ;以下3条指令计算串1在串2中的位置存于bp
       sub  di, bp
       mov  bp, di
       ret
IsSubStr endp
;============================================
  codeseg ends
        end  main

抱歉!评论已关闭.