MIPs learning notes (1)-string (string) Comparison

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.