View previous topic :: View next topic |
Author |
Message |
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Tue Mar 03, 2015 2:11 am Post subject: setting a value into multi index array |
|
|
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 |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Tue Mar 03, 2015 3:03 am Post subject: |
|
|
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 |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Tue Mar 03, 2015 8:39 am Post subject: |
|
|
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 |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Tue Mar 03, 2015 3:10 pm Post subject: |
|
|
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 |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Wed Mar 04, 2015 12:28 am Post subject: |
|
|
@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 |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Wed Mar 04, 2015 1:11 am Post subject: |
|
|
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 |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Wed Mar 04, 2015 2:03 am Post subject: |
|
|
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 |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Wed Mar 04, 2015 2:32 am Post subject: |
|
|
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 |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Wed Mar 04, 2015 3:34 am Post subject: |
|
|
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 |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Wed Mar 04, 2015 4:30 am Post subject: |
|
|
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.
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 |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Wed Mar 04, 2015 7:33 am Post subject: |
|
|
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 |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Wed Mar 04, 2015 8:18 am Post subject: |
|
|
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 |
|
|
|