Код сборки MIPS для подсчета количества цифр в строке - PullRequest
1 голос
/ 26 февраля 2020

Я пишу код сборки MIPS для подсчета количества цифр в строке. Например, если пользовательская строка была: "qapww9 ... $$$ 64", вывод был бы 3.

Я использовал коды ascii, чтобы установить границу целых чисел (48 для 0, 57 для 9). Я хотел проверить каждый символ, если он больше или равен 0, затем меньше или равен 9, если да, затем добавьте один к счетчику, а затем перейдите к следующему символу. Мой код не работает после ввода строкового значения

EDITS : я получаю сообщение о том, что я неправильно увеличиваю каждый символ строки должным образом. В настоящее время я занимаюсь addi $t0, $t0, 1.

# MIPS assembly language program to count the number of decimal digits in an ascii string of characters.
# $a0 = $t0 = user input
# $t1 = counter
# $t2 = temporary register to hold each byte value
.data
    length: .space 100
    p: .asciiz "\nEnter a string: "
    p2: .asciiz "\nNumber of integers: "
.text

#Prompt User for String
   la $a0, p                   #print prompt
   li $v0, 4                #load string stored in v0
   syscall                  #display prompt string

#Get user input
   li $v0, 8                #load string stored in v0
   la $a0, length              #set string length to 100
   addi $a1, $0, 100           #add length 100 to regist $a1
   syscall

   move $t0, $a0                #user input is now in register $t0
   addi $t1, $0, 0              #initialize counter

#loop to check if greater or equal to 0
lowerBound:
   lb $t2, 0($t0)           #load first character of user input into $t2
   bge $t2, 48, upperBound     #branch to upperBound checker if char is greater than or equal 0
   addi $t0, $t0, 1         #increment to next character in string
   beqz $t0, end            #if character = 0 (end of string), end
   j lowerBound

#loop to check if less than or equal to 9
upperBound:
    ble  $t2, 57, count_digits  #if in the range 0-9, just to count digits and add 1 to counter
    j lowerBound            #loop through again

count_digits:
    addi $t1, $t1, 1           #add 1 to counter
    j lowerBound

end:
    li $v0, 4          #load string print service
    la $a0, p2         #load address of prompt 2 inro $a0
    syscall            #print prompt
    la $a0, ($t1)          #load address of count into $a0
    li $v0, 1          #specify print integer service
    syscall            #print count

1 Ответ

0 голосов
/ 29 февраля 2020

Вы застряли в бесконечном l oop, когда символ ASCII больше 57. Потому что в этом случае вы будете переходить к upperBound, где вы будете бесконечно возвращаться к lowerBound, так как указатель на строку ($ t0 ) не будет увеличиваться в этом случае. Просто переместите инкрементную часть над bge, чтобы он выполнялся всегда.

Также вы пытаетесь распечатать адрес счета по неизвестной причине, а не по значению, как вы должны.

Измененный код

# MIPS assembly language program to count the number of decimal digits in an ascii string of characters.
# $a0 = $t0 = user input
# $t1 = counter
# $t2 = temporary register to hold each byte value
.data
    length: .space 100
    p: .asciiz "\nEnter a string: "
    p2: .asciiz "\nNumber of integers: "
.text

#Prompt User for String
   la $a0, p                   #print prompt
   li $v0, 4                #load string stored in v0
   syscall                  #display prompt string

#Get user input
   li $v0, 8                #load string stored in v0
   la $a0, length              #set string length to 100
   addi $a1, $0, 100           #add length 100 to regist $a1
   syscall

   move $t0, $a0                #user input is now in register $t0
   addi $t1, $0, 0              #initialize counter

#loop to check if greater or equal to 0
lowerBound:
   lb $t2, 0($t0)           #load first character of user input into $t2
   addi $t0, $t0, 1         #increment to next character in string
   bge $t2, 48, upperBound     #branch to upperBound checker if char is greater than or equal 0
   beqz $t2, end            #if character = 0 (end of string), end
   j lowerBound

#loop to check if less than or equal to 9
upperBound:
    ble  $t2, 57, count_digits  #if in the range 0-9, just to count digits and add 1 to counter
    j lowerBound            #loop through again

count_digits:
    addi $t1, $t1, 1           #add 1 to counter
    j lowerBound

end:
    li $v0, 4          #load string print service
    la $a0, p2         #load address of prompt 2 inro $a0
    syscall            #print prompt
    move $a0, $t1          #load count into $a0
    li $v0, 1          #specify print integer service
    syscall            #print count
...