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

MIPS汇编中sb指令触发异常问题及无数据段实现求助

Why Your sb Instruction Throws a "Bad Address" Exception (and How to Fix It Without .data)

Great question—you’re already on the right track with your guess about $t3 not holding a valid address. Let’s break this down clearly:

1. The Root Cause: Invalid Address in $t3

Your推测 is 100% correct. Here’s what’s happening:

  • MIPS general-purpose registers like $t3 are uninitialized by default. When your program starts, $t3 contains a random value (often 0x00000000 in simulators like SPIM/MARS).
  • The sb $t2, 0($t3) instruction tries to write a byte to the memory address stored in $t3 plus 0. If $t3 is 0, that’s the null address—reserved by the system, and not writable. Even if it’s another random value, it might point to read-only memory (like the .text segment where your code lives) or an unallocated region, both of which will trigger a "bad address" exception.

When you declare word: .space 10 in .data and use la $t1, word, you’re loading a valid, pre-allocated, writable memory address into $t1—so the sb instruction works as expected.

2. How to Store Bytes Without Declaring Space in .data

You can use the program stack—a dynamic, writable memory region that’s part of your process’s memory space, no need for pre-declaration in .data. Here’s how to do it:

Step-by-Step Implementation:

  1. Reserve space on the stack: Adjust the stack pointer ($sp) to allocate enough bytes for your word (e.g., 10 bytes for a word up to 9 characters plus a null terminator).
  2. Use the stack address as your target: Load or move the stack pointer’s value into your base register (like $t3), then use sb to write bytes to this stack space.
  3. Clean up the stack (optional but good practice): After you’re done with the stack space, restore the stack pointer to its original value to avoid memory leaks or stack corruption.

Example Modified Code:

.data
.text
main:
    # Reserve 10 bytes of stack space for our word
    addi $sp, $sp, -10
    # Move the stack base address to $t3
    move $t3, $sp

    # Your existing logic to read characters goes here
    # For demonstration, let's store 'S' as before
    li $t2, 'S'
    sb $t2, 0($t3)  # Now this works—$t3 points to valid stack memory

    # (Add code to read more characters, check for spaces, etc.)

    # Clean up the stack (restore original pointer)
    addi $sp, $sp, 10

    li $v0, 10
    syscall

Key Takeaways

  • Always ensure your base register (like $t3) points to a valid, writable memory address before using sb, sw, or any store instruction.
  • The .data segment is for static pre-allocated memory; the stack is for dynamic, runtime-allocated memory.
  • Uninitialized registers do not point to valid memory—never assume they hold a usable address.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:17:47