读取小写字母数量的 MIPS 代码
MIPS Code that reads number of lower case letters
我需要编写一个程序,从用户那里读取一个字符串并输出该字符串中小写字母的个数。这是我写的
.data
msg1 : .word 0:24
.text
.globl main
main :
addu $s0 , [=10=] , $ra #save the return address
li $v0 , 8 #syscall for read str
la $a0 , msg1 #load address of msg1 to store string
li $a1 , 100 #msg1 is 100 bytes
syscall
add $t6, $t6, [=10=]
compare :
lb $t0 , 0($a0) #load the character into $t0
beq $t0, [=10=], endloop
li $t1 , 'a' #get value of 'a'
blt $t0 , $t1 , nomodify #do nothing if letter is less than 'a'
li $t1 , 'z' #get value of 'z'
bgt $t0 , $t1 , nomodify #do nothing if letter is great than 'z'
addi $t6, $t6, 1 #add one to the character count
addi $a0, $a0, 1 #move to next character
beq [=10=],[=10=],compare #branch to compare
nomodify :
addi $a0, $a0, 1 #next character
j compare
endloop :
addu $a0, [=10=], $t6
li $v0 , 1 #syscall for print int
syscall
addu $ra , $s0 , [=10=] #restore return address
jr $ra
但是,当它运行时,它因错误而终止,我不完全确定我做错了什么。任何 suggestions/advice 非常感谢!提前致谢。
MARS/SPIM 中有一个 exit
系统调用可用于退出您的应用程序。所以不要用这个结束你的程序:
addu $ra , $s0 , [=10=] #restore return address
jr $ra
你应该使用:
li $v0, 10
syscall # syscall 10 = exit
jr $ra
只是跳回调用您的 main
例程的任何代码。在 MARS 中似乎是 "nowhere",即当您的 main
开始时 运行 $ra
是 0。
在 SPIM 中有一些设置代码调用您的 main
,然后在 return 时执行系统调用 10。所以在 SPIM 中,您的代码将按原样工作。
我需要编写一个程序,从用户那里读取一个字符串并输出该字符串中小写字母的个数。这是我写的
.data
msg1 : .word 0:24
.text
.globl main
main :
addu $s0 , [=10=] , $ra #save the return address
li $v0 , 8 #syscall for read str
la $a0 , msg1 #load address of msg1 to store string
li $a1 , 100 #msg1 is 100 bytes
syscall
add $t6, $t6, [=10=]
compare :
lb $t0 , 0($a0) #load the character into $t0
beq $t0, [=10=], endloop
li $t1 , 'a' #get value of 'a'
blt $t0 , $t1 , nomodify #do nothing if letter is less than 'a'
li $t1 , 'z' #get value of 'z'
bgt $t0 , $t1 , nomodify #do nothing if letter is great than 'z'
addi $t6, $t6, 1 #add one to the character count
addi $a0, $a0, 1 #move to next character
beq [=10=],[=10=],compare #branch to compare
nomodify :
addi $a0, $a0, 1 #next character
j compare
endloop :
addu $a0, [=10=], $t6
li $v0 , 1 #syscall for print int
syscall
addu $ra , $s0 , [=10=] #restore return address
jr $ra
但是,当它运行时,它因错误而终止,我不完全确定我做错了什么。任何 suggestions/advice 非常感谢!提前致谢。
MARS/SPIM 中有一个 exit
系统调用可用于退出您的应用程序。所以不要用这个结束你的程序:
addu $ra , $s0 , [=10=] #restore return address
jr $ra
你应该使用:
li $v0, 10
syscall # syscall 10 = exit
jr $ra
只是跳回调用您的 main
例程的任何代码。在 MARS 中似乎是 "nowhere",即当您的 main
开始时 运行 $ra
是 0。
在 SPIM 中有一些设置代码调用您的 main
,然后在 return 时执行系统调用 10。所以在 SPIM 中,您的代码将按原样工作。