【问题标题】:Creating (and accessing) an array in MIPS在 MIPS 中创建(和访问)数组
【发布时间】:2011-01-23 00:53:48
【问题描述】:

我正在尝试在 MIPS 程序集中创建一个数组,然后将所有元素添加在一起。但是,当我尝试组装以下内容时,它会说

read_array 第 1 行位置 7 出错:“.word”指令不能出现在文本段中 组装:操作完成但出现错误。

这是我的程序集:

list: .word 3, 2, 1, 0, 1, 2
li $t0, 0x00000000  #initialize a loop counter to $t0
li $t4, 0x00000005  #last index of array
li $t3, 0x00000000  #this will hold our final sum
la $t1, list  #the address of list[0] is in $t1

loop: addi $t0, $t0, 0x00000001 #index++
  add $t5, $t0, $t0 #array index X2
  add $t5, $t0, $t0 #array index X2 again
  add $t6, $t5, $t1 #4x array index in $t6

  lw $t2, 0($t6)   #load list[index] into $t2
  add $t3, $t3, $t2 #$t3 = $t3 + $t2
  beq $t0, $t4, end
  j loop

end:

谢谢!

【问题讨论】:

  • 我知道这段代码中有一些逻辑错误,但我的问题得到了解答。谢谢!
  • 这真的很奇怪,IDK,如果这个限制是为了保护初学者免于将数据与代码混合并在执行落入他们的数据时让他们的程序崩溃,或者什么。在大多数汇编程序中,您可以在任何地方使用 .byte / .worddb / dd 在任何位置发出您想要的任何字节。 (例如,出于某种原因发出指令的非默认编码。)

标签: arrays assembly mips mars-simulator


【解决方案1】:

你必须把这行:

list: .word 3, 2, 1, 0, 1, 2

进入.data 部分。检查这个quick tutorial

【讨论】:

  • “快速教程”链接已关闭。
【解决方案2】:

错误告诉您不能将数据 (.word 3, 2) 放入代码段。 “文本段”是一个旧式术语,意思是“代码段”http://en.wikipedia.org/wiki/Code_segment

汇编器希望你声明一个数据段并将数组放在那里。我从未做过 Mips 汇编器,但我希望它是这样的

.data
list: .word 3, 2, 1, 0, 1, 2

.text
start:
li $t0, 0x00000000  #initialize a loop counter to $t0
li $t4, 0x00000005  #last index of array
li $t3, 0x00000000  #this will hold our final sum
la $t1, list  #the address o

【讨论】:

  • 在我用过的大多数汇编程序中,它会是 .text 而不是 .code
  • @Carl:您可能是对的,尤其是考虑到错误消息。我会改的。
猜你喜欢
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
  • 2011-11-06
  • 2012-09-21
  • 1970-01-01
  • 2010-12-15
  • 2011-12-09
  • 1970-01-01
相关资源
最近更新 更多