Member Avatar for Xeon1st

Hi,
I need to write a program that receives a 20 char string from the user and transforms lowercase to uppercase letter, but it should not affect any other character not included in the lowercase range (97-122, a-z). I wrote my program and the filters for the a-z configuration but it doesn't returns what it should return. Eg. "hello world"----> "HELLO WORLD". It returns only what the array had on the initial state. This is my code:

.data
.align 0
array: .space 20

msg1:	.asciiz "Please enter 20 characters\n"

.text
.globl main

main:

#prompting user for 20 char
addi $v0,$0,4
la $a0,msg1
syscall

#storring string in array
addi $v0,$0,8
la $a0,array
addi $a1,$0,20
syscall


#settting limits and counters
addi $t0,$0,32 			#32 difference
addi $t1,$0,0 			#counter
addi $t2,$0,20 			#limit
addi $t3,$0,97			#limit for lower case chars(a)
addi $t4,$0,123			#limit for lower case chars(z)

loop:

lb $t3,array($t1) 		#loading byte from $t1 place of array

filtering1:			
bge $t3,$t5,filtering2		#if char in array(i)>=97---->filtering2
beqz $t5,exit			#if char in array(i)=null----> exit (it has already printed the rest of chars)
j nothing				#if char in array(i)<97---> print character

filtering2:
blt $t3,$t5,transforming	#if char in array(i)<=122---->filtering2
j nothing			#if char in array(i)>122---> print character

	
transforming:		 	#lower to uppercase
sub $t3,$t3,$t0	  		#substract 32 (eg. 97-32=65 --->A)
sb $t3,array($t1) 		#storring the new sub in place $t1 of array
addi $t1,$t1,1			#i=i+1
j loop				#loop until char in array(i)=null

nothing:
addi $t1,$t1,1	  		#setting counter i=i+1
j loop				#loop until char in array(i)=null


exit:
addi $v0,$0,4			
la $a0,array			#printing array
syscall			


addi $v0,$0,10               #exit
syscall

Thank you for any kind of help!

> beqz $t5,exit #if char in array(i)=null
1. This test should be first
2. Should it be testing $t3, what you loaded from the array?

> lb $t3,array($t1)
Earlier, you assigned $t3 to be the lower bound, and now you're trashing it.

Member Avatar for Xeon1st

Thanks for the pointed problems. I made the corrections but the problem still exists. It doesn't show what it should show. It just printing the characters i already entered. The test with $t3 must be done so the program will recognise if the character is lowercase or not. If it is lower case then it should make it uppercase else do nothing. Do you have any clue what is going on?
Thanks again

Member Avatar for Xeon1st

The $t2 is set to check the counter. This is my latest code. Still not giving the correct result.

.data
.align 0
array: .space 20

msg1:	.asciiz "Please enter 20 characters\n"

.text
.globl main

main:

#prompting user for 20 char
addi $v0,$0,4
la $a0,msg1
syscall

#storring string in array
addi $v0,$0,8
la $a0,array
addi $a1,$0,20
syscall


#settting limits and counters
addi $t0,$0,32 			#difference
addi $t1,$0,0 			#counter
addi $t2,$0,20 			#limit
addi $t3,$0,97			#limit for lower case chars(a)
addi $t4,$0,123			#limit for lower case chars(z)

loop:
bgt $t1,$t2,exit		#checking if counter reached limit(20)
lb $t5,array($t1) 		#loading byte from $t1 place of array

filtering1:			
beqz $t5,exit			#if char in array(i)=null----> exit (it has already printed the rest of chars)
bge $t3,$t5,filtering2		#if char in array(i)>=97---->filtering2

j nothing				#if char in array(i)<97---> print character

filtering2:
blt $t3,$t5,transforming	#if char in array(i)<=122---->filtering2
j nothing			#if char in array(i)>122---> print character

	
transforming:		 	#lower to uppercase
sub $t3,$t3,$t0	  		#substract 32 
sb $t3,array($t1) 		#storring the new sub in place $t1 of array
addi $t1,$t1,1			#i=i+1
j loop				#loop until char in array(i)=null

nothing:
addi $t1,$t1,1	  		#setting counter i=i+1
j loop				#loop until char in array(i)=null


exit:
addi $v0,$0,4			
lw $a0,array			#printing array
syscall			


addi $v0,$0,10
syscall
Member Avatar for Xeon1st

By the way there is another correction. At "exit" function the lw $a0,array is lb $a0,array.

> addi $v0,$0,4
> la $a0,msg1
This is how you print a message to begin with - note the 'la'

> At "exit" function the lw $a0,array is lb $a0,array.
Both of which would seem to be wrong, what about la, to match the example at the start.
You're supposed to be loading the address at where the array starts, not some character stored at the start of the array.

Do you have a debugger, to allow you to single step the code and observe the changes in memory?

Member Avatar for Xeon1st

Done that, still nothing. I have PC SPIM.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.