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

MIPS编程咨询:如何不使用la伪指令将整数存入array标签内存

How to Store Data to the array Label Without Using the la Pseudo-Instruction in MIPS

Great question! The la pseudo-instruction is just a convenience tool that combines lui (Load Upper Immediate) and ori (OR Immediate) under the hood to load a full 32-bit address. Since you want to avoid la, you can replicate this logic manually using those two real instructions, paired with assembler directives to grab the high and low 16-bit portions of the array label's address.

Here's the breakdown:

  • The assembler provides %hi(array) which returns the upper 16 bits of the memory address assigned to your array label.
  • %lo(array) returns the lower 16 bits of that address.
  • By loading the high bits with lui and then OR-ing in the low bits with ori, you'll get the full 32-bit address of array in your register—no hardcoded addresses required.

Modified Working Code

.text
.globl main
main:
ori $8, $0, 0       # Initialize counter to 0
ori $9, $0, 25      # Set counter bound (stop when counter hits 25)
# Load array's full address without using la:
lui $10, %hi(array) # Load upper 16 bits of array's address into $10
ori $10, $10, %lo(array) # OR in lower 16 bits to complete the full address

loop:
beq $8, $9, exit    # Exit loop if counter equals the bound
sll $0, $0, 0       # No-op for branch delay slot (your original placeholder works)
sw $8, 0($10)       # Store current counter value to the current array position
sll $0, $0, 0
addiu $8, $8, 1     # Increment counter by 1
addiu $10, $10, 4   # Move to the next 4-byte integer slot in array
j loop
sll $0, $0, 0

exit:
sll $0, $0, 0
.data
array: .space 100   # Reserve 100 bytes (enough for 25 4-byte integers)

Key Changes Explained:

  1. Replaced the hardcoded lui $10, 0x1000 with lui $10, %hi(array) to fetch the correct upper address bits tied to your array label.
  2. Added ori $10, $10, %lo(array) to fill in the lower 16 bits, creating the complete 32-bit address of the array memory region.
  3. Now your program stores values directly into the space reserved by array, instead of relying on a hardcoded address. This makes your code more portable—even if the linker assigns a different address to .data section, your program will still find array correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:35:59