MIPS汇编代码,用于计算字符串中的位数



我正在编写一个MIPS汇编代码来计算字符串中的位数。例如,如果用户字符串为:"qapww9…$$64"输出为3。

我使用ascii代码设置整数的边界(48表示0,57表示9(。我想检查每个字符是否大于或等于0,然后小于或等于9,如果是,则在计数器中加一,然后移动到下一个字符。键入字符串输入后,我的代码出现故障

编辑:我收到的反馈是,我没有正确地递增字符串的每个字符。我目前正在做的是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

当字符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

最新更新