在mips中查找子字符串和索引

在mips中查找子字符串和索引,mips,mips32,spim,Mips,Mips32,Spim,我试图找出子串和首次出现的索引。但有点不对劲。我正在比较模式数组的每个元素和字符串数组的每个元素,直到指针指向“\0”。有什么问题吗。算法完全错了 #Note: $v0 is a symbolic name used by the assember for $2. # $a0 is a symbolic name used by the assember for $4. .data prompt_str: .asciiz "Please type a

我试图找出子串和首次出现的索引。但有点不对劲。我正在比较模式数组的每个元素和字符串数组的每个元素,直到指针指向“\0”。有什么问题吗。算法完全错了

#Note: $v0 is a symbolic name used by the assember for $2.
#      $a0 is a symbolic name used by the assember for $4.

          .data

prompt_str:     .asciiz  "Please type a text string: "
prompt_ptr: .asciiz  "Please type a pattern string: "
print_yes:  .asciiz  "Yes, there is a match."
print_no:   .asciiz  "No, there is no match."
text_str:   .asciiz  "Text string : "
pattern_str:    .asciiz  "Pattern string : "
print_out:      .asciiz  "Output to be produced :"
print_dash:     .asciiz  "----------------------"
print_index:    .asciiz  "Starting index :"
print_msg :     .asciiz  "Length of longest partial match = "
nl:             .asciiz  "\n"
print_outer:    .asciiz  "please enter string"    
str :       .space  81
ptr :       .space  81
tmp :       .space  81
          .text



main:   la  $a0, prompt_str   
        li  $v0, 4          #print_string command.
        syscall

        la  $a0,str   #read string
    li  $a1,81
    li  $v0,8
    syscall

    la  $t0,str     #move string to $t0

    la  $a0,prompt_ptr
    li  $v0,4       #print pattern command
    syscall

    la  $a0,ptr     #read pattern
    li  $a1,81
    li  $v0,8
    syscall

    la  $t1,ptr     #move pattern to $t1    

    lb  $t2,0($t0)  #pointer first element array of string
    move    $t4,$t2     #address pointer of $t2
    lb  $t3,0($t1)  #pointer first element array of pattern

outer_loop :    beq $t2,$0,end_outer_loop
        j   inner_loop  

inner_loop :    beq $t2,$0,end_inner_loop
        beq $t3,$0,end_inner_loop
        beq $t2,$t3,end_inner_loop
        addiu   $t2,$t2,1
        addiu   $t3,$t3,1
        j   inner_loop

end_inner_loop :bne $t3,$0,inc_ptr
        j   print_match

inc_ptr :   add $t2,$t4,1   
        j   outer_loop

end_outer_loop :la  $a0,print_outer
        li  $v0,4
        syscall





print_match :   la  $a0,text_str    #print string
        li  $v0,4
        syscall
        move    $a0,$t0
        li  $v0,4
        syscall

        la  $a0,nl      #print newline character
        li  $v0,4
        syscall

        la  $a0,pattern_str #print pattern string
        li  $v0,4
        syscall
        move    $a0,$t1
        li  $v0,4
        syscall

        la  $a0,nl      #print newline character
        li  $v0,4
        syscall         

        la  $a0,print_out   #print output line and newline character
        li  $v0,4
        syscall
        la  $a0,nl
        li  $v0,4
        syscall

        la  $a0,print_dash
        li  $v0,4
        syscall
        la  $a0,print_yes
        li  $v0,4
        syscall
        la  $a0,print_index     #print starting index
        li  $v0,4
        syscall
        li  $v0,10
        syscall

end_loop :  li  $v0,10
        syscall

我在一个类似的项目中使用了您的代码,在内部循环中,您没有合适的
bne

我只放了一个
bne
,现在它只打印带子字符串的字符串

“出错”是什么意思?到底发生了什么?你有错误吗?它的行为是错误的吗?是的,它正在进行无限循环(你能告诉我你输入了什么吗?
.text
.globl main

main:
    li $v0, 4
    la $a0, msg1
    syscall

    li $v0, 8
    la $a0, strMain
    li $a1, 99
    syscall

    li $v0, 4
    la $a0, msg2
    syscall

    li $v0, 8
    la $a0, strSub
    li $a1, 99
    syscall

    la $a0,strMain
    jal findLengthString
    move $a2, $v0

    la $a0, strSub
    jal findLengthString
    move $a3, $v0 # M
    sub $a2, $a2, $a3 # N-M
    

    la $a0, strMain
    la $a1, strSub 

    jal subStringMatch
    move $t1, $v0


    li $v0, 1
    move $a0, $t1
    syscall
    
exit:
    li $v0, 10
    syscall

    lb $t9, endline

findLengthString:
    li $t0, -1
    move $s0, $a0

    loop_fls:
        lb $t1, 0($s0)
        beq $t1, $t9, foundLength

        addi $t0, $t0, 1
        addi $s0, $s0, 1
        j loop_fls

    foundLength:
        move $v0, $t0
        jr $ra

subStringMatch:
    li $t0, 0 #i
    loop1:
        bgt $t0,$a2, loop1done  
        li $t1, 0 #j
        loop2:
            bge $t1, $a3, loop2done
            add $t3, $t0, $t1
            add $t4, $a0, $t3
            lb $t3, 0($t4) # main[i+j] 

            add $t4, $a1, $t1
            lb $t4, 0($t4) # sub[j]
            # if a0[i + j] != a1[j]
            bne $t3, $t4, break1

            addi $t1, $t1, 1
            j loop2
        loop2done:
            beq $t1, $a3, yesReturn
            j break1
        yesReturn:
            move $v0, $t0
            jr $ra
    break1:
        addi $t0, $t0, 1
        j loop1
    loop1done:
        li $v0, -1
        jr $ra

.data

msg1: .asciiz "Enter Main String: "
msg2: .asciiz "Enter String to Check SubString: "

strMain: .space 100
strSub: .space 100
endline: .asciiz "\n"