【问题标题】:Run-time exception in MIPS when trying to store register value in data segment尝试在数据段中存储寄存器值时 MIPS 中的运行时异常
【发布时间】:2016-05-26 13:05:16
【问题描述】:

我正在尝试编写一个接受 2 个输入的程序,将它们存储在寄存器中,然后将这些寄存器存储在数据段中。

.data
val1: .word 1
val2: .word 2
val3: .word 3

.asciiz "Branden"
.asciiz "Enter a number "
.asciiz "\n"

.globl main
.text

main:

addi $s0, $0, 23 # initializes the register $s0 to 23

lui $a0, 0x1001
ori $a0, $a0, 20 #outputs string that is at 20
ori $v0, $0, 4 #command for output
syscall
addi $v0, $0, 5 # asks for input
syscall 
addi $s1, $v0, 0 # set the value of $s1 as the given input


lui $a0, 0x1001 
ori $a0, $a0, 20 #outputs string that is at 20
ori $v0, $0, 4 #command for output 
syscall
addi $v0, $0, 5 #asks for input
syscall
addi $s2, $v0, 0 # set the value of $s2 as the given input

sw $s1, 0($t0) # store the value of $s1 into data segment val1
sw $s2, 4($t0) # store the value of $s2 into data segment val2


ori $v0, $0, 10 
syscall

问题是我收到此错误:C:\Users\Danny\MIPS\assignment1.asm 第 34 行错误:0x0040003c 处的运行时异常:地址超出范围 0x00000000

错误发生在 sw $s1, 0($t0) 行,但原因是什么?是否需要与 sw 关联的 lw?

【问题讨论】:

  • 崩溃时t0寄存器里有什么?我没有看到你设置它,所以大概是 0?可能不是正确的答案。
  • 那么将它存储到 val1 中的正确格式是什么?我应该使用 lw 将 val1 存储到 $t0 中吗? @DavidWohlferd
  • 我注意到你正在做一堆lui, ori 序列。你可以使用伪操作(例如la、li)吗?此外,您正在硬连线偏移到您的 .asciiz 字符串。每个人都应该有自己的标签。例如,如果我们将.asciiz "Branden" 更改为.asciiz "brandenfam",您的打印字符串系统调用将中断。
  • 不,不幸的是我不能使用 la 或 li @CraigEstey

标签: exception assembly mips


【解决方案1】:

正如 David 提到的,您没有将 $t0 设置为任何值,所以对于 sw,它为零

但是,即使您想使用该构造,您将如何获得地址? la 让这一切变得简单。

我已经修复了这个错误并重新编码以使用一些让生活更轻松的伪操作 [请原谅无缘无故的风格清理]:

    .data
val1:       .word       1
val2:       .word       2
val3:       .word       3

me:     .asciiz     "Branden"
enter:  .asciiz     "Enter a number "
nl:     .asciiz     "\n"

    .globl  main
    .text

main:
    li      $s0,23                  # initializes the register $s0 to 23

    la      $a0,enter               # address of string to output
    li      $v0,4                   # print string syscall number
    syscall

    li      $v0,5                   # asks for input
    syscall
    move    $s1,$v0                 # set the value of $s1 as the given input

    la      $a0,enter               # address of string to output
    li      $v0,4                   # print string syscall number
    syscall

    li      $v0,5                   # asks for input
    syscall
    move    $s2,$v0                 # set the value of $s2 as the given input

    sw      $s1,val1                # store the value of $s1 into val1
    sw      $s2,val2                # store the value of $s2 into val2

    li      $v0,10
    syscall

    # NOTE: these are alternate ways to do the stores (they're after the exit
    # so they won't be executed)
    la      $t0,val1                # address of val1
    sw      $s1,0($t0)              # store the value of $s1 into val1

    la      $t0,val2                # address of val2
    sw      $s2,0($t0)              # store the value of $s1 into val2

更新:

我不允许使用 la 或 li,但我不知道我可以直接将 val1 与 sw 一起使用,这很有帮助,谢谢!

我故意这样做是为了展示一种形式。

但是,当以这种方式使用sw时,它实际上是一个伪操作。看一下序列,你会看到sw 生成lui $at,...,然后是sw $s1,0($at)。

关于伪操作的事情是它们做了一些基本指令不能做的事情。松散地[使用一些类似 C 的语法],la $t0,val1 生成 lui $at,(&val1 >> 16),然后生成 ori $at,$at,(&val1 & 0xFFFF)

因此,您也许可以手动编码la。但是,[AFAIK],没有办法像伪操作那样指定/拆分符号的“上/下”。

【讨论】:

  • 我不允许使用 la 或 li 但我不知道我可以直接将 val1 与 sw 一起使用,这很有帮助,谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-10-29
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多