第一题
.data #数据段标识 数据声明格式: 变量名: 数据类型 变量值
alphabet: .asciiz "Alpha ","Bravo ","China ","Delta ","Echo ","Foxtrot ","Golf ","Hotel ","India ","Juliet ","Kilo ","Lima ","Mary ","November ","Oscar ","Paper ","Quebec ","Reserch ","Sierra ","Tango ","Uniform ","Victor ","Whisky ","X-ray ","Yankee ","Zulu "
alpha: .asciiz "alpha ","bravo ","china ","delta ","echo ","foxtrot ","golf ","hotel ","india ","juliet ","kilo ","lima ","mary ","november ","oscar ","paper ","quebec ","reserch ","sierra ","tango ","uniform ","victor ","whisky ","x-ray ","yankee ","zulu "
al_offset: .word 0,7,14,21,28,34,43,49,56,63,71,77,83,89,99,106,113,121,130,138,145,154,162,170,177,185#字节
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 #
end : .asciiz "\r\n\r\n\r\n Finish \r\n"
.text
.globl main
#? word length[] = 4
main: li $v0, 12 # li load immmidiate $v0=12 读取系统封输入
syscall #系统调用,获取键盘输入
sub $t1, $v0, 63 # is '?" ? t1 = v0 -63
beqz $t1, exit #判断t1是否为0,是零跳出程序,输入为?
sub $t1, $v0, 48 # t1=v0-48 ascii48 = 0
slt $s0, $t1, $0 # if t1 < 0 then s0 = 1,s0静态变量
bnez $s0, others #不等于0跳转至others
sub $t2, $t1, 10 #t2 = t1 - 10
slt $s1, $t2, $0 #if t2 < 0 then s1 = 1 else s1 = 0
bnez $s1, getnum #s1不等于0跳转至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, getword
# 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, getword2
j others
getnum: add $t2, $t2, 10 #主程序中减了10,加上
sll $t2, $t2, 2 #乘四,一个word类型4字节
la $s0, n_offset #取n_offset首地址
add $s0, $s0, $t2 #地址偏移
lw $s1, ($s0) #s1从$s0地址加载word值
la $a0, number #a0取数字首地址
add $a0, $a0, $s1 #与s1地址偏移量相加
li $v0, 4 #打印
syscall
j main
getword:sub $t3, $t3, 1
sll $t3, $t3,2
la $s0, al_offset
add $s0, $s0, $t3
lw $s1, ($s0)
la $a0, alphabet
add $a0, $s1, $a0
li $v0, 4
syscall
j main
getword2:sub $t3, $t3, 1
sll $t3, $t3,2
la $s0, al_offset
add $s0, $s0, $t3
lw $s1, ($s0)
#sll $s1, $s1, 1
la $a0, alpha
add $a0, $s1, $a0
li $v0, 4
syscall
j main
others: and $a0, $0, $0
add $a0, $a0, 42 #a0=42,赋值*
li $v0, 11 #打印a0指令
syscall #系统调用执行
j main #跳回主程序
exit: la $a0,end
li $v0, 4
syscall