[英]Memory accessing in assembly language
我想访问数组的特定内存位置。 我的病情有点像这样
让我们说数组是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
要访问数组中的索引,首先需要该数组的地址。 有多种方法可以获取数组中的特定索引,从概念上讲,最简单的方法是将地址增加arrayIndex乘以itemSize(如果项目是字节,则itemSize == 1,并且术语简化为arrayIndex)。 从此之后,第一项的地址(索引为零)等于数组地址本身。
如果只是字节数组,则只需添加地址即可完成地址的增加 ,例如:
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
当偏移量由循环计数器提供或作为子例程参数提供时,后者特别有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.