Skip to content

Instantly share code, notes, and snippets.

@dcb9
Last active April 10, 2019 10:29
Show Gist options
  • Save dcb9/35f1dca9b7afc86196f8efa4c34913ee to your computer and use it in GitHub Desktop.
Save dcb9/35f1dca9b7afc86196f8efa4c34913ee to your computer and use it in GitHub Desktop.
计算机组成第三周第二题
字符串查找比较
利用系统功能调用从键盘输入一个字符串,然后输入单个字符,查找该字符串中是否有该字符(区分大小写)。具体要求如下:
(1) 如果找到,则在屏幕上显示:
Success! Location: X
其中,X为该字符在字符串中第一次出现的位置
(2) 如果没找到,则在屏幕上显示:
Fail!
(3) 输入一个字符串后,可以反复输入希望查询的字符,直到按“?”键结束程序
(4) 每个输入字符独占一行,输出查找结果独占一行,位置编码从1开始。
提示:为避免歧义,字符串内不包含"?"符号
格式示例如下:
abcdefgh
a
Success! Location: 1
x
Fail!
.data
buffer: .space 1024
succMsg: .asciiz "\r\nSuccess! Location: "
failMsg: .asciiz "\r\nFail!\r\n"
newLine: .asciiz "\r\n"
.text
.globl main
main:
# read string
la $a0, buffer
la $a1, 1024
li $v0, 8
syscall
j loop
loop:
# read a character
li $v0, 12
syscall
beq $v0, 63, quit # if $v0 == '?' then quit
la $t0, buffer # reset $t0
li $t1, 1 # offset flag
j find
find:
lb $t2, ($t0)
beq $t2, 0, fail
beq $t2, $v0, success
addi $t1, $t1, 1
addi $t0, $t0, 1
j find
success:
la $a0, succMsg
li $v0, 4
syscall
addi $t1, $t1, 48
move $a0, $t1
li $v0, 11
syscall
la $a0, newLine
li $v0, 4
syscall
j loop
fail:
la $a0, failMsg
li $v0, 4
syscall
j loop
quit:
li $v0, 10 # exit
syscall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment