In this case, d1 determines the position to be read and the value is stored in d0:
Code: Select all
lea array, %a0
adda %d1, %a0
move.l (%a0), %d0
Code: Select all
const u32 array[] = {1, 2, 3, 4, 5};
Moderator: BigEvilCorporation
Code: Select all
lea array, %a0
adda %d1, %a0
move.l (%a0), %d0
Code: Select all
const u32 array[] = {1, 2, 3, 4, 5};
LEA can be used to load the address of a section of Memory (W, L).M-374 LX wrote:Is this the right way to read the value at a position of a C array from Assembly?
In this case, d1 determines the position to be read and the value is stored in d0:
A possible declaration in C is:Code: Select all
lea array, %a0 adda %d1, %a0 move.l (%a0), %d0
Code: Select all
const u32 array[] = {1, 2, 3, 4, 5};
Code: Select all
lea array, a0
add.w d0,d0 ; multiply by 4
add.w d0,d0 ; you can replace this part by single lsl.w #2,d0
move.l (a0,d0.w),d1
Code: Select all
moveq #0,d1 ; fast clear (or clr.w d1, or clr.l d1)
move.b d0,d1
lea array, a0
add.w d1,d1 ; multiply by 4
add.w d1,d1
move.l (a0,d1.w),d1
Code: Select all
moveq #0,d1
move.w d0,d1
add.l d1,d1
add.l d1,d1
lea array,a0
move.l 0(a0,d1.l),d0