Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

setting a value into multi index array

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

Bascom Member



Joined: 01 Jul 2006
Posts: 1042

usa.gif
PostPosted: Tue Mar 03, 2015 2:11 am    Post subject: setting a value into multi index array Reply with quote

Hello all,

Can anyone tell me the proper way to set a value into a multi-index array?

Code:

  Dim RxBuffer(64,12) As Byte
  Dim Indx As Byte
 


This is is 12 sets of 64 contiguous bytes. I want to set byte 1 of Indx to the value 127. I have tried the following, nothing seems to work:


Code:

   Local n As Byte
   n = &H7F   '127
   Rxbuffer(1,Indx) = 127
   n = 0
   n = Rxbuffer(1,Indx)
   Print "Test Repeater = "; n

 



Code:

    Local n As Byte
   n = &H7F   '127
   Rxbuffer(1,Indx) = n
   n = 0
   n = Rxbuffer(1,Indx)
   Print "Test Repeater = "; n
 


I never seem to get the value 127 coming out of the array. What am I doing wrong?
Thanks,
Tim



(BASCOM-AVR version : 2.0.7.8 )
Back to top
View user's profile
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1042

usa.gif
PostPosted: Tue Mar 03, 2015 3:03 am    Post subject: Reply with quote

I think I got it, but if there is more to the story, please someone advise me.

Actually, they both worked. The problem I had was I was using the wrong variable 'Indx'. The variable should have been 'Idx'. This is odd too, since I could not find a dimension for Indx = only Idx but will check again since the program did compile!


Code:

Local n As Byte
   n = &H7F   '127
   Rxbuffer(1,Idx) = 127
   n = 0
   n = Rxbuffer(1,Idx)
   Print "Test Repeater = "; n
 


Code:

Local n As Byte
   n = &H7F   '127
   Rxbuffer(1,Idx) = n
   n = 0
   n = Rxbuffer(1,Idx)
   Print "Test Repeater = "; n
 



I suppose, what I do not understand is how the IDE knows to update the first index (1) rather than the second index (Idx).
Tim
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1801

blank.gif
PostPosted: Tue Mar 03, 2015 8:39 am    Post subject: Reply with quote

TSEYFARTH wrote:
I suppose, what I do not understand is how the IDE knows to update the first index (1) rather than the second index (Idx).

Actually we may have the same problem, only that I don't understand, what you don't understand.
Within a compiled program the IDE knows exactly nothing.
For addressing the proper cell in SRam a routine exists within the compilation, which calculates the absolute address out of two indexes.
Whether these indexes are constants or variables is of second order - only they actually should exist.
Quote:
The problem I had was I was using the wrong variable 'Indx'. The variable should have been 'Idx'.

I would consider it as compiler bug, as using non-existent (non-dimensioned) variables must rise an error.
This compiles without errors, without having dimensioned any of the indexes:
Code:
Rxbuffer(nurbel,swurbel) = 127
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4975
Location: Holland

blank.gif
PostPosted: Tue Mar 03, 2015 3:10 pm    Post subject: Reply with quote

yes, the alternative code for multi index does have a bug. the compiler will first check numeric values to calculate the base address and offset. then it will use the variables to calculate at runtime the remaining offset. But the logic only tested for variables and had no case for non existing data.
_________________
Mark
Back to top
View user's profile Visit poster's website
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2470
Location: Queensland

australia.gif
PostPosted: Wed Mar 04, 2015 12:28 am    Post subject: Reply with quote

@Tim

Aside from the bug that the compiler should flag an error for a non-dimensioned variable used as an index, where exactly in your code do you set the value of Idx ( or Indx ) ? If you do not set it, the value will default to 0, which may not be what you want, depending on the CONFIG BASE directive.

If you show a code snippet, it would be much more helpful if you showed the dimensions and values you set for any variables.

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1042

usa.gif
PostPosted: Wed Mar 04, 2015 1:11 am    Post subject: Reply with quote

Hi Adrian,
That value is set elsewhere as a index pointer. The whole problem was with using the wrong variable... once I fixed that, it all worked perfectly. That is why I answered my own post...

The only question that remained was
Quote:
I suppose, what I do not understand is how the IDE knows to update the first index (1) rather than the second index (Idx).


Tim
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2470
Location: Queensland

australia.gif
PostPosted: Wed Mar 04, 2015 2:03 am    Post subject: Reply with quote

Dont quite know what you mean.

You set the values of the two indexes, to 1 and whatever you set idx to, then the compiler inserts code to compute the target address in SRAM based on those settings. Once it has that, setting or getting the value works whether its a simple variable, or an array.

The compiler does not update the indexes at all, only you do that.

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1042

usa.gif
PostPosted: Wed Mar 04, 2015 2:32 am    Post subject: Reply with quote

Hi Adrian,


In a single index array, there is no choice but to assign the index the value
L = 100
Array(L) = 255


In my case, there are 2 indexs Lets call them L and R. L is on the left and r is on the right

Dim Array(50, 50) As Byte
Dim R As Byte
Dim L As Byte

R = 100
Now I want to assign a value to the Byte in the L Index
Array(10, R) = 255

How does *it know* to give the 10th index (L) a value of 255 and not the R index (100) the value of 255?

Does this help clarify the muddy water? I know that it does give the 10 index (L) the value of 255 but how to I know it does, and also then how to change the value of R??

Tim
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2470
Location: Queensland

australia.gif
PostPosted: Wed Mar 04, 2015 3:34 am    Post subject: Reply with quote

Think of the multi-index array as it would actually be arranged in a linear memory space ( since that is what an AVR has ).
If its in row-column order ( corresponding to your L,R indexes, then the indexes 10,20 correspond to 10 rows 'down' and the 20th column in that row.

If your array is dimensioned as 50 * 50, then it has 50 rows of 50 columns per row.
So the address of (10,20) is 10*50 + 20 = 520, relative to the starting address of 1,1 ( or 0.0 if you use BASE = 0 )

You change the value of the R index just by assigning it a new value:

R = 5 'assign the index

Array(3,R) = 50 'assign the value to row 3 and column R ( 5 in this case )

And of course you can assign the left ( row ) index too:

L = 7
R = 25

Array(L,R) = 200 'puts 200 at the 7th row, 25th column = address 7*50 + 25

Somewhere you seem to have missed a lot of basic maths and computation knowledge. If you were never taught it, then I wonder about an education system which is missing this vital and elementary stuff. If you just forgot it, then can I suggest you read some Wiki or texts on basic math.

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1042

usa.gif
PostPosted: Wed Mar 04, 2015 4:30 am    Post subject: Reply with quote

Hi Adrian,

It has nothing to do with 'basic math'.

It has to do with "which array index's value gets changed when array(L,R) = 10? You are saying that it is always the Left index.

Quote:
Array(3,R) = 50 'assign the value to row 3 and column R ( 5 in this case )


This does appear to be correct, but there are no docs that support that - trial does/did. Hence the question.

And yes, our educational system sucks but that is beside the point. Very Happy

Thanks for your response Adrian! Maybe later in the month when I am in the UK I will look you up - if nearby perhaps a visit to the pub!
Tim
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2470
Location: Queensland

australia.gif
PostPosted: Wed Mar 04, 2015 7:33 am    Post subject: Reply with quote

No, in that case I still dont understand what you are saying.

Neither index gets changed when you assign a value to an element in an array, only the value of the element at that row,column address

If you want to address ( for either reading or writing ) the 'next' element, you have to increment whichever index you want. Depends whether you want the next row or the next column.

PS if you want to visit, you will have to go to Australia, not the UK.

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
TSEYFARTH

Bascom Member



Joined: 01 Jul 2006
Posts: 1042

usa.gif
PostPosted: Wed Mar 04, 2015 8:18 am    Post subject: Reply with quote

Hi Adrian,
I think I would rather go "down under" to Australia. Apologies for my ignorance and mistake.

OK - so the value at coordinate L,R is updated. That makes more sense. I suppose I made more of it than necessary. T

Thank you for the clarification.

Tim
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