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

已知一任意长度字符串str(该字符串是一个变量,需要在运行时手动输入),以00h结束,长度小于200h,编写汇编程序实现在该字符串中搜索匹配子串substr(以00h结束,长度小…

  1. User

    已知一任意长度字符串str(该字符串是一个变量,需要在运行时手动输入),以00h结束,长度小于200h,编写汇编程序实现在该字符串中搜索匹配子串substr(以00h结束,长度小于80),若找到,则将found单元置为ffh,并将匹配位置(匹配位置以字符串str首地址为0参考点)存放在pos单元中;否则,将found单元置为00H。 要求:1. 编写完整程序,并上机通过调试能运行。(禁止使用串操作指令)

  2. Assistant

    .data str db 200h dup(?) substr db 80 dup(?) found db ? pos dw ?

    .code start: ; 读入字符串 str mov si, offset str read_str: mov ah, 01h int 21h mov [si], al cmp al, 00h je search inc si cmp si, offset str + 200h jb read_str jmp end_prog

    ; 读入子串 substr
    search:
    mov si, offset substr
    read_substr:
    mov ah, 01h
    int 21h
    mov [si], al
    cmp al, 00h
    je start_search
    inc si
    cmp si, offset substr + 80
    jb read_substr
    jmp end_prog
    
    ; 开始搜索
    start_search:
    mov di, offset str
    mov cx, 200h
    mov dx, 80h
    sub cx, dx
    next_char:
    cmp byte ptr [di], 00h
    je not_found
    mov si, offset substr
    cmp byte ptr [di], [si]
    jne next_char
    mov bx, di
    mov al, 1
    cmp cx, dx
    jb not_found
    mov cx, dx
    compare:
    cmp byte ptr [di], 00h
    je found
    cmp byte ptr [di], [si]
    jne next_char
    inc si
    inc di
    loop compare
    
    ; 找到了子串
    found:
    mov al, 0ffh
    mov [found], al
    sub di, bx
    mov [pos], di
    jmp end_prog
    
    ; 没有找到子串
    not_found:
    mov al, 00h
    mov [found], al
    
    ; 结束程序
    end_prog:
    mov ah, 4ch
    int 21h
    

    end start

  3. User

    已知一任意长度字符串str,以00h结束,长度小于200h,编写汇编程序实现在该字符串中搜索匹配子串substr(以00h结束,长度小于80),若找到,则将found单元置为ffh,并将匹配位置(匹配位置以字符串str首地址为0参考点)存放在pos单元中;否则,将found单元置为00H。 要求:1. 编写完整程序,并上机通过调试能运行。(禁止使用串操作指令),任意长度字符串str和匹配子串substr都是需要自己输入的变量

  4. Assistant

    .data str db 200h dup(?) substr db 80 dup(?) found db ? pos dw ?

    .code start: ; 读入字符串 str mov si, offset str read_str: mov ah, 01h int 21h mov [si], al cmp al, 00h je search inc si cmp si, offset str + 200h jb read_str jmp end_prog

    ; 读入子串 substr
    search:
    mov si, offset substr
    read_substr:
    mov ah, 01h
    int 21h
    mov [si], al
    cmp al, 00h
    je start_search
    inc si
    cmp si, offset substr + 80
    jb read_substr
    jmp end_prog
    
    ; 开始搜索
    start_search:
    mov di, offset str
    mov cx, 200h
    mov dx, 80h
    sub cx, dx
    next_char:
    cmp byte ptr [di], 00h
    je not_found
    mov si, offset substr
    cmp byte ptr [di], [si]
    jne next_char
    mov bx, di
    mov al, 1
    cmp cx, dx
    jb not_found
    mov cx, dx
    compare:
    cmp byte ptr [di], 00h
    je found
    cmp byte ptr [di], [si]
    jne next_char
    inc si
    inc di
    loop compare
    
    ; 找到了子串
    found:
    mov al, 0ffh
    mov [found], al
    sub di, bx
    mov [pos], di
    jmp end_prog
    
    ; 没有找到子串
    not_found:
    mov al, 00h
    mov [found], al
    
    ; 结束程序
    end_prog:
    mov ah, 4ch
    int 21h
    

    end start

  5. User

    (63) duplicate declaration of: FOUND

  6. Assistant

    这个错误说明在程序中重复定义了一个变量 FOUND。在汇编程序中,每个变量的名称必须是唯一的,不能重复定义。

    解决方法是在程序中找到所有定义 FOUND 变量的地方,将其改成其他名称,保证每个变量的名称都不重复即可。

  7. User

    上面的代码中,出现了(63) duplicate declaration of: FOUND的问题

  8. Assistant

    这个问题是因为在示例代码中,变量 FOUND 被定义了多次,导致汇编器无法确定到底是哪个定义是正确的。

    解决方法有两种:

    1. 删除其中一个定义:找到并删除多余的 FOUND 定义即可。

    2. 另取一个变量名:将其中一个 FOUND 改名为其他的变量名,保证每个变量名都是唯一的。

    下面是修改后的代码示例:

    .data
    str db 200h dup(?)
    substr db 80 dup(?)
    found db ?
    pos dw ?
    
    .code
    start:
        ; 读入字符串 str
        mov si, offset str
        read_str:
        mov ah, 01h
        int 21h
        mov [si], al
        cmp al, 00h
        je search
        inc si
        cmp si, offset str + 200h
        jb read_str
        jmp end_prog
    
        ; 读入子串 substr
        search:
        mov si, offset substr
        read_substr:
        mov ah, 01h
        int 21h
        mov [si], al
        cmp al, 00h
        je start_search
        inc si
        cmp si, offset substr + 80
        jb read_substr
        jmp end_prog
    
        ; 开始搜索
        start_search:
        mov di, offset str
        mov cx, 200h
        mov dx, 80h
        sub cx, dx
        next_char:
        cmp byte ptr [di], 00h
        je not_found
        mov si, offset substr
        cmp byte ptr [di], [si]
        jne next_char
        mov bx, di
        mov al, 1
        cmp cx, dx
        jb not_found
        mov cx, dx
        compare:
        cmp byte ptr [di], 00h
        je found_substring
        cmp byte ptr [di], [si]
        jne next_char
        inc si
        inc di
        loop compare
    
        ; 找到了子串
        found_substring:
        mov al, 0ffh
        mov [found_sub], al
        sub di, bx
        mov [pos], di
        jmp end_prog
    
        ; 没有找到子串
        not_found:
        mov al, 00h
        mov [found_sub], al
    
        ; 结束程序
        end_prog:
        mov ah, 4ch
        int 21h
    end start
    
    .data
    found_sub db ?
    

    以上修改后的代码中,将第一个 FOUND 改名为 FOUND_SUB,并在第二个数据段中添加了定义 FOUND_SUB 的代码。

  9. User

    (46) wrong parameters: CMP byte ptr [di], [si]

  10. Assistant

    这个错误是由于在比较两个字节时使用了错误的语法。

    在示例代码中,使用的语法是 CMP byte ptr [di], [si],这个语法是错误的。正确的语法应该是 CMP byte ptr [di], [si] 或者 CMP [di], [si]。

    修改后的代码示例如下:

    ; 比较两个字节是否相等
    cmp byte ptr [di], [si]
    ; 或者
    cmp [di], [si]
    

    在汇编代码中,指令的参数必须是正确的语法,否则编译器会报告错误。