【问题标题】:I'm getting the error MIPS “spim: (parser) syntax error” when running code运行代码时出现错误 MIPS “spim: (parser) syntax error”
【发布时间】:2021-06-17 06:17:58
【问题描述】:

这是我的代码:

    .data
num1:   .word   # num1 variable
num2:   .word   # num2 variable
max:    .word   # max variable
msg:    .asciiz "Enter an integer " # msg
msg2:   .asciiz "The bigger value is " # msg2
    .text
main:
   la   $a0, msg   # prints msg
   li    $v0, 4
   syscall
   la   $v0, 5       # reads data
   syscall
   # loads address of num1 to $t0
   la $t0, num1
   # moves the content of $v0 to $t0
   move $t0,$v0
   la   $a0, msg   # print msg
   li    $v0, 4
   syscall
   la   $v0, 5       # read data
   syscall
   # loads address of num1 to $t1
   la $t1, num2
   # moves the content of $v0 to $t1
   move $t1,$v0
   # loads address of max to t2
   la $t2, max
   # if $t0 (num1) > $t2 (num2), execute if part/label
   bgt $t0,$t1,if   # go to if label
   # else
   else:
   # moves the contents of $t1 (num2) to $t2 (max)
   move $t2,$t1
   la   $a0, msg2   # print msg2
   li    $v0, 4
   syscall
   move   $a0, $t2   # print max
   li   $v0, 1
   syscall
   li   $v0, 10       # exit
   syscall
   # if
   if:
   # moves contents of $t0 (num1) to $t2 (max)
   move $t2,$t0
   la   $a0, msg2   # print msg2
   li    $v0, 4
   syscall
   move   $a0, $t2   # print max
   li   $v0, 1
   syscall
   li   $v0, 10       # exit
   syscall

每次我尝试在 QTSimp 中运行此代码时,我都会收到错误消息 “spim:文件 C:/Users/danie/Desktop/program_1.asm 第 3 行的(解析器)语法错误 .word # num1 变量"

我做错了什么?谢谢

【问题讨论】:

  • 为你的单词提供一个初始化器。例如。 .word 0.
  • .word 没有 args(如果它确实组装了)将保留 0 字节的空间,用于字初始值设定项的空列表。就像 C 中的 uint32_t array[0] = {};。这显然不是你想要的,所以汇编器阻止你编写无用的东西是件好事。

标签: assembly mips qtspim spim


【解决方案1】:

您希望.word 0 发出一个值为0 的32 位字。

.word 没有操作数将保留 0 字节的空间,用于字初始值设定项的空列表。 (这是其他一些汇编器实际处理它的方式,而不是出错。)所以它就像 C1 中的uint32_t array[] = {};,就像声明一个 0 元素的数组。

这不是任何人想要的(如果他们只是想在同一个地址上放置多个标签,普通人会完全忽略 .word),所以似乎 SPIM 汇编器阻止你编写无用的东西并将其视为一个错误。我想也许你可以想象一个宏对于某些配置扩展为空,这可能就是为什么其他汇编程序选择不出错并将.word 视为获取零个或多个单词值的列表,而不是一个或多个。

脚注 1:除了在 C 中,两个数组即使为空也不能具有相同的地址,或者至少 GCC 避免了这种情况。 GCC 在空数组后确实会发出一些填充:https://godbolt.org/z/5GMT6e

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 2015-07-09
    相关资源
    最近更新 更多