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

MIPS代码中%hi与%lo指令报错问题及替换方式咨询

Answer

First, let's break down your errors and fix them step by step:

1. Fixing the %hi/%lo Parser Errors

Yes, you absolutely need to adjust the %hi and %lo syntax—SPIM's assembler doesn't recognize the % prefix for these directives. Instead, use hi() and lo() without the percent sign. This is because different MIPS assemblers have slightly different syntax conventions: GNU as uses %hi/%lo, but SPIM (and MARS, another common simulator) uses hi/lo.

Example Fix for Your First Code Snippet

Original problematic lines:

lui $v0,%hi(length)
lw $v1,%lo(length)($v0)

Corrected version:

lui $v0, hi(length)
lw $v1, lo(length)($v0)

A more concise alternative is to use the la (load address) instruction, which automatically handles splitting the symbol's address into high and low 16-bit parts for you:

la $v0, length  # Loads the full address of 'length' into $v0
lw $v1, 0($v0)  # Dereferences the address to get the value of 'length'

2. Fixing the Undefined Symbol Error

The second error:

Instruction references undefined symbol at 0x00400038 [0x00400038] 0x0c000000 jal 0x00000000 [init_measure]

This means your code is trying to jump to init_measure, but that label isn't defined anywhere in your assembly file. To fix this:

  • Ensure there's a label init_measure: followed by the corresponding code somewhere in measurement.s.
  • If init_measure is defined in another file, declare it as an external symbol at the top of your file with .extern init_measure.

Applying the Same Fix to Your Other Code Snippets

For all other instances of %hi and %lo in your code, replace them with hi and lo respectively. Here are examples:

First Additional Snippet Corrected

Original:

lui $a0,%hi(grid)
...
addiu $v1,$a0,%lo(grid)

Corrected:

lui $a0, hi(grid)
...
addiu $v1,$a0, lo(grid)

Or using la for simplicity:

la $a0, grid
addiu $v1,$a0, 0  # Since grid's address is already in $a0

Second Additional Snippet Corrected

Original:

lui $v0,%hi(column)
lb $v0,%lo(column)($v0)
...
lui $v0,%hi(column)
addiu $v1,$v0,%lo(column)
...
lui $v0,%hi(growth)
sw $v1,%lo(growth)($v0)

Corrected:

lui $v0, hi(column)
lb $v0, lo(column)($v0)
...
lui $v0, hi(column)
addiu $v1,$v0, lo(column)
...
lui $v0, hi(growth)
sw $v1, lo(growth)($v0)

Again, la can simplify parts of this code (e.g., la $v0, column followed by lb $v0, 0($v0)).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 21:32:36