flat assembler
Message board for the users of flat assembler.

Index > Main > Multi-dimensional arrays...

Author
Thread Post new topic Reply to topic
Patrick_



Joined: 11 Mar 2006
Posts: 53
Location: 127.0.0.1
Patrick_
Haven't ever had to use multi-dimensional arrays in assembly until now. What's the best way to do it? I tried:

Code:
mov     eax, 5
lea     ebx, dword [array+array_size*eax]
mov     ecx, dword [ebx+4*4]

array       dd 100
array_size = 100*4    


This should be the equivalent of:

Code:
unsigned int array[10][10];
unsigned int temp = array[5][4];    


but FASM gives me an 'out of range' error. Is there a simpler way to do it than having to do some multiplication instructions?
Post 13 Aug 2008, 23:39
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3035
Location: vpcmipstrm
bitRAKE
One way...
Code:
ARRAY_WIDTH = 10
ARRAY_HEIGHT = 10

mov eax,[X]
imul ebx,[Y],4*ARRAY_WIDTH
mov ecx,[array+eax*4+ebx]

array rd ARRAY_WIDTH * ARRAY_HEIGHT    
Note: addressing modes only support register multiples of 1,2,4, and 8 - Intel calls it the "scale".

Edit: thx, LocoDelAssembly

_________________
¯\(°_o)/¯ unlicense.org


Last edited by bitRAKE on 14 Aug 2008, 02:16; edited 1 time in total
Post 14 Aug 2008, 01:42
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
dd must be rd
Post 14 Aug 2008, 01:58
View user's profile Send private message Reply with quote
Patrick_



Joined: 11 Mar 2006
Posts: 53
Location: 127.0.0.1
Patrick_
Yeah, that should've been an 'rd'. Smile

Thanks guys. For some reason I thought it wouldn't require an extra instruction but it does.
Post 14 Aug 2008, 13:22
View user's profile Send private message Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1159
Azu
If you aligned the array on 4byte boundary shouldn't the lea way work then? Since it would be multiples of 4?
Post 08 Jun 2009, 00:34
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.