2014-10-31 111 views
0

我想访问数组的特定存储位置。我的条件是这样的使用汇编语言进行存储器访问

让我们说数组是arr []有100个元素,我想访问第10个元素。所以为此我想移动到第10个内存位置。存储器位置由用户定义,因此存储在数据寄存器中。那么我如何使用数值数据寄存器移动到所需的地址。这是代码,我有

 lea  morse,a1 
     clr.b d2 
     add.b #11,d1 
     move.b (d1,a1,d2.w),d1 
     move.l #6,d0 
     trap #15 

我也试过这个代码,但它不工作

 lea  morse,a1 
     clr.b d2 
     move.b #13,d2 
     move d2(a1),d3 
     move.b d3,d1 
     move.l #6, d0 
     trap #15 
+0

这看起来很奇怪:move.b(d1,a1,d2.w),d1请参阅http://www.freescale.com/files/archives/doc/ref_manual/M68000PRM.pdf表2-4。有效的寻址模式和类别。在那里找不到任何3寄存器寻址模式。只有两个寄存器+一个常量。在第二个版本中如何“移动d2(a1),d3”=>移动(a1,d2),d3? (=具有disp = 0的索引的地址寄存器间接)。 – turboscrew 2014-10-31 15:01:27

+0

或move.b(a1,d2),d1? – turboscrew 2014-10-31 15:02:54

回答

3

要访问数组的索引,你首先需要数组的地址。有多种方法可以访问数组中的特定索引,概念上最简单的方法是仅通过arrayIndex times itemSize(如果项目是字节,itemSize == 1并且术语简化为arrayIndex)来增加地址。从这之后,第一项(索引为ZERO)的地址等于数组地址本身。

增加一个地址,通过简单地添加它,在一个字节数组的情况下,完成它的简单:

lea myArray,a0  ; load address of the array 
add.l #10,a0   ; go to index 10 
move.b (a0),d0  ; read the value at index 10 

现在,这改变了地址寄存器,这常常是不希望的。在这种情况下有寻址模式,其可通过一个常数,另一个寄存器偏移值中的地址寄存器(或甚至两个):

; access with constant offset 
lea myArray,a0  ; load address of the array 
move.b 10(a0),d0  ; read from address plus 10, aka 10th index 

或者

; access with register offset 
lea myArray,a0  ; load address of the array 
moveq #10,d1   ; prepare the index we want to access 
move.b (a0,d1.w),d0 ; read from address plus *index* register 

后者时特别有用偏移由循环计数器提供,或作为子程序参数提供。

+0

Thaaaank youuuuuu!非常简洁。 – Pangamma 2015-10-05 03:54:03