Reputation: 47
I wrote this code in MIPS to count the # of lowercase letters input by a user:
.data
msg1: .asciiz
count_lower: .space 41
.text
.globl main
main:
la $a0, msg1 #load address of msg1 to store string
li $a1, 100 #msg1 is 100 bytes
li $v0, 8 #syscall for read str
syscall
add $s1, $0, $0 #interger to print later
la $t0, count_lower
sw $a0, 0($t0) #store input string in $t0
loop:
lb $t1, 0($t0) #load char from $t0
beqz $t1, exit #if no more chars, exit
blt $t1, 'a', else #This line and line below are for if char is not lowercase
bgt $t1, 'z', else
addi $s1, $s1, 1 #if char is lowercase, +1 to $s1
sll $t0, $t0, 8 #shift one byte to get to next char
sll $t1, $t1, 8 #shift one byte to get to next char
else:
sll $t0, $t0, 8 #shift one byte to get to next char
sll $t1, $t1, 8 #shift one byte to get to next char
j loop #jump back to loop
exit:
move $a0, $s1 #move interge to print into $a0
li $v0, 1 #print interger in $a0
syscall
li $v0, 10 #exit
syscall
But when I run it, all it does it print the integer "0" no matter what string is input. Can anyone help me troubleshoot? I wrote comments in the code to explain my thought proccess.
EDIT:
1 .data
2 msg1: .space 100
3 .text
4 .globl main
5 main:
6 li $v0, 8 #syscall for read str
7 la $a0, msg1 #load address of msg1 to store string
8 li $a1, 100 #msg1 is 100 bytes
9 move $t0, $a0 #save user input to $t0
10 syscall
11 add $s1, $0, $0 #interger to print later
12 add $t1, $0, $0 #initialize $t1
13 loop:
14 sb $t0, 0($t1) #store rightmost char into memory 0
15 lb $t1, 0($t0) #load char from memory 0 into $t1
16 beqz $t1, exit #if no more chars, exit
17 blt $t1, 'a', else #This line and line below are for if char is not
18 lowercase
19 bgt $t1, 'z', else
20 addi $s1, $s1, 1 #if char is lowercase, +1 to $s1
21 srl $t0, $t0, 8 #shift one byte to get to next char
22 add $t1, $0, $0 #re-initialize $t1
23 j loop #jump back to loop
24 else:
25 srl $t0, $t0, 8 #shift one byte to get to next char
26 add $t1, $0, $0 #re-initialize $t1
27 j loop #jump back to loop
28
29 exit:
30 move $a0, $s1 #move interge to print into $a0
31 li $v0, 1 #print interger in $a0
32 syscall
33 li $v0, 10 #exit
34 syscall
Upvotes: 0
Views: 630
Reputation: 58427
This doesn't do what you seem to think it does:
sw $a0, 0($t0) #store input string in $t0
That instruction only stores the address of the string at ($t0)
(i.e. in the first 4 bytes of count_lower
).
I really don't see why you need two buffers for this. Just read the characters from the same buffer that you specified for syscall 8.
Also, your declaration of msg1
is incorrect. Your comment says #msg1 is 100 bytes
, but your declaration is msg1: .asciiz
, which certainly doesn't reserve 100 bytes. That should be changed to msg1: .space 100
.
Upvotes: 1