Welcome to Dream.In.Code
Getting Help is Easy!

Join 86,392 Programmers. There are 1,404 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Checking for empty strings in SPIM

 
Reply to this topicStart new topic

Checking for empty strings in SPIM, Trying to figure out how to check if the string inputed was an empty o

maihaman
post 27 Apr, 2008 - 05:53 PM
Post #1


New D.I.C Head

*
Joined: 28 Jan, 2008
Posts: 3



This is what i got so far.

CODE

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 biggrin.gif
I don't really need someone to do this for me, just hints on how to check for an empty string. biggrin.gif

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

Thanks in Advance smile.gif
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


pertheusual
post 27 Apr, 2008 - 07:30 PM
Post #2


D.I.C Head

**
Joined: 26 Jan, 2008
Posts: 212

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

CODE

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
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

maihaman
post 28 Apr, 2008 - 11:34 PM
Post #3


New D.I.C Head

*
Joined: 28 Jan, 2008
Posts: 3

I think it worked. biggrin.gif
Thanks you so much pertheusual.
I owe you big time.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

pertheusual
post 29 Apr, 2008 - 12:24 AM
Post #4


D.I.C Head

**
Joined: 26 Jan, 2008
Posts: 212

No problem.

Per
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/17/08 05:09AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month