A89: Re: storing from d1 to a0


[Prev][Next][Index][Thread]

A89: Re: storing from d1 to a0




 > I tried it that way but for some reason it's not working for me.  The
 > following code should equal the same thing, shouldn't it?
 > 
 >  move.l #t_stat,a0
 >  move.w d0,(a0)
 > 
 > and
 > 
 >  move.l #s1stat,a0
 >  move.w d0,(a0)
 > 
 > s1stat dc.w    0
 > t_stat dc.l   s1stat,s2stat,etc.
 > 
 > but in my program when I try the second one, it works, but if I try the
 > first one, it doesn't.

Well, they are two different things. I think I understand what you
want to do. 

Do you want to access a data element of which the address is stored in
the table ? That is, something like this:

a dc.w 1
b dc.w 3
c dc.w 99

t dc.l a, b, c

and then you want to read the content of 'b' as the object pointed by
the second element of the table 't' ?
 
If that's what you want, then you have to omit the # in the table
business *or* use explicit indirection. Look at this:

; This code fetches the content of 'a' then stores to 'b':

  move.l  t,a0     ; Note the lack of '#'. A0 is loaded the
                   ; address of 'a'
  move.w  (a0),d0
  move.l  t+4,a0   ; Read a long from t+4, that is a0 is now 
                   ; the address of b
  move.w  d0,(a0)
  
In the above case your code explicitely contained the table indices 0
and 1 (which translate to nothing and 4 in byte offset, respectively). 
If you want a dynamic offset, then do this:


; This code fetches the word pointed by the d1-th element of the 
; table then stores it in the word pointed by the d2-th element of 
; the table. That is, if d1 is 1 and d2 is 2 then, in our example, it
; will make 'c' to be equal to 'b'.

  sll.w  #2,d1            ; Make longword offset from both indices
  sll.w  #2,d2

  move.l #t,a0            ; Get the address of the table into a0

  move.l 0(a0,d1.w),a1    ; Fetch the d1-th element of the table into
                          ; a1. This table element is the address of
                          ; the word object we read from

  move.w (a1),d1          ; Fetch the word object

  add.w  d2,a0            ; This two instructions are an equivalent
  move.l (a0),a1          ; to move.l 0(a0,d2.w),a1 except that a0 is
                          ; changed so it does not point to the table
                          ; any more. Nevertheless, a1 now points to 
                          ; the target.

  move.w d1,(a1)          ; Store the word in the target
  
In general,

  move.l   #x,a0
  
loads the address if x into a0 while 

  move.l   x,a0
  
loads the content of the longword at address x to a0.

Hope this helps,

Regards,

Zoltan


References: