Last active
April 10, 2019 10:28
-
-
Save dcb9/f6e32b82fcee573c8a55d79c938de254 to your computer and use it in GitHub Desktop.
计算机组成第三周第一题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
用系统功能调用实现简单输入输出 | |
利用系统功能调用从键盘输入,转换后在屏幕上显示,具体要求如下: | |
(1) 如果输入的是字母(A~Z,区分大小写)或数字(0~9),则将其转换成对应的英文单词后在屏幕上显示,对应关系见下表 | |
(2) 若输入的不是字母或数字,则在屏幕上输出字符“*”, | |
(3) 每输入一个字符,即时转换并在屏幕上显示, | |
(4) 支持反复输入,直到按“?”键结束程序。 | |
A Alpha N November 1 First a alpha n november | |
B Bravo O Oscar 2 Second b bravo o oscar | |
C China P Paper 3 Third c china p paper | |
D Delta Q Quebec 4 Fourth d delta q quebec | |
E Echo R Research 5 Fifth e echo r research | |
F Foxtrot S Sierra 6 Sixth f foxtrot s sierra | |
G Golf T Tango 7 Seventh g golf t tango | |
H Hotel U Uniform 8 Eighth h hotel u uniform | |
I India V Victor 9 Ninth i india v victor | |
J Juliet W Whisky 0 zero j juliet w whisky | |
K Kilo X X-ray k kilo x x-ray | |
L Lima Y Yankee l lima y yankee | |
M Mary Z Zulu m mary z zulu |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.data | |
number_words: .asciiz | |
" zero\r\n"," First\r\n"," Second\r\n"," Third\r\n", " Fourth\r\n", " Fifth\r\n", " Sixth\r\n", " Seventh\r\n", " Eighth\r\n", " Ninth\r\n" | |
number_offset: .word | |
0, 8, 17, 27, 36, 46, 55, 64, 75, 85 | |
uppercase_words: .asciiz | |
" Alpha\r\n", " Bravo\r\n", " China\r\n", " Delta\r\n", " Echo\r\n", " Foxtrot\r\n", " Golf\r\n", " Hotel\r\n", " India\r\n", " Juliet\r\n", " Kilo\r\n", " Lima\r\n", " Mary\r\n", " November\r\n", " Oscar\r\n", " Paper\r\n", " Quebec\r\n", " Research\r\n", " Sierra\r\n", " Tango\r\n", " Uniform\r\n", " Victor\r\n", " Whisky\r\n", " X-ray\r\n", " Yankee\r\n", " Zulu\r\n" | |
uppercase_offset: .word | |
0, 9, 18, 27, 36, 44, 55, 63, 72, 81, 91, 99, 107, 115, 127, 136, 145, 155, 167, 177, 186, 197, 207, 217, 226, 236 | |
lowercase_words: .asciiz | |
" alpha\r\n", " bravo\r\n", " china\r\n", " delta\r\n", " echo\r\n", " foxtrot\r\n", " golf\r\n", " hotel\r\n", " india\r\n", " juliet\r\n", " november\r\n", " oscar\r\n", " paper\r\n", " quebec\r\n", " research\r\n", " sierra\r\n", " tango\r\n", " uniform\r\n", " victor\r\n", " whisky\r\n", " kilo\r\n", " lima\r\n", " mary\r\n", " x-ray\r\n", " yankee\r\n", " zulu\r\n" | |
lowercase_offset: .word | |
0, 9, 18, 27, 36, 44, 55, 63, 72, 81, 91, 103, 112, 121, 131, 143, 153, 162, 173, 183, 193, 201, 209, 217, 226, 236 | |
.text | |
.globl main | |
main: | |
# read a character | |
li $v0, 12 | |
syscall | |
beq $v0, 63, quit # if $v0 == '?' then quit | |
## check number begin | |
# check if the character is less than '0' | |
sub $t0, $v0, 48 # $t0 = $v0(the character we just received from input) - 48 | |
bltz $t0, unknown_character # Branch on less than zero | |
# check if the character is less than or euqal to '9' | |
sub $t0, $v0, 57 | |
blez $t0, print_number_word | |
## check number end | |
## check uppercase begin | |
sub $t0, $v0, 65 | |
bltz $t0, unknown_character # Branch on less than zero | |
# check if the character is less than or euqal to 'Z' | |
sub $t0, $v0, 90 | |
blez $t0, print_uppercase_word | |
## check uppercase end | |
## check lowercase begin | |
sub $t0, $v0, 97 | |
bltz $t0, unknown_character # Branch on less than zero | |
# check if the character is less than or euqal to 'z' | |
sub $t0, $v0, 122 | |
blez $t0, print_lowercase_word | |
## check lowercase end | |
j unknown_character | |
print_number_word: | |
sub $t0, $v0, 48 # get number | |
sll $t0, $t0, 2 # for 1 word is equal to 2 bytes | |
la $t1, number_offset | |
add $t1, $t1, $t0 | |
lw $t1, ($t1) | |
la $a0, number_words | |
add $a0, $a0, $t1 | |
li $v0, 4 | |
syscall | |
j main | |
print_uppercase_word: | |
sub $t0, $v0, 65 | |
sll $t0, $t0, 2 # for 1 word is equal to 2 bytes | |
la $t1, uppercase_offset | |
add $t1, $t1, $t0 | |
lw $t1, ($t1) | |
la $a0, uppercase_words | |
add $a0, $a0, $t1 | |
li $v0, 4 | |
syscall | |
j main | |
print_lowercase_word: | |
sub $t0, $v0, 97 | |
sll $t0, $t0, 2 # for 1 word is equal to 2 bytes | |
la $t1, lowercase_offset | |
add $t1, $t1, $t0 | |
lw $t1, ($t1) | |
la $a0, lowercase_words | |
add $a0, $a0, $t1 | |
li $v0, 4 | |
syscall | |
j main | |
unknown_character: | |
li $a0, 42 # '*' | |
li $v0, 11 # print a character | |
syscall | |
j main | |
quit: | |
li $v0, 10 # exit | |
syscall | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment