RosettaCodeData/Task/String-matching/MIPS-Assembly/string-matching-1.mips

38 lines
698 B
Plaintext

InString:
;input: $a0 = ptr to string 1
; $a1 = ptr to string 2
; assumes len($a1) <= len($a0)
;out: $v0 = zero-based index where the second string is placed in the first.
;clobbers: $t0,$t1
subiu sp,sp,4 ;set up a stack frame of 4 bytes.
sw $a1,(sp)
li $v0,0
InString_again:
lbu $t0,($a0)
nop
beqz $t0,InString_terminated
nop
lbu $t1,($a1)
nop
beqz $t1,InString_terminated
nop
bne $t0,$t1,InString_noMatch
nop
b InString_overhead
addiu $a1,1
InString_noMatch:
lw $a1,(sp) ;reset the substring pointer if the letters don't match
addiu $v0,1 ;load delay slot
InString_overhead:
addiu $a0,1
b InString_Again
nop
InString_terminated:
addiu sp,sp,4
jr ra
nop