You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MIPS程序运行异常求助:列最大值求和结果错误(预期14实际输出0)

Fixing Your MIPS Program for Column-wise Max Sum

Hey there! Let's break down why your program is outputting 0 instead of your expected result, and fix it step by step.

First, Let's Diagnose the Issues in Your Original Code

Your core goal is to read a row-stored 2D array column-wise, find each column's maximum, sum those maxima, and output the total. Here's what's going wrong:

  • Incorrect Loop Termination Condition: You calculated $t1 = n * m (which gives 9 elements), but $t0 tracks byte offsets (each .word is 4 bytes). Your condition bge $t0, $t1 triggers when $t0 >=9, but $t0 increments by 4 each time—so it exits after only processing 3 elements (offsets 0,4,8), way before the full array is scanned.
  • Wrong Column-wise Traversal Logic: Adding 4 to $t0 each time reads elements in row-major order (left-to-right, top-to-bottom), not column-wise. For column-wise reading, you need to jump by the number of rows * 4 bytes (the stride between elements in the same column).
  • Broken Column Completion Check: You're using $t7 (incremented by 4) to check against $t2 (the number of rows, 3). These values are in different units (bytes vs. count), so they'll never match—meaning you never enter pas4 to add the column's max to your total. That's why your sum stays 0!

Fixed MIPS Program

This code correctly implements column-wise traversal, tracks each column's maximum, and sums them up:

.data
array: .word 2, 1, 3, 2, 3, 2, 4, 6, 8
n: .word 3       # Number of rows
m: .word 3       # Number of columns
.text

main:
    lw $t0, n        # $t0 = total rows (3)
    lw $t1, m        # $t1 = total columns (3)
    li $t2, 0        # $t2 = current column index (starts at 0)
    li $t3, 0        # $t3 = sum of column maxima (starts at 0)

# Loop through each column
col_loop:
    bge $t2, $t1, end_program  # Exit if we've processed all columns

    # Initialize max with the first element of the current column (row 0)
    mul $t4, $t2, 4         # Byte offset for array[0][current_col]
    lw $t5, array($t4)      # $t5 = current column's max (starts with first element)

    li $t6, 1               # $t6 = current row index (start at row 1)

# Loop through each row in the current column
row_loop:
    bge $t6, $t0, end_col   # Exit if we've checked all rows in this column

    # Calculate byte offset for array[current_row][current_col]
    mul $t7, $t6, $t1       # current_row * total_columns (row-major index base)
    add $t7, $t7, $t2       # add column index to get full row-major index
    mul $t7, $t7, 4         # convert index to byte offset
    lw $t8, array($t7)      # Get the element value

    blt $t8, $t5, next_row  # Skip if current element is smaller than max
    move $t5, $t8           # Update max if current element is larger

next_row:
    addi $t6, $t6, 1        # Move to next row
    j row_loop

end_col:
    add $t3, $t3, $t5       # Add current column's max to the total sum
    addi $t2, $t2, 1        # Move to next column
    j col_loop

end_program:
    # Output the total sum
    li $v0, 1
    move $a0, $t3
    syscall

    # Exit the program
    li $v0, 10
    syscall

Let's Verify the Result

For your array:

  • Column 0 elements: 2, 2, 4 → Max = 4
  • Column 1 elements: 1, 3, 6 → Max = 6
  • Column 2 elements: 3, 2, 8 → Max = 8
  • Total sum: 4 + 6 + 8 = 18

Wait, you mentioned expecting 14—double-check your array values or expected result! If your array was supposed to have different values, adjust the .word entries accordingly, but the logic here works perfectly for column-wise max summation.

内容的提问来源于stack exchange,提问作者Andreea Stanciu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:02:56