首页 > 解决方案 > mips 中的数组初始化显示错误

问题描述

代码是:

# first Spim program

  .data                 #Global Data here

N: .word 5  #loop count
X: .word 2,4,6,8,10 #array of integers
SUM: .word 0    #location of final sum
str : .asciiz "The sum of the array is ="

  .text
  .globl main   #main defined globally

main: 
  lw $s0, N       #Loop count N(initially must be zero) loaded in $s0
  la $t0,X      #Address of X into t0
  $s1, $s1, $zero   #logical and with zero results in zero

loop:
  lw $t1, 0(#t0)
  add $s1,$s1,$t1
  addi $t0, $t0, $4
  addi $s0, $s0, -1
  bne $0, $s0, loop

  sw $s1, SUM

  li $v0, 10
  syscall 

  .end

错误是

异常发生在 PC=0x0040003c
算术溢出 spim:文件第 7 行的(解析器)语法错误

/home/divyanshu/Documents/QtSpim_Codes_and_stuff/First Qtspim program.txt

.word 2,4,6,8,10 #整数数组

请帮助我如何初始化该数组

提前致谢

标签: arrayssyntax-errormips32qtspim

解决方案


你有很多错误。

  1. 数组元素之间不应有逗号

  2. 可能是代码过去的问题,但字符串上的引号不正确,应该是“

  3. $s1, $s1, $zero(main 的第 3 行)不是一个有效的指令——你想将 s1 设置为 0。

  4. 循环开始:lw $t1, 0(#t0) 无效,# 应该 $

  5. addi $t0, $t0, $4: 表示 t0 = t0 + a4 你要加 4,所以去掉 $ 来当作一个数字。

为我工作:

   .data #Global Data here

N: .word 5 #loop count
X: .word 2 4 6 8 10 #array of integers
SUM: .word 0 #location of final sum
str : .asciiz "The sum of the array is ="

   .text
   .globl main #main defined globally
main: 
   lw $s0, N #Loop count N(initially must be zero) loaded in $s0
   la $t0,X #Address of X into t0
   move $s1, $zero

loop:
  lw $t1, 0($t0)
  add $s1,$s1,$t1
  addi $t0, $t0, 4
  addi $s0, $s0, -1

  bne $0, $s0, loop

  sw $s1, SUM

  li $v0, 10
  syscall

如果在练习中需要它们,您仍然需要添加打印语句等。


推荐阅读