I am trying to write a method that will remove all characters that aren't letters or integers from a string that is input by the user. As of now what happens is that as soon as a character designated for removal is encountered, it just chops off all of the following characters. I don't know why this happens.

Here is the code:

.data
Buffer:	.asciiz "                                                                                "	# 80 bytes in Buffer
intro:	.asciiz "Hello, please enter a string of up to 80 characters.  I will then tell you if that string was a palindrome!"
		.text
main:
	li	$v0, 4		# print_string call number
	la	$a0, intro	# pointer to string in memory
	syscall
	li	$v0, 8		#syscall code for reading string
	la	$a0, Buffer	#save read string into buffer
	li	$a1, 80		#string is 80 bytes long
	syscall
	li	$s0, 0		#i = 0
	li	$t0, 80		#max for i to reach
	la	$a0, Buffer
	jal stripNonAlpha
	li	$v0, 4		# print_string call number
	la	$a0, Buffer	# pointer to string in memory
	syscall
	
#################################
#	stripNonAlpha method	#
#################################
stripNonAlpha:
	beq	$s0, $t0, stripEnd	#if i = 80 end
	add	$t4, $s0, $a0		#address of Buffer[i] in $t4
	lb	$s1, 0($t4)		#load value of Buffer[i]
	slti	$t1, $s1, 48		#if ascii code is less than 48
	bne	$t1, $zero, strip	#remove ascii character
	slti	$t1, $s1, 58		#if ascii code is greater than 57
					#and
	slti	$t2, $s1, 65		#if ascii code is less than 65
	slt	$t3, $t1, $t2		
	bne	$t3, $zero, strip	#remove ascii character
	slti	$t1, $s1, 91		#if ascii code is greater than 90
					#and
	slti	$t2, $s1, 97		#if ascii code is less than 97
	slt	$t3, $t1, $t2
	bne	$t3, $zero, strip	#remove ascii character
	slti	$t1, $s1, 123		#if ascii character is greater than 122
	beq	$t1, $zero, strip	#remove ascii character
	addi	$s0, $s0, 1		#i = i + 1
	j	stripNonAlpha		#go to stripNonAlpha
strip:
	add	$t5, $s0, $a0		#address of Buffer[i] in $t5
	sb	$0, 0($t5)		#Buffer[i] = 0
	addi	$s0, $s0, 1		#i = i + 1
	j	stripNonAlpha		#go to stripNonAlpha
stripEnd:
	la	$a0, Buffer		#save modified string into buffer
	jr	$ra			#return

Thanks in advance, any help is appreciated. I will keep working on this on my own, so I resolve the problem on my own I will let update this thread.

Recommended Answers

All 6 Replies

> add $t5, $s0, $a0 #address of Buffer in $t5
You're using the same index for both. buffer[j] = buffer[i]; j lags behind i for every non-valid character you come across.

Don't forget to put the \0 in the right place when you're done.

Thanks for the quick reply! I understand what you mean, but I am not quite sure on how to implement those changes in my code. If you could elaborate that would be fantastic.

Thanks in advance.

Write the same thing in C (or language of your choice) so that you understand the logic.

Write the same thing in C (or language of your choice) so that you understand the logic.

My subroutine that sets lower case to uppercase acts on the same premise, and it works fine.

toUpperCase:
	beq 	$s0, $t0, upperEnd	#check to see if done
	add	$t4, $s0, $a0		#address of buffer in t4
	lb	$s4, 0($t4)		#value of buffer[i] in s4
	slti	$t1, $s4, 97		#if s4 is more than 97, t1 = 0
	slti	$t2, $s4, 123		#if s4 is less than 123, t2 = 1 
	slt	$t3, $t1, $t2		#if t1 is less than t2, t3 =1
	bne	$t3, $zero, upper	#if t3 is 1, go to upper
	addi	$s0, $s0, 1		#increment i 
	j	toUpperCase
upper:
	add	$t5, $s0, $a0
	addi	$t6, $s4, -32		
	sb	$t6, 0($t5)
	addi	$s0, $s0, 1		#increment i 
	j	toUpperCase
upperEnd:
	la	$a0, Buffer
	jr	$ra

But that wasn't altering the length of the string was it?

Oh you are right, that comment actually helped a lot. Thanks!

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.