0

im trying find out substring and first occurrence indices. but something wrong. im comparing each element of pattern array and each element of string array until pointer reach to '\0'. whats the problem. algorithm is totaly wrong ?

#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
ccc
  • 93
  • 1
  • 3
  • 10

2 Answers2

0

I used your code for a similar project, in inner_loop you don't have a proper bne I just put one bne and now it print just the string whit the substring..

Matteo
  • 79
  • 11
0
.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"