Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Multi dimensional array question

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Pikczu

Bascom Member



Joined: 22 Jan 2006
Posts: 72
Location: Dublin, Ireland

ireland.gif
PostPosted: Sun Apr 14, 2019 6:21 pm    Post subject: Multi dimensional array question Reply with quote

HI all,
I'm trying to use multi dimensional arrays, and i don't understand behavior of it:
Online help says:
Quote:
Lets see how memory is organized when using multiple indexes. For the sample we use an array of 5x3 bytes.

Dim ar(5,3) as byte.

This gives us the following possible index values :

1,1 1,2 1,3

2,1 2,2 2,3

3,1 3,2 3,3

4,1 4,2 4,3

5,1 5,2 5,3


Since the memory of the processor is linear, we have 15 cells.




So this is clear for me.
But when I run the test simulator show something different , look at table 2 attached.



However when I run memcopy on the same array addresses are increased linear.


You can see it in the table 3.


Can some one explain this please ?

Test code run it in the simulator.
Code:
$regfile = "m8def.dat"
$crystal = 16000000
$hwstack=40
$swstack=16
$framesize = 32



Dim Multi_array(16 , 6) As Byte                             'destination array
dim A as byte 'used for for next to fill up array
dim byte_count as byte   'used for memcopy to store amount of copied bytes
Dim index as byte 'index for array idex
dim source_array(6) as byte 'source array

'test1



Multi_array(1 , 1) = 1
Multi_array(1 , 2) = 1
Multi_array(1 , 3) = 1
Multi_array(1 , 4) = 1
Multi_array(1 , 5) = 1
Multi_array(1 , 6) = 1

Multi_array(2 , 1) = 2
Multi_array(2 , 2) = 2
Multi_array(2 , 3) = 2
Multi_array(2 , 4) = 2
Multi_array(2 , 5) = 2
Multi_array(2 , 6) = 2

Multi_array(3 , 1) = 3
Multi_array(3 , 2) = 3
Multi_array(3 , 3) = 3
Multi_array(3 , 4) = 3
Multi_array(3 , 5) = 3
Multi_array(3 , 6) = 3

Multi_array(4 , 1) = 4
Multi_array(4 , 2) = 4
Multi_array(4 , 3) = 4
Multi_array(4 , 4) = 4
Multi_array(4 , 5) = 4
Multi_array(4 , 6) = 4

Multi_array(5 , 1) = 5
Multi_array(5 , 2) = 5
Multi_array(5 , 3) = 5
Multi_array(5 , 4) = 5
Multi_array(5 , 5) = 5
Multi_array(5 , 6) = 5

Multi_array(6 , 1) = 6
Multi_array(6 , 2) = 6
Multi_array(6 , 3) = 6
Multi_array(6 , 4) = 6
Multi_array(6 , 5) = 6
Multi_array(6 , 6) = 6

Multi_array(7 , 1) = 7
Multi_array(7 , 2) = 7
Multi_array(7 , 3) = 7
Multi_array(7 , 4) = 7
Multi_array(7 , 5) = 7
Multi_array(7 , 6) = 7

Multi_array(8 , 1) = 8
Multi_array(8 , 2) = 8
Multi_array(8 , 3) = 8
Multi_array(8 , 4) = 8
Multi_array(8 , 5) = 8
Multi_array(8 , 6) = 8

Multi_array(9 , 1) = 9
Multi_array(9 , 2) = 9
Multi_array(9 , 3) = 9
Multi_array(9 , 4) = 9
Multi_array(9 , 5) = 9
Multi_array(9 , 6) = 9

Multi_array(10 , 1) = 10
Multi_array(10 , 2) = 10
Multi_array(10 , 3) = 10
Multi_array(10 , 4) = 10
Multi_array(10 , 5) = 10
Multi_array(10 , 6) = 10

Multi_array(11 , 1) = 11
Multi_array(11 , 2) = 11
Multi_array(11 , 3) = 11
Multi_array(11 , 4) = 11
Multi_array(11 , 5) = 11
Multi_array(11 , 6) = 11

Multi_array(12 , 1) = 12
Multi_array(12 , 2) = 12
Multi_array(12 , 3) = 12
Multi_array(12 , 4) = 12
Multi_array(12 , 5) = 12
Multi_array(12 , 6) = 12

Multi_array(13 , 1) = 13
Multi_array(13 , 2) = 13
Multi_array(13 , 3) = 13
Multi_array(13 , 4) = 13
Multi_array(13 , 5) = 13
Multi_array(13 , 6) = 13

Multi_array(14 , 1) = 14
Multi_array(14 , 2) = 14
Multi_array(14 , 3) = 14
Multi_array(14 , 4) = 14
Multi_array(14 , 5) = 14
Multi_array(14 , 6) = 14

Multi_array(15 , 1) = 15
Multi_array(15 , 2) = 15
Multi_array(15 , 3) = 15
Multi_array(15 , 4) = 15
Multi_array(15 , 5) = 15
Multi_array(15 , 6) = 15

Multi_array(16 , 1) = 16
Multi_array(16 , 2) = 16
Multi_array(16 , 3) = 16
Multi_array(16 , 4) = 16
Multi_array(16 , 5) = 16
Multi_array(16 , 6) = 16


'test part 2

A= 17

for index = 1 to 6
source_array(index) =A
next index

index =1 'set index to one for memcopy


byte_count = Memcopy(source_array(1) , Multi_array(index , 1 ) , 6 , 3)




Do


Loop

End
 

Regards
Pikczu

(BASCOM-AVR version : 2.0.8.1 )


Last edited by Pikczu on Sun Apr 14, 2019 7:15 pm; edited 1 time in total
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Sun Apr 14, 2019 6:54 pm    Post subject: Reply with quote

Maybe this is the problem in data interpretation:
Quote:
Multiple indexes is a new feature in 2078. The simulator does not support this option yet, so for the simulator, only 1 array exists.


P.S. Check this topic for future posts Very Happy FORUM LINK ABOUT PICS
Back to top
View user's profile Visit poster's website
Pikczu

Bascom Member



Joined: 22 Jan 2006
Posts: 72
Location: Dublin, Ireland

ireland.gif
PostPosted: Sun Apr 14, 2019 7:11 pm    Post subject: Reply with quote

Well i'm confused.
That's why I have asked here.
I will edit attachments.
Back to top
View user's profile
Madf

Bascom Member



Joined: 13 Nov 2011
Posts: 179
Location: Moscow

russia.gif
PostPosted: Mon Apr 15, 2019 10:51 am    Post subject: Reply with quote

Hi,

Correct me if I'm wrong...
As I recall, Bascom does not support two-dimensional arrays.
Or has this changed since version 2078? Rolling Eyes
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Mon Apr 15, 2019 11:17 am    Post subject: Reply with quote

@Madf Read Wiki or online Help topic about DIM and scroll into "Arrays" but maybe is worth to reread all Very Happy
https://wiki.mcselec.com/bavr/DIM
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Mon Apr 15, 2019 9:24 pm    Post subject: Re: Multi dimensional array question Reply with quote

Pikczu wrote:
So this is clear for me.

I wouldn't say so. Your yellow/blue/white table is wrong.

Assume A is an leftmost, B the middle, and C the rightmost index.
Increase A by one and in linear view the cell immediately coming next is addressed.
Increase B by one and in linear view the cell indexed by B multiplied A-size with B is addressed. A.s.o.
In your table A and B is interchanged.

Try out the following code and watch within simulator the indexes address the cell within the linear array.
Notice I did use Base= 0, otherwise the calculations would have been slightly more complex.
Code:
$Regfile="m328pdef.dat"
$Crystal=16000000
$hwstack=40
$swstack=16
$framesize=32

Config Base = 0

Const A_size = 7
Const B_size = 4
Const C_size = 6

Const A_width = 1
Const B_width = A_size
Const C_width = A_size * B_size

Const arr_size = A_size * B_size * C_size

Dim mult_arr(A_size, B_size, C_size) As Byte
Dim linarr(arr_size) As Byte At mult_arr Overlay


Dim A As Byte, A_ptr As Byte
Dim B As Byte, B_ptr As Byte
Dim C As Byte, C_ptr As Byte
Dim linptr As Byte

A = 3 : B = 2 : C = 4

  mult_arr(A,B,C) = 29

A_ptr = A
B_ptr = B * B_width
C_ptr = C * C_width
linptr = A_ptr + B_ptr : linptr = linptr + C_ptr
  Print linarr(linptr)

A = 3 : B = 0 : C = 5

  mult_arr(A,B,C) = 36

A_ptr = A
B_ptr = B * B_width
C_ptr = C * C_width
linptr = A_ptr + B_ptr : linptr = linptr + C_ptr
  Print linarr(linptr)

A = 1 : B = 1 : C = 1

  mult_arr(A,B,C) = 96

A_ptr = A
B_ptr = B * B_width
C_ptr = C * C_width
linptr = A_ptr + B_ptr : linptr = linptr + C_ptr
  Print linarr(linptr)

end
Back to top
View user's profile
Pikczu

Bascom Member



Joined: 22 Jan 2006
Posts: 72
Location: Dublin, Ireland

ireland.gif
PostPosted: Tue Apr 16, 2019 5:55 pm    Post subject: Reply with quote

MWS thank you very much for your example.
I will analyze it step by step.
PS.Table pasted by me came from bascom help Confused
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Tue Apr 16, 2019 6:55 pm    Post subject: Reply with quote

Pikczu wrote:
Table pasted by me came from bascom help

Early version Wink
Back to top
View user's profile
Pikczu

Bascom Member



Joined: 22 Jan 2006
Posts: 72
Location: Dublin, Ireland

ireland.gif
PostPosted: Sat Apr 20, 2019 4:01 pm    Post subject: Reply with quote

MWS I have analyzed your example, thank you very much once more for your help.
Now it is very clear for me.
I have created excel spread sheet to visualize memory layout of the arrays.
If someone else have "wrong understanding" of the multidimensional arrays this should help.
Also varprt() is very helpful.
Standard single dimension array:


Two dimensional array:



three indexes array:
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
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 cannot download files in this forum