MIPS汇编程序数组求和功能异常排查及代码优化咨询
First, let's pinpoint the core issue: your code never actually calculates the sum of the input numbers! You're storing all 10 integers into the array correctly, but you never add them to $t1—the register you're using to hold the total. Since $t1 starts at 0 (and stays that way), your output will always be 0, no matter what numbers you input.
A secondary (but important) detail: you should explicitly initialize registers like $t0 (your array index counter) and $t1 (your sum accumulator) to 0. While some MIPS simulators default registers to 0, relying on that isn't good practice—explicit initialization makes your code predictable across environments.
Corrected Code
Here's a fixed version that calculates the sum as you input each number (more efficient than storing first then summing):
.data array: .space 40 # 10 integers * 4 bytes each input: .asciiz "Enter the 10 Elements: \n" output: .asciiz "\nThe sum is: " .text main: li $t0, 0 # Initialize array index counter to 0 li $t1, 0 # Initialize sum accumulator to 0 la $a0, input # Load prompt string address li $v0, 4 # Syscall code for print string syscall jal loop # Jump to input loop la $a0, output # Load result prompt address li $v0, 4 syscall move $a0, $t1 # Move sum to $a0 for printing li $v0, 1 # Syscall code for print integer syscall li $v0, 10 # Exit program syscall loop: beq $t0, 40, end_loop # Exit loop when we've processed 10 elements (40 bytes) li $v0, 5 # Read integer syscall syscall sw $v0, array($t0) # Store input in array (optional if you don't need the array later) add $t1, $t1, $v0 # Add input value to sum accumulator add $t0, $t0, 4 # Increment index by 4 bytes (1 integer) j loop end_loop: jr $ra # Return to main
If you want to store all numbers first and then calculate the sum (maybe for later use), here's an alternative version of the loop termination section:
end_loop: li $t0, 0 # Reset index to 0 for summing sum_loop: beq $t0, 40, print_sum lw $t2, array($t0) # Load element from array add $t1, $t1, $t2 # Add to sum add $t0, $t0, 4 j sum_loop print_sum: jr $ra
Optimization Tips for MIPS Beginners
- Follow register conventions: Use temporary registers (
$t0-$t9) for variables you don't need to preserve across function calls, and saved registers ($s0-$s7) if you need to keep values when calling other functions. Remember to save/restore saved registers to the stack if you use them! - Explicitly initialize all registers: Never assume a register starts at 0—this avoids bugs in simulators or hardware that don't default to zero.
- Avoid redundant operations: If you don't need to keep the input array (just need the sum), you can skip storing the values entirely to save memory and cycles.
- Add clear comments: Comment every section of your code (e.g., "Initialize sum accumulator", "Read integer input")—this makes debugging and revisiting code much easier.
- Test incrementally: Build and test small parts of your program first (e.g., test printing a string, then reading one integer, then summing two integers) before putting it all together.
内容的提问来源于stack exchange,提问作者Liyanna

