1. The question is as follows: enter a short string, and compare it with a long string that was saved in advance to see if the input string is a substring of the string.
2. Key points: the string storage method. Strings in MIPs are stored in byte mode. The End mark of a string is 00. Therefore, you can use $ zero to determine the end of a string. However, this topic is special, because '\ n' (0a) is not added at the end of the main string, and' \ n' is bound at the end of the substring input from the keyboard '. If $ zero is used as the mark of the end of the loop, the substring will never match. Therefore, 0a should be used as the end mark of the substring.
3. Program:
. Data
Names:. asciiz "abedc John Jack Niu JOP Mike Judy Belle Lucy Wang"
Tip:. asciiz "Please input name: \ n"
Tip1:. asciiz "find it! \ N"
Tip2:. asciiz "not find! \ N"
Key:. Space 60
. Text
Main:
Li $ v0, 4
La $ A0, tip
Syscall
Li $ v0, 8
La $ A0, key
Li $ A1, 60
Syscall
La $ S1, key
La $ S2, names
Li $ S3, 10
Loop:
LB $ S4, 0 ($ S1)
LB $ S5, 0 ($ S2)
Beq $ S4, $ S3, find
Beqz $ S5, nfind
BNE $ S4, $ S5, forward
Addi $ S1, $ S1, 1
Addi $ S2, $ S2, 1
J Loop
Forward:
La $ S1, key
Addi $ S2, $ S2, 1
J Loop
Find:
Li $ v0, 4
La $ A0, tip1
Syscall
J end
Nfind:
Li $ v0, 4
La $ A0, tip2
Syscall
End: Li $ v0, 10
Syscall