caesar
caesar

Reputation: 3135

accessing first character of an string and comparing it with an char MIPS

I want to compare a first character of a string with '#' char. If these are equal I want to print "they're equal" in mips. To do this, I've written a piece of code as below.However it does not give me an output even if they're equal. Is there anyone to help me ? Thanks in advance.

.data 
input:          .space 201
string2:    .asciiz "they're equal.\n"
finish:         .byte '#'
.text
main:
        la $a0,input
        li $a1,201          #read 200 char 
        li $v0,8            #read string
        syscall 

        jal evaluate

evaluate:
        lw $t1, 0($a0)
        lw $t2,finish
        beq $t1,$t2,testi

testi:
        la $a0,string2
        li $v0,4
        syscall

        li $v0, 10
        syscall

Upvotes: 2

Views: 14881

Answers (1)

Konrad Lindenbach
Konrad Lindenbach

Reputation: 4961

Yes, you have placed the branch in such a way that the next instruction is the same regardless of whether or not the branch is taken.

Consider changing it to something like this:

evaluate:

        lw $t1, 0($a0)
        lw $t2,finish
        bne $t1,$t2,testi

        la $a0,string2
        li $v0,4
        syscall

test1:
        li $v0, 10
        syscall

Upvotes: 1

Related Questions