第一题:用系统功能调用实现简单输入输出
利用系统功能调用从键盘输入,转换后在屏幕上显示,具体要求如下:
如果输入的是字母(A~Z,区分大小写)或数字(0~9),则将其转换成对应的英文单词后在屏幕上显示,对应关系见下表
若输入的不是字母或数字,则在屏幕上输出字符“*”
每输入一个字符,即时转换并在屏幕上显示
支持反复输入,直到按“?”键结束程序
第一题代码:
.data
suc: .asciiz "\nSuccess!!!Location:"
fail: .asciiz "\r\nFail!!!\r\n"
buf: .space 100
.text
.globl main
main:
la $a0, buf
la $a1, 100
li $v0, 8 #8 means to input a string
syscall
# $a0 = address of input bufferll
# $a1 = maximum number of characters to read
inputchar:
li $v0, 12 # input a char
syscall
addi $t7, $0, '?'
sub $t6, $t7, $v0
beq $t6, $0, exit
add $t0, $0, $0
la $s1, buf
find_loop:
lb $s0, 0($s1)
addi $t2, $0, '\n'
sub $t2, $v0, $t2
beq $t2, $0, none
sub $t1, $v0, $s0
beq $t1, $0, success
addi $t0, $t0, 1
slt $t3, $t0, $a1
beq $t3, $0, failed
addi $s1 $s1, 1
j find_loop
none:
j inputchar
success:
la $a0, suc
li $v0, 4 # 4 means to print a string
syscall
addi $a0, $t0, 1
li $v0, 1
syscall
j inputchar
failed:
la $a0, fail
li $v0, 4
syscall
j inputchar
exit:
li $v0, 10
syscall
第二题:字符串查找比较
利用系统功能调用从键盘输入一个字符串,然后输入单个字符,查找该字符串中是否有该字符(区分大小写)。具体要求如下:
如果找到,则在屏幕上显示:“Success! Location: X”,其中,X为该字符在字符串中第一次出现的位置
如果没找到,则在屏幕上显示:“Fail!”
输入一个字符串后,可以反复输入希望查询的字符,直到按“?”键结束程序
每个输入字符独占一行,输出查找结果独占一行,位置编码从1开始
提示:为避免歧义,字符串内不包含“?”符号
格式示例如下:
abcdefgh
a
Success! Location: 1
x
Fail!
第二题代码:
.data
u_word: .asciiz
"Alpha ","Bravo ","China ","Delta ","Echo ","Foxtrot ",
"Golf ","Hotel ","India ","Juliet ","Kilo ","Lima ",
"Mary ","November ","Oscar ","Paper ","Quebec ","Research ",
"Sierra ","Tango ","Uniform ","Victor ","Whisky ","X-ray ",
"Yankee ","Zulu "
uw_offset: .word
0,7,14,21,28,34,43,49,56,63,71,
77,83,89,99,106,113,121,131,
139,146,155,163,171,178,186
l_word: .asciiz
"alpha ","bravo ","china ","delta ","echo ","foxtrot ",
"golf ","hotel ","india ","juliet ","kilo ","lima ",
"mary ","november ","oscar ","paper ","quebec ","research ",
"sierra ","tango ","uniform ","victor ","whisky ","x-ray ",
"yankee ","zulu "
lw_offset: .word
0,7,14,21,28,34,43,49,56,63,71,
77,83,89,99,106,113,121,131,
139,146,155,163,171,178,186
number: .asciiz
"zero ", "First ", "Second ", "Third ", "Fourth ",
"Fifth ", "Sixth ", "Seventh ","Eighth ","Ninth "
n_offset: .word
0,6,13,21,28,36,43,50,59,67
.text
.globl main
main: li $v0, 12 # read character
syscall
sub $t1, $v0, 63 # '?'
beqz $t1, exit
sub $t1, $v0, 48 # '0'
slt $s0, $t1, $0 # if t1 < 0 then s0 = 1
bnez $s0, others
# is number?
sub $t2, $t1, 10 # number
slt $s1, $t2, $0 # if t2 < 0 then s1 = 1
bnez $s1, getnum
# is capital?
sub $t2, $v0, 91
slt $s3, $t2, $0 # if v0 <= 'Z' then s3 = 1
sub $t3, $v0, 64
sgt $s4, $t3, $0 # if v0 >='A' then s4 = 1
and $s0, $s3, $s4 # if s3 == 1 && s4 == 1
bnez $s0, getuword
# is lower case?
sub $t2, $v0, 123
slt $s3, $t2, $0 # if v0 <= 'z' then s3 = 1
sub $t3, $v0, 96
sgt $s4, $t3, $0 # if v0 >= 'a' then s4 = 1
and $s0, $s3, $s4
bnez $s0, getlword
j others
getnum: add $t2, $t2, 10
sll $t2, $t2, 2
la $s0, n_offset
add $s0, $s0, $t2
lw $s1, ($s0)
la $a0, number
add $a0, $a0, $s1
li $v0, 4
syscall
j main
# upper case word
getuword: sub $t3, $t3, 1
sll $t3, $t3, 2
la $s0, uw_offset
add $s0, $s0, $t3
lw $s1, ($s0)
la $a0, u_word
add $a0, $s1, $a0
li $v0, 4
syscall
j main
# lower case word
getlword: sub $t3, $t3, 1
sll $t3, $t3, 2
la $s0, lw_offset
add $s0, $s0, $t3
lw $s1, ($s0)
la $a0, l_word
add $a0, $s1, $a0
li $v0, 4
syscall
j main
others: and $a0, $0, $0
add $a0, $a0, 42 # '*'
li $v0, 11 # print character
syscall
j main
exit: li $v0, 10 # exit
syscall