0

I want to access a particular memory location of an array. the condition that i have is somewhat like this

Let us say the array is arr[] have 100 elements and i want to access the 10th elements. so for that i want to move to the 10th memory location. the memory location is defined by the user so it is stored in a data register. so how do i use the value data register to move to the required address. this is the code that i have

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

I also tried this code but it does not work

        lea     morse,a1
        clr.b   d2
        move.b  #13,d2
        move    d2(a1),d3
        move.b  d3,d1
        move.l  #6, d0
        trap    #15
  • This looks strange: move.b (d1,a1,d2.w),d1 See http://www.freescale.com/files/archives/doc/ref_manual/M68000PRM.pdf Table 2-4. Effective Addressing Modes and Categories. Couldn't find any 3-register addressing mode there. Only two-register + one constant. How about in the second version "move d2(a1), d3" => move (a1, d2), d3? (= Address Register Indirect with Index with disp=0). – turboscrew Oct 31 '14 at 15:01
  • Or move.b (a1, d2), d1? – turboscrew Oct 31 '14 at 15:02

1 Answers1

4

To access an index in an array, you first need the address of the array. There are multiple ways to get to a specific index in the array, the conceptually simplest is to just increase the address by arrayIndex times itemSize (if the items are bytes, itemSize == 1 and the term simplified to just arrayIndex). From that follows the address of the first item (with index ZERO) is equal to the array address itself.

Increasing an address is done by simply adding to it, in case of a byte array its as simple as:

 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

Now this changes the address register, which is often not desirable. In that case there are addressing modes which can offset the value in an address register by either a constant, another register (or even both):

 ; 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

Or

 ; 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

The latter is especially useful when the offset is provided by a loop counter, or as a subroutine parameter.

Durandal
  • 19,919
  • 4
  • 36
  • 70