MIPS汇编字符串统计程序计数异常求助:数字、大小写等统计错误
MIPS字符串统计程序错误排查与修复
你的程序所有统计项返回24,本质是每个字符都被无条件计入所有统计类别,加上输入字符串(含换行符)总长度刚好为24,所以所有计数器都等于总字符数。以下是具体错误点和修复后的完整代码:
核心错误点
- 计数器无条件递增:判断数字、小写、大写字母的逻辑中,计算出条件结果
$t8后,没有根据$t8的值决定是否递增计数器,而是直接执行addi操作,导致所有字符都被计入对应类别。 - 空格判断逻辑错误:空格判断的
beq指令后没有添加分支跳过非空格的情况,无论字符是否为空格,都会执行increment_space标签下的计数器递增操作,所有字符都被算作空格。 - 语法错误:
- 数据段声明错误:
.date应为.data,错误的段声明会导致变量无法正确寻址。 - syscall指令语法错误:
li $v0.8应为li $v0, 8,逗号被误写为点,导致字符串读取功能失效。 - 跳转指令语法错误:
j-loop应为j loop,连字符被误写为减号,导致循环无法正常跳转。 - 指令格式不规范:多处
li$v0缺少空格(如li$v0,4),虽部分汇编器兼容,但需修正为li $v0, 4的规范格式。
- 数据段声明错误:
修复后的完整代码
.data prompt: .asciiz "Enter the string: " result: .asciiz "Entered string contains:\n" newline: .asciiz "\n" digits: .asciiz " digits" lowercase: .asciiz " lower case" uppercase: .asciiz " upper case" spaces: .asciiz " spaces" buffer: .space 256 # Buffer for the read string .text .globl main main: # Printing the prompt li $v0, 4 la $a0, prompt syscall # Read a string of characters li $v0, 8 la $a0, buffer li $a1, 256 syscall # Initialization of variables li $t0, 0 # Total character count li $t1, 0 # Number of digits li $t2, 0 # Lower case counter li $t3, 0 # Upper case counter li $t4, 0 # Space counter # String processing la $t5, buffer # Address of the first character lb $t6, ($t5) # Read the first character loop: beqz $t6, done # Exit if null character encountered (end of string) addi $t0, $t0, 1 # Increment the counter of all characters # Check if a character is a number li $t7, 48 # '0' in ASCII li $t8, 58 # ':' in ASCII code - limit value slt $t9, $t6, $t8 # Is the character less than ':'? slt $t8, $t7, $t6 # Is '0' less than the character? and $t8, $t8, $t9 # Are both conditions met? beq $t8, 1, increment_digit # Increment only if condition is true j check_lowercase increment_digit: addi $t1, $t1, 1 check_lowercase: # Check if a character is a lowercase letter li $t7, 97 # 'a' in ASCII li $t8, 123 # '{' in ASCII code - limit value slt $t9, $t6, $t8 # Is the character less than '{'? slt $t8, $t7, $t6 # Is 'a' less than the character? and $t8, $t8, $t9 # Are both conditions met? beq $t8, 1, increment_lowercase # Increment only if condition is true j check_uppercase increment_lowercase: addi $t2, $t2, 1 check_uppercase: # Check if a character is an uppercase letter li $t7, 65 # 'A' in ASCII li $t8, 91 # '[' in ASCII code - limit value slt $t9, $t6, $t8 # Is the character less than '['? slt $t8, $t7, $t6 # Is 'A' less than the character? and $t8, $t8, $t9 # Are both conditions met? beq $t8, 1, increment_uppercase # Increment only if condition is true j check_space increment_uppercase: addi $t3, $t3, 1 check_space: # Check if a character is a space li $t7, 32 # ASCII space beq $t6, $t7, increment_space # Increment space counter if character is a space j next_char increment_space: addi $t4, $t4, 1 next_char: addi $t5, $t5, 1 # Go to next character lb $t6, ($t5) # Load next character j loop done: # Displaying results li $v0, 4 la $a0, result syscall # Display the number of all characters li $v0, 1 move $a0, $t0 syscall li $v0, 4 la $a0, newline syscall # Display number of digits li $v0, 1 move $a0, $t1 syscall li $v0, 4 la $a0, digits syscall li $v0, 4 la $a0, newline syscall # Display the number of lowercase letters li $v0, 1 move $a0, $t2 syscall li $v0, 4 la $a0, lowercase syscall li $v0, 4 la $a0, newline syscall # Display the number of capital letters li $v0, 1 move $a0, $t3 syscall li $v0, 4 la $a0, uppercase syscall li $v0, 4 la $a0, newline syscall # Display the number of spaces li $v0, 1 move $a0, $t4 syscall li $v0, 4 la $a0, spaces syscall li $v0, 10 # End of program syscall
修复说明
- 每个类别判断后,通过
beq $t8, 1, increment_xxx指令,仅当条件满足时才跳转到对应计数器递增的标签,否则跳过。 - 空格判断后添加
j next_char,非空格字符不会执行空格计数器递增。 - 修正所有语法错误,规范指令格式。
- 将
buffer移到.data段内,确保变量正确分配内存。
内容的提问来源于stack exchange,提问作者Mikołaj K.
相关产品推荐
相关产品推荐

