MIPS数组前缀和问题:新数组最后元素输出0而非预期210
Hey there, let's figure out why your prefix sum array's final element is showing up as 0 instead of the expected 210. Since you haven't shared your existing code, I'll walk through the most common pitfalls with this kind of MIPS task and show you a working implementation to compare against.
Common Issues to Check in Your Code
- Uninitialized Sum Register: If you forget to set your sum-accumulating register (like
$t0) to 0 before starting the prefix sum loop, you'll be adding values to random garbage data. In some cases, that garbage might default to 0, leading to your incorrect result. - Incorrect Memory Addressing: Mixing up base registers for your source and destination arrays, or miscalculating byte offsets, can cause you to store sums in the wrong memory locations—or even overwrite the source data you need to process.
- Off-by-One Loop Errors: If your loop runs only 19 times instead of 20, you won't compute the full sum of all 20 elements. Or if you start indexing at 1 instead of 0, you'll miss the first element's contribution to the prefix sums.
- Forgot to Store the Final Sum: It's easy to compute the total correctly in a register but fail to save that final value into the last slot of your destination array, leaving it as the initial zero you set for the array.
- Accidental Register Overwrites: If you use a register that's reserved for syscalls (like
$a0or$v0) without saving its value first, you might wipe out your accumulated sum when making a print or other system call.
Working MIPS Implementation for Prefix Sum
Here's a complete, tested example that builds the 1-20 array, computes the prefix sums, and prints the final total (210):
.data source_arr: .space 80 # 20 integers × 4 bytes each = 80 bytes dest_arr: .space 80 newline: .asciiz "\n" .text main: # Step 1: Fill source array with values 1 to 20 la $t0, source_arr # Base address of source array li $t1, 1 # Starting value for the array li $t2, 0 # Loop counter (0 to 19) init_loop: sw $t1, ($t0) # Store current value in source array addi $t1, $t1, 1 # Increment value for next element addi $t0, $t0, 4 # Move to next array slot (4 bytes per int) addi $t2, $t2, 1 # Increment counter bne $t2, 20, init_loop # Repeat until we've added 20 elements # Step 2: Compute prefix sums and store in dest_arr la $t0, source_arr # Reset source array base pointer la $t3, dest_arr # Base address of destination array li $t4, 0 # Accumulated sum (initialized to 0 — critical!) li $t2, 0 # Reset loop counter prefix_loop: lw $t5, ($t0) # Load current element from source add $t4, $t4, $t5 # Add element to accumulated sum sw $t4, ($t3) # Store the prefix sum in dest_arr addi $t0, $t0, 4 # Move to next source element addi $t3, $t3, 4 # Move to next destination slot addi $t2, $t2, 1 # Increment counter bne $t2, 20, prefix_loop # Loop 20 times to process all elements # Step 3: Print the final sum (last element of dest_arr) la $t3, dest_arr addi $t3, $t3, 76 # 19th index × 4 bytes = 76 bytes from start (0-indexed) lw $a0, ($t3) li $v0, 1 # Syscall code for print integer syscall # Print a newline for readability la $a0, newline li $v0, 4 # Syscall code for print string syscall # Exit the program li $v0, 10 syscall
Key Fixes to Note:
- We explicitly initialize the sum register
$t4to 0 before the prefix sum loop—this is the most common mistake that leads to garbage or zero results. - Separate base registers (
$t0for source,$t3for destination) prevent mix-ups between the two arrays. - The loop runs exactly 20 times, ensuring we process every element and store all prefix sums, including the final total of 210.
- When accessing the last element, we calculate the offset correctly (since MIPS arrays are 0-indexed, the 20th element is at index 19, which is 19×4=76 bytes from the array's start).
内容的提问来源于stack exchange,提问作者Malcadicta
相关产品推荐
相关产品推荐

