2013-09-27 75 views
0

我想用mips(使用spim模擬器)想出一個基本的「如果是,那麼做到這一點,如果沒有然後退出」算法。然而,不管是yes還是no(在這種情況下是y或n)都會被分支。我對mips非常陌生,所以我可能錯過了一些很大的東西......或者我不知道。這是我有:如何比較預存的字符串和用戶輸入字符串在mips中

.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 

所以問題是,$ t0和$ t1永遠不會是平等的,無論用戶輸入什麼。我需要做什麼才能正確比較這兩個值?

回答

1

我相信你需要更改線路:

la $t1, yes

lb $t1, yes

+0

仍然沒有工作:/ – Colton

+0

韋爾普..我想補充線「磅$ T1,是「以及」lb $ t0,0($ t0)「,認爲這會加載兩者的第一個字節,現在它正在工作。你能證實這是原因嗎? – Colton

+0

嘗試更改「移動$ t0,$ a0」爲「移動$ t0,$ v0」並擺脫「lb $ t0,0($ t0)」,因爲我認爲您當前的設置正在將控制檯輸入的地址加載到$ t0而不是數據本身。這就是爲什麼在您調用「lb $ t0,0($ t0)」從該地址中提取實際值之後它才起作用。 – geg