使用汇编语言访问内存

时间:2014-10-31 12:34:33

标签: assembly 68000 easy68k

我想访问数组的特定内存位置。我的条件有点像这样

让我们说数组是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

1 个答案:

答案 0 :(得分:3)

要访问数组中的索引,首先需要数组的地址。有多种方法可以获取数组中的特定索引,概念上最简单的方法是通过arrayIndex乘以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

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

相关问题