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

MIPS字符串数组程序求助:仅显示最后输入字符串

Fixing Your MIPS String Array Display Issue

Hey there! Let's break down why your program only shows the last entered string, and fix it step by step.

The Root Problem

Right now, your code is storing the memory address of the string buffer into array every time you input a new string. Since all entries in array point to the same string buffer, each new input overwrites the previous one. When you go to display them, every entry just points to the final version of that single buffer—hence why you only see the last string.

Additionally, there's a small syntax error: addi, $t1,$t1,20 has an extra comma (should be addi $t1, $t1, 20), but that's not the main issue here.

The Fix: Copy String Content Instead of Storing Pointers

Instead of storing pointers to the same buffer, we'll copy each input string directly into separate sections of the array (since you allocated 200 bytes, that gives us space for 10 20-byte strings, which fits your 3-input goal).

Here's the corrected code:

.data
string: .space 20       # Temporary buffer for input
array: .space 200       # Array to hold 10 x 20-byte strings
enter: .asciiz "Enter a string:\n"
.text
li $t0, 0               # Offset for array (starts at 0)
li $t2, 3               # Number of strings to input

# Input loop
input_loop:
beqz $t2, display       # Exit input loop if we've got all 3 strings
# Prompt user for input
li $v0, 4
la $a0, enter
syscall
# Read string into temporary buffer
li $v0, 8
la $a0, string
li $a1, 20
syscall
# Copy string from temp buffer to array
la $t1, string          # Source address (temp buffer)
add $t3, $t0, array     # Destination address (array + offset)
copy_loop:
lb $t4, ($t1)           # Load byte from source
sb $t4, ($t3)           # Store byte to destination
beqz $t4, copy_done     # Stop when we hit the null terminator
addi $t1, $t1, 1        # Move to next source byte
addi $t3, $t3, 1        # Move to next destination byte
j copy_loop
copy_done:
addi $t0, $t0, 20       # Move offset to next 20-byte slot in array
addi $t2, $t2, -1       # Decrement input counter
j input_loop

# Display loop
display:
li $t0, 0               # Reset offset to start of array
li $t2, 3               # Number of strings to display
display_loop:
beqz $t2, exit          # Exit if all strings are displayed
li $v0, 4
add $a0, $t0, array     # Load address of current string in array
syscall
addi $t0, $t0, 20       # Move to next string slot
addi $t2, $t2, -1       # Decrement display counter
j display_loop

exit:
li $v0, 10              # Exit program
syscall

Key Changes Explained

  • String Copy: We added a copy_loop that reads each byte from the temporary string buffer and writes it to the corresponding slot in array. This ensures each input is stored in its own unique space.
  • Offset Increment: Instead of adding 4 (for a pointer), we now add 20 to $t0 each time—this moves us to the next 20-byte slot in array for the next string.
  • Display Logic: The display loop now uses the same 20-byte offset to access each stored string directly from array.

This way, each input string is preserved in its own section of the array, and you'll see all three strings when you run the program.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:38:58