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 yourarraylabel. %lo(array)returns the lower 16 bits of that address.- By loading the high bits with
luiand then OR-ing in the low bits withori, you'll get the full 32-bit address ofarrayin 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:
- Replaced the hardcoded
lui $10, 0x1000withlui $10, %hi(array)to fetch the correct upper address bits tied to yourarraylabel. - Added
ori $10, $10, %lo(array)to fill in the lower 16 bits, creating the complete 32-bit address of thearraymemory region. - 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.datasection, your program will still findarraycorrectly.
内容的提问来源于stack exchange,提问作者DaeguBlues
相关产品推荐
相关产品推荐

