gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

ASM > Arrays

#3684 - regularkid - Wed Mar 05, 2003 3:45 am

Hi!

I am trying to access an array in asm code and can't seem to do it without using a multiply. There has to be a better way of doing this! So, say I want to translate this C code into ASM, I would do:

C Code:
Code:

int x = 10;
myArray[10] = 5;


ASM Code:
Code:

ldr        r0, =myArray
mov     r1, #4
mov     r2, #10
mul      r3, r1, r2
add      r0, r0, r2
ldr        r0, [r0]


This still works, however I have a feeling that there is a much quicker way of doing this. So, I put in the -S compiler option and this is what the above C code translated into:

Code:

mov   ip, sp
stmfd   sp!, {fp, ip, lr, pc}
sub   fp, ip, #4
sub   sp, sp, #4


I have no idea what this is trying to do! Can someone help me try to understand this code or maybe just help me find a quick way to access an element from an array? Thanks so much!
_________________
- RegularKid

#3687 - tepples - Wed Mar 05, 2003 5:41 am

In a 'mov' instruction, you can shift the index term in the address calculation (the third operand) left by a given number of bits. A left-shift is a fast multiply by 2^n. For an array of 32-bit values, you'd use 'asl 2' or something like that. For syntax details, read any ARM assembly documentation.

You can also access the elements of an array sequentially by adding the size of an element to your base pointer every time through the loop. This is fast and frees up a register.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#3691 - Torlus - Wed Mar 05, 2003 8:43 am

To read the 10th element for your array :

Code:

ldr r0, =myArray
ldr r1, =10
ldr r0, [r0, r1, lsl #2]


lsl #2 performs a 2-bit left logical shift, so multiply by 4.

For your question, the C and ASM code of your post does not do the same thing : C code writes value 5 at 10th poisiton, whereas the first ASM code just reads the value. And the second one seems to be "off-topic" :)

Maybe you should have another look at the generated code :)

#3693 - ampz - Wed Mar 05, 2003 11:32 am

I'd say your compiler optimized (removed) the write to the array. Or, the write is done somewhere else.

#3698 - regularkid - Wed Mar 05, 2003 3:57 pm

Awsome! Thanks, Torlus! That worked great. Does that also work for writes? Like, can I do this:

Code:

ldr    r0, =myArray
ldr    r1, =10
ldr    r2, =5
str    [r0, r1, lsl#2], r2


Wait....I just tried that and it wouldn't compile. Any ideas?
_________________
- RegularKid

#3703 - Torlus - Wed Mar 05, 2003 6:29 pm

you need to use :

str r2, [r0, r1, lsl #2]

I think you should have a look a the excellent tutorials available, especially gbaguy's one.
I also keep under my pillow the "Imperial College - University of London" tutorials, which have a good explanation of this kind of memory operations, and quick reference sheet.

You will find it at http://www.ee.ic.ac.uk/pcheung/teaching/ee2_computing/

#3723 - regularkid - Thu Mar 06, 2003 4:51 pm

Great! Thanks alot! This helps me tons!
_________________
- RegularKid