在输入字符串中查找子字符串 [英] Finding the substring in an input string

查看:51
本文介绍了在输入字符串中查找子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个汇编程序,我需要在我输入的主字符串中找到子字符串.我的问题是它总是输出找到的词",即使我输入了两个完全不同的词.我不知道我的循环或条件的哪一部分是错误的.请帮我弄清楚.另外,请建议一些可用于检查子字符串的字符串指令,以便我可以缩短我的代码.我真的对 cmpsb 的工作方式感到困惑,我只是尝试使用它.顺便说一句,我不知道如何使用调试器,这就是为什么我无法调试我的代码,而且我只是汇编语言的新手.

下面是我代码的逻辑部分.

.dataprompt1 db输入字符串:$"prompt2 db 10,10, 13, "输入单词:$"prompt3 db 10,10, 13, "输出:$"found db "Word Found. $"notfound db找不到单词.$"无效 db 10,10, 13, "无效.$"InputString db 21,?,21 dup("$")InputWord db 21,?,21 dup("$")活动数据库?strlen dw ($-InputWord).代码开始:移动斧头,@datamov ds, ax移动,斧头;获取输入字符串移动啊,09hlea dx, prompt121 小时lea si, InputString移动啊,0啊mov dx, si21 小时;获取输入词移动啊,09hlea dx, prompt221 小时lea di, 输入词移动啊,0啊mov dx, di21 小时;检查子串的长度是否比主串短mov cl, [si+1]移动 ch, 0添加 si, cxmov bl, [di+1]mov bh, 0cmp bx, cxja invalid_length有效jb匹配有效的:CLD重复 cmpsbje found_displayjne notfound_display匹配:mov al, [si]mov啊,[di]cmp 啊,啊我检查迭代迭代:公司mov dx, strlen十二月cmp dx, 0je notfound_displayjmp匹配查看:mov cl, [di+1]移动 ch, 0mov ax, si添加斧头,1CLD重复 cmpsb再次珍妮jmp found_display再次:mov si, ax十二月lea di, 输入词jmp匹配无效长度:移动啊,09hlea dx, 无效21 小时

解决方案

strlen dw ($-InputWord)

这没有任何用处.它计算的长度对您没有任何帮助!

<块引用>

;检查子串的长度是否比主串短mov cl, [si+1]移动 ch, 0添加 si, cxmov bl, [di+1]mov bh, 0cmp bx, cx

这里(正如 Jester 告诉你的)add si, cx 指令是错误的.您需要 add si, 2SI 设置为字符串的开头.您还需要添加 add di, 2 以将 DI 设置为单词的开头.执行此操作,您程序的有效部分将正常运行.

<小时>

对于匹配部分:

考虑字符串有 7 个字符而您要查找的单词有 6 个字符的情况.您最多可以通过 2 种方式找到该词.

考虑字符串有 8 个字符而您要查找的单词有 6 个字符的情况.您最多可以通过 3 种方式找到该词.

考虑字符串有 9 个字符而您要查找的单词有 6 个字符的情况.您最多可以通过 4 种方式找到该词.

注意到规律了吗?可能找到的数量等于长度差加 1.

 mov bp, cx ;CX 为长度字符串(long)sub bp, bx ;BX 是长度字(短)公司 bp

这会将 BP 设置为匹配例程中的尝试次数.

 cldlea si, [InputString + 2]lea di, [InputWord + 2]匹配:mov al, [si] ;字符串中的下一个字符cmp al, [di] ;总是单词的第一个字符我检查继续:inc si ;DI 保留在单词的开头十二月jnz 匹配;更多尝试jmp notfound_display

check 部分将使用 repe cmpsb 来测试是否匹配,但如果没有找到匹配,您必须能够返回 repe cmpsbcontinue 标签上的 em>匹配 代码.您必须保留寄存器.

检查:推si推地mov cx, bx ;BX 是字的长度重复 cmpsb流行音乐流行音乐继续jmp found_display

I have this assembly program where I need to find the substring in the main string I input. My problem is that it always outputs the "word found" even if I typed two completely different words. I don't know which part of my loop or condition is wrong. Please help me figure it out. Also, please suggest some string instructions that could be used in checking for a substring so that I can shorten my code. I am really confused with how the cmpsb works, I only tried to use it. Btw, I don't know how to use a debugger that's why I can't debug my code and I am just a newbie in assembly language.

Below is the logic part of my code.

.data
     prompt1 db "Input String: $"
     prompt2 db 10,10, 13, "Input Word: $"
     prompt3 db 10,10, 13, "Output: $"
     found db "Word Found. $"
     notfound db "Word Not Found. $"
     invalid db 10,10, 13, "Invalid. $"
     InputString db 21,?,21 dup("$")  
     InputWord db 21,?,21 dup("$")
     actlen db ?
     strlen dw ($-InputWord)

.code
start:
      mov ax, @data
      mov ds, ax
      mov es, ax

     ;Getting input string
     mov ah,09h
     lea dx, prompt1
     int 21h

     lea si, InputString
     mov ah, 0Ah
     mov dx, si
     int 21h

     ;Getting input word
     mov ah,09h
     lea dx, prompt2
     int 21h

     lea di, InputWord
     mov ah, 0Ah
     mov dx, di
     int 21h

     ;To check if the length of substring is shorter than the main string
     mov cl, [si+1]
     mov ch, 0
     add si, cx
     mov bl, [di+1]
     mov bh, 0
     cmp bx, cx
     ja invalid_length
     je valid
     jb matching

valid:
     cld
     repe cmpsb
     je found_display
     jne notfound_display

matching:
     mov al, [si]
     mov ah, [di]
     cmp al, ah
     je check
     jne iterate

iterate:  
     inc si
     mov dx, strlen
     dec dx
     cmp dx, 0
     je notfound_display
     jmp matching

check:
     mov cl, [di+1]
     mov ch, 0
     mov ax, si
     add ax, 1
     cld
     repe cmpsb
     jne again
     jmp found_display

again:
     mov si, ax    
     dec dx
     lea di, InputWord
     jmp matching


invalid_length:
     mov ah, 09h
     lea dx, invalid
     int 21h

解决方案

strlen dw ($-InputWord)

This does nothing useful. The length that it calculate can not help you in any way!

;To check if the length of substring is shorter than the main string
 mov cl, [si+1]
 mov ch, 0
 add si, cx
 mov bl, [di+1]
 mov bh, 0
 cmp bx, cx

Here (as Jester told you) the add si, cx instruction is wrong. You need add si, 2 to set SI to the start of the string. You will also need to add add di, 2 to set DI to the start of the word. Do this and the valid part of your program will work correctly.


For the matching part:

Consider the case where the string has 7 characters and the word that you're looking for has 6 characters. You can find the word in at most 2 ways.

Consider the case where the string has 8 characters and the word that you're looking for has 6 characters. You can find the word in at most 3 ways.

Consider the case where the string has 9 characters and the word that you're looking for has 6 characters. You can find the word in at most 4 ways.

Notice the regularity? The number of possible finds is equal to the difference in length plus 1.

    mov     bp, cx      ;CX is length string (long)
    sub     bp, bx      ;BX is length word  (short)
    inc     bp

This sets BP to the number of tries in your matching routine.

    cld
    lea     si, [InputString + 2]
    lea     di, [InputWord + 2]
matching:
    mov     al, [si]    ;Next character from the string
    cmp     al, [di]    ;Always the first character from the word
    je      check
continue:  
    inc     si          ;DI remains at start of the word
    dec     bp
    jnz     matching    ;More tries to do
    jmp     notfound_display

The check part will use repe cmpsb to test for a match, but in the event that the match is not found, you must be able to return to the matching code at the continue label. You have to preserve the registers.

check:
    push    si
    push    di
    mov     cx, bx     ;BX is length of word
    repe cmpsb
    pop     di
    pop     si
    jne     continue
    jmp     found_display

这篇关于在输入字符串中查找子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆