开发者

MIPS - compare input string to one stored in memory

I have a functioning string compare method written in MIPS (bit by bit comparison of two strings from an input by the user), but I'm trying to update it to compare the second input with one that I have stored in memory. (If those two are equal, I want to 开发者_C百科use the first string somewhere else).

However, I'm running into some issues. Here's my code:

.data  
str1: .space 20  
str2: .space 20  
msg1:.asciiz "Please enter string (max 20 characters): "  
msg2: .asciiz "\n Please enter method (max 20 chars): "  
msg3:.asciiz "\nSAME"  
msg4:.asciiz "\nNOT SAME"  


.text
.globl main

main:  
    li $v0,4        #loads msg1  
    la $a0,msg1  
    syscall

    li $v0,8
    la $a0,str1
    addi $a1,$zero,20
    syscall          #got string to manipulate

    li $v0,4        #loads msg2
    la $a0,msg2
    syscall

    li $v0,8
    la $a0,str2
    addi $a1,$zero,20
    syscall         #got string method

        la $a0,str1             #pass address of str1  
    la $a1,str2         #pass address of str2  
    jal methodComp      #call methodComp  

    beq $v0,$zero,ok    #check result  
    li $v0,4
    la $a0,msg4
    syscall
    j exit
ok:  
    li $v0,4  
    la $a0,msg3  
    syscall  
exit:  
    li $v0,10  
    syscall  

methodComp:  
    add $t0,$zero,$zero  
    add $t1,$zero,$a0  
    add $t2,$zero,$a1  

loop:  
    lb $t3($t1)         #load a byte from each string  
    lb $t4($t2)  
    beqz $t3,checkt2    #str1 end  
    beqz $t4,missmatch  
    slt $t5,$t3,$t4     #compare two bytes  
    bnez $t5,missmatch  
    addi $t1,$t1,1      #t1 points to the next byte of str1  
    addi $t2,$t2,1  
    j loop  

missmatch:   
    addi $v0,$zero,1  
    j endfunction  
checkt2:  
    bnez $t4,missmatch  
    add $v0,$zero,$zero  

endfunction:  
    jr $ra

Essentially, what I was hoping to do was declare at the beginnning

strToCompare: .asciiz "foo"

and change where I pass the adress of str1 from

la $a0,str1             #pass address of str1 

to

la $a0,strToCompare       #pass address of strToCompare

Does anyone know why this would not work, or a better way to implement this? Thanks a bunch, sorry if my formatting is off, this is my first time posting.


You are missing the line feed character (\n) that your input method (the system call you are using to input data from the user) is adding at the end. You can either preprocess your input string to remove that line feed or add it to your stored string. The latter is easy, just change

strToCompare: .asciiz "foo"

to

strToCompare: .asciiz "foo\n"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜