【问题标题】:find substring and indices in mips在 mips 中查找子字符串和索引
【发布时间】:2012-12-07 22:50:06
【问题描述】:

我正在尝试找出子字符串和首次出现的索引。但有些不对劲。我比较模式数组的每个元素和字符串数组的每个元素,直到指针到达'\ 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

【问题讨论】:

  • “有问题”是什么意思?究竟会发生什么?你有错误吗?它的行为是否错误?
  • 是的,它正在无限循环。 :(
  • 你能告诉我你输入了什么吗?

标签: mips mips32 spim


【解决方案1】:

我将您的代码用于类似项目,在 inner_loop 中您没有正确的 bne 我只放了一个bne,现在它只打印带有子字符串的字符串..

【讨论】:

    【解决方案2】:
    .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"
    

    【讨论】:

      猜你喜欢
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      相关资源
      最近更新 更多