Checking for empty strings in SPIM

Trying to figure out how to check if the string inputed was an empty o

Page 1 of 1

3 Replies - 7728 Views - Last Post: 29 April 2008 - 12:24 AM Rate Topic: -----

#1 maihaman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-January 08

Checking for empty strings in SPIM

Posted 27 April 2008 - 05:53 PM

This is what i got so far.

loop:
	la		 $v0,8		#takes an input
	syscall
	move   $t0,$v0

	sw	   $t1, \n
							#Branch if input is empty string.
	beq	 $t0, $t1, done
		
	j loop

done:
	#etc...



I have no clue if i'm on the right track or not. This is one of my first times using SPIM

Any help is appreciated.

I'm basically trying to loop while my input is not an empty string.

I'm getting an error at the "sw" line. I could be doing this totally wrong, if thats the case i would appreciated being told :D
I don't really need someone to do this for me, just hints on how to check for an empty string. :D

From what I've been getting from my book bne is for not equal and beq is for equal comparisons.

Thanks in Advance :)

Is This A Good Question/Topic? 0
  • +

Replies To: Checking for empty strings in SPIM

#2 pertheusual   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Checking for empty strings in SPIM

Posted 27 April 2008 - 07:30 PM

I haven't done MIPS in a while, so bear with me.

loop:
	li		 $v0,8		#takes an input
	syscall
		li	   $t0, 0			  #I forget if "lb" will zero the rest of the register, so do it first or check.
		lb	  $t0, ($a0)	 #copy first byte from string(which starts at mem address in $a0

	li	   $t1, 10	#ascii for '\n'

	bne	 $t0, $t1, loop
		
	#etc...



You seem to have your operators a bit confused. la is for loading arrays, so normally you would pass it the identifier for memory declared in the .data block. sw is for taking from registers and putting them into memory.

li is what you should use to load integer values into a register.

You also don't need to have the jump. You can just say while not equal, go back to the start using "bne"
I don't remember if you can load character values using li, but I doubt it. 10 is the int value of '\n' I think.

Syscall for strings also loads the memory address of the string into $a0, not $v0.

I've adjusted the code to reflect what I mentioned.
I think that's all correct. I haven't done this in a year though.

Good luck!

Per
Was This Post Helpful? 0
  • +
  • -

#3 maihaman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-January 08

Re: Checking for empty strings in SPIM

Posted 28 April 2008 - 11:34 PM

I think it worked. :D
Thanks you so much pertheusual.
I owe you big time.
Was This Post Helpful? 0
  • +
  • -

#4 pertheusual   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Checking for empty strings in SPIM

Posted 29 April 2008 - 12:24 AM

No problem.

Per
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1