I need to write a program that reads a string from the user and outputs the number of lowercase letters in the string. This is what I wrote
.data
msg1 : .word 0:24
.text
.globl main
main :
addu $s0 , $0 , $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, $0
compare :
lb $t0 , 0($a0) #load the character into $t0
beq $t0, $0, 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 $0,$0,compare #branch to compare
nomodify :
addi $a0, $a0, 1 #next character
j compare
endloop :
addu $a0, $0, $t6
li $v0 , 1 #syscall for print int
syscall
addu $ra , $s0 , $0 #restore return address
jr $ra
However, when it runs, it terminates with errors and I'm not entirely sure what it is that I'm doing wrong. Any suggestions/advice is much appreciated! Thanks in advance.
There's an
exit
syscall available in MARS/SPIM to exit your application. So instead of ending your program with this:you should use:
jr $ra
just jumps back to whatever code was calling yourmain
routine. In MARS that appears to be "nowhere", i.e. when yourmain
starts running$ra
is 0.In SPIM there's some setup code that calls your
main
, and then performs a syscall 10 if and when you return. So in SPIM your code would work as-is.