Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

已知一任意长度字符串str,以00h结束,长度小于200h,编写汇编程序实现在该字符串中搜索匹配子串substr(以00h结束,长度小于80),若找到,则将found单元置为ff…

  1. User

    已知一任意长度字符串str,以00h结束,长度小于200h,编写汇编程序实现在该字符串中搜索匹配子串substr(以00h结束,长度小于80),若找到,则将found单元置为ffh,并将匹配位置(匹配位置以字符串str首地址为0参考点)存放在pos单元中;否则,将found单元置为00H。 要求:

    1. 编写完整程序,代码有简要注释,并上机通过调试能运行 2.禁止使用串操作指令 3.写出算法思路说明 4.题目中的两种字符串是用户通过代码段的输入指令手动输入的。
  2. Assistant

    算法思路: 1.先初始化found单元为00h,pos单元为0,分别用bx、si指向str和substr的首地址 2.利用循环扫描str中的每个字符,直到遇到00h结束;每次扫描前将si重新指向substr的首地址 3.每次扫描时,先将bx保存在cx中,然后通过循环比较str与substr中的字符,若发现不匹配则跳出循环;若匹配,则si、bx分别加1并继续比较 4.循环结束后再次判断si是否指向substr的结束标志00h,若是,则说明找到匹配子串,则将found单元置为ffh,并将str和substr的匹配位置保存在pos单元中;否则,将bx恢复到cx中,继续扫描str的下一个字符

    完整程序:

    assume cs:code,ds:data
    data segment              
        str db 'abcdeabcdeabcd00h' ; 任意长度字符串str,以00h结束 
        substr db 'cde00h'        ; 匹配子串substr,以00h结束
        found db 0                ; 匹配标志,初始为00H
        pos dw 0                  ; 匹配位置,初始为0
    data ends
    
    code segment
    start:
        mov ax,data
        mov ds,ax
    
        mov si,offset substr      ; si指向substr的首地址
        mov bx,offset str         ; bx指向str的首地址
    
        mov cx,0                  ; cx作为保存bx的临时寄存器
    
        scan:
            cmp byte ptr[bx],'00' ; 判断是否到了str的结尾
            je finish             ; 如果是,则跳转到finish
            
            mov cx,bx             ; 保存bx的值到cx
            
            cmp byte ptr[si],'00' ; 判断是否到了substr的结尾
            je match              ; 如果是,则跳转到match
            
        loopback:
            cmp byte ptr[bx],byte ptr[si] ; 比较str和substr中的字符
            jne nomatch          ; 如果不匹配,则跳转到nomatch
            
            inc bx               ; 如果匹配,则bx和si都加1,继续比较
            inc si
            cmp byte ptr[si],'00' ; 判断是否到了substr的结尾
            je match              ; 如果是,则跳转到match
        
            inc cx               ; 如果没到substr的结尾,则继续比较
            cmp byte ptr[bx],byte ptr[si]
            je loopback
            
        nomatch:
            mov bx,cx            ; 如果不匹配,恢复bx的值到cx
            inc bx               ; bx加1,继续扫描下一个字符
            jmp scan             ; 跳回scan,继续扫描
            
        match:
            mov al,1             ; 如果匹配,将found单元置为ffh
            mov found,al
    
            mov ax,bx            ; 将匹配位置保存在pos单元中
            sub ax,offset str
            mov pos,ax
    
            inc bx               ; bx加1,继续扫描下一个字符
            jmp scan             ; 跳回scan,继续扫描
    
        finish:
            mov ah,4ch           ; 结束程序
            int 21h
    code ends
    end start
    
  3. User

    我的意思是,任意长度的字符串和匹配子串,是在代码运行后,要用户自己输入的自定义变量

  4. Assistant

    哦,那就需要使用INT 21H中断来读取用户输入的字符串和匹配子串,修改程序中的data段和代码中的字符常量定义,以及采用INT 21H中断来读取字符串。下面是修改后的程序:

    assume cs:code,ds:data
    data segment
        str db 200 dup(0)        ; 存放任意长度字符串str,以00h结束
        substr db 80 dup(0)      ; 存放匹配子串substr,以00h结束
        found db 0                ; 匹配标志,初始为00H
        pos dw 0                  ; 匹配位置,初始为0
        msg1 db 'Enter the string:',0dh,0ah,'$' ; 提示用户输入字符串的消息
        msg2 db 'Enter the substring to match:',0dh,0ah,'$' ; 提示用户输入匹配子串的消息
    data ends
    
    code segment
    start:
        mov ax,data
        mov ds,ax
    
        ; 显示提示消息,读取用户输入的任意长度字符串
        mov dx,offset msg1
        mov ah,9
        int 21h
        mov ah,0ah
        lea dx,str
        int 21h
    
        ; 显示提示消息,读取用户输入的匹配子串
        mov dx,offset msg2
        mov ah,9
        int 21h
        mov ah,0ah
        lea dx,substr
        int 21h
    
        mov si,offset substr      ; si指向substr的首地址
        mov bx,offset str         ; bx指向str的首地址
    
        mov cx,0                  ; cx作为保存bx的临时寄存器
    
        scan:
            cmp byte ptr[bx],'00' ; 判断是否到了str的结尾
            je finish             ; 如果是,则跳转到finish
            
            mov cx,bx             ; 保存bx的值到cx
            
            cmp byte ptr[si],'00' ; 判断是否到了substr的结尾
            je match              ; 如果是,则跳转到match
            
        loopback:
            cmp byte ptr[bx],byte ptr[si] ; 比较str和substr中的字符
            jne nomatch          ; 如果不匹配,则跳转到nomatch
            
            inc bx               ; 如果匹配,则bx和si都加1,继续比较
            inc si
            cmp byte ptr[si],'00' ; 判断是否到了substr的结尾
            je match              ; 如果是,则跳转到match
        
            inc cx               ; 如果没到substr的结尾,则继续比较
            cmp byte ptr[bx],byte ptr[si]
            je loopback
            
        nomatch:
            mov bx,cx            ; 如果不匹配,恢复bx的值到cx
            inc bx               ; bx加1,继续扫描下一个字符
            jmp scan             ; 跳回scan,继续扫描
            
        match:
            mov al,1             ; 如果匹配,将found单元置为ffh
            mov found,al
    
            mov ax,bx            ; 将匹配位置保存在pos单元中
            sub ax,offset str
            mov pos,ax
    
            inc bx               ; bx加1,继续扫描下一个字符
            jmp scan             ; 跳回scan,继续扫描
    
        finish:
            mov ah,4ch           ; 结束程序
            int 21h
    code ends
    end start