0

I am trying to come up with a basic "if yes then do this, if no then exit" algorithm in mips (using spim simulator). However, bne always branches off whether yes or no (y or n in this case) has been given. I am very new to mips, so I am probably missing something big... or small I don't know. Here is what I have:

.data
    welcome:   .asciiz "Hello World!\n"
    begin: .asciiz "\nEnter a mathematical operator: "
    question: .asciiz "\nWould you like to solve a problem (y/n)? "
    back: .asciiz "You wrote "
    buffer: .space 2
    yes: .asciiz "y"
    exiting: .asciiz "exiting"

.text
.globl main
 main:   
       li $v0, 4       # syscall 4 (print_str)        
       la $a0, welcome # argument: string        
       syscall         # print the string

Loop:
    # ASK IF WANT TO SOLVE A QUESTION
    li $v0, 4     # syscall 4 (print_str)
    la $a0, question  # argument: string
    syscall

     # GET INPUT FROM USER
    li   $v0, 8   # get input
    la   $a0, buffer    # load byte space into address
    li   $a1, 2         # allot the byte space for string
    move $t0,$a0      # save string to t0
    syscall
#EDIT
  lb $t1, yes     #previously la $t1, yes
      lb $t0, 0($t0)  #new

#END EDIT

bne $t0, $t1, Exit

########IF YES, PRINT MESSAGE
########this code is only for testing and doesn't really mean anything
########so you can ignore it

    li $v0, 4       # syscall 4 (print_str)
    la $a0, begin   # argument: string
    syscall


    li $v0, 8   #get input
    la $a0, buffer  #load byte space into address
    li $a1, 20      # allot the byte space for string
    move $t0,$a0    #save string to t0
    syscall


    la $a0,back     #load and print "you wrote" string
    li $v0,4
    syscall


    la $a0, buffer  #reload byte space to primary address
    move $a0,$t0    # primary address = t0 address (load pointer)
    li $v0,4        # print string
    syscall

    j Loop

########### END IF YES

Exit:
    li $v0, 4
    la $a0, exiting
    syscall

    li $v0, 10
    syscall
    jr $ra          # return to caller

So the issue is that $t0 and $t1 are never equal, regardless of what the user inputs. What do I need to do to properly compare the two values?

Colton
  • 1,297
  • 14
  • 27

1 Answers1

1

I believe you need to change the line:

la $t1, yes

to

lb $t1, yes

geg
  • 4,399
  • 4
  • 34
  • 35
  • Welp.. I add the lines "lb $t1, yes" as well as "lb $t0, 0($t0)", thinking that this would load the first byte of both, and it is now working. Can you confirm that is the reason? – Colton Sep 27 '13 at 03:44
  • Try changing "move $t0, $a0" to "move $t0, $v0" and get rid of "lb $t0, 0($t0)" because I think your current setup is loading the address of the console input into $t0 rather than the data itself. That's why it works after you call "lb $t0, 0($t0)" to pull the actual value from that address. – geg Sep 27 '13 at 04:05
  • Hmm it stops working after that... I am not quite sure what you mean. The $t0 is being filled with an address instead of data? Even when I use read_string? I just tried adding two numbers that I enter when prompted (testing 100 + 100), and it spat out 57000000. So I think you are probably right. :p But, I am not sure how to fix it still! – Colton Sep 27 '13 at 04:30
  • I'm new to assembly as well but I think your move instruction is putting the address $a0 of the result in $t0 rather than the input value which would be stored in $v0 after the syscall. Move from $v0 instead of $a0 and comment out "lb $t0, 0($t0)". See if that works. – geg Sep 27 '13 at 05:14
  • Yeah I did that and it broke it. I got the rest of the code working using your last comment, just that particular section we are discussing does not seem to be working. Thank you for your help (especially for sticking around after I already gave you your points :p). So my code is running fine, albeit iffy. – Colton Sep 27 '13 at 05:40
  • Happy to help I need to learn these things too. Debug and post the line where things go awry. – geg Sep 27 '13 at 06:07