Assembly MIPS汇编代码,用于计算字符串中的位数
我正在编写一个MIPS汇编代码来计算字符串中的位数。例如,如果用户字符串为:“qapww9…$$$$64” 输出为3 我使用ascii码设置整数的边界(48代表0,57代表9)。我想检查每个字符是否大于或等于0,然后小于或等于9,如果是,则在计数器中添加一个,然后移动到下一个字符。键入字符串输入后,我的代码失败 编辑 :我收到的反馈是,我没有正确地递增字符串的每个字符。我目前正在做的是Assembly MIPS汇编代码,用于计算字符串中的位数,assembly,mips,mips32,mars-simulator,Assembly,Mips,Mips32,Mars Simulator,我正在编写一个MIPS汇编代码来计算字符串中的位数。例如,如果用户字符串为:“qapww9…$$$$64” 输出为3 我使用ascii码设置整数的边界(48代表0,57代表9)。我想检查每个字符是否大于或等于0,然后小于或等于9,如果是,则在计数器中添加一个,然后移动到下一个字符。键入字符串输入后,我的代码失败 编辑 :我收到的反馈是,我没有正确地递增字符串的每个字符。我目前正在做的是添加$t0,$t0,1 # MIPS assembly language program to count th
添加$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时,您陷入了无休止的循环。因为在本例中,由于指向字符串($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
您正在反复加载同一个字符。您的计数器不是
$t2
,您打算如何准确地到达末尾?哪里出错了?哪里没有递增字符串中要检查的每个字符。我计划用这句话结束:beqz$t2,结束。。。我相信这意味着如果字符$t2=0,那么这意味着字符串是空的,因此我完成了检查。lb$t2,0($t0)
正在从$t0
加载,而$t0
从未更改。此外,如果字符小于48,您只需在下半框中循环,就永远不会退出。addi$t2,$t2,1
这是否会移动到下一个字符?然后它再次循环通过下一个
来检查下一个?$t2
是您加载的字符。增加它是没有意义的。您希望递增$t0
,以便可以使用它将下一个字符加载到$t2
中。