Сравнение строк в MIPS

Следующий код сравнивает две строки. Он увеличивает счетчик, если в Input найдено C. Я хочу знать, где проблема в моем коде. Он не дает вывода.

.data
Input: .asciiz "\nThis is Lab-05 of the course CSCS323 Computer Organization and Assembly Language.We are FCCU/CS Students"
Check: .asciiz "C"
Result: .asciiz "\nThe number of times this character occurs in the string is: "
.text
.globl main
 main:

 la $t0, Input #load address of input
 la $t1, Check #load address of Check
 li $s1, 0     #Set Counter to zero

compare:
lb $t2, 0($t0)
lb $t3, 0($t1)

beq $t2, $t3, counter
addi $t0, $t0, 1

counter:
addi $s1, $s1, 1
jr $ra

add $a0 , $s1 , $zero
li $v0, 1
move $a0, $s1
syscall

person Community    schedule 25.11.2015    source источник
comment
Вы использовали отладчик, чтобы увидеть, что на самом деле происходит? Пошаговое выполнение вашего кода часто приводит к проблемам.   -  person Peter Cordes    schedule 25.11.2015


Ответы (1)


Инструкция jr неверна. Вы возвращаетесь из своей подпрограммы туда. Вы хотите j compare

lb $t3,0($t1) не изменяется в цикле (например, вы увеличиваете $t0, но не $t1), поэтому вы можете переместить инструкцию перед compare:

Вы не выходите из цикла сравнения в конце сканирования Input. То есть после lb $t2,0($t0). Вам нужно сравнить $t2 с нулем

Вы не увеличиваете $t0, если переходите к counter:. Это означает, что вы будете зацикливаться бесконечно [с фиксированным jr].

Вы не используете строку Result

Вот исправленный код. Это может быть не идеально, но это ближе [Пожалуйста, извините за неоправданную очистку стиля]:

.data
Input: .asciiz "\nThis is Lab-05 of the course CSCS323 Computer Organization and Assembly Language.We are FCCU/CS Students"
Check: .asciiz "C"
Result: .asciiz "\nThe number of times this character occurs in the string is: "

.text
.globl main
main:
    # output the result string
    li      $v0,4               # print_string syscall number
    la      $a0,Result          # string address
    syscall

    la      $t0,Input           # load address of Input
    li      $s1,0               # Set Counter to zero

    la      $t1,Check           # load address of Check
    lb      $t3,0($t1)          # get "check" char value

    li      $s2,0               # get end of string (EOS) char

compare:
    lb      $t2,0($t0)          # get next char in 'Input'
    addi    $t0,$t0,1           # advance pointer to next char

    beq     $t2,$s2,done        # is current char 0?  if yes, fly and print

    bne     $t2,$t3,compare     # does current match check? If no, loop
    addi    $s1,$s1,1           # yes, increment count
    b       compare             # loop

done:
    li      $v0,1               # print_int syscall
    move    $a0,$s1             # get total count to correct register
    syscall
person Craig Estey    schedule 25.11.2015