View previous topic :: View next topic |
Author |
Message |
cucuruzel
Joined: 18 Mar 2010 Posts: 53 Location: Cluj-Napoca
|
Posted: Sun Jul 10, 2011 5:07 pm Post subject: Lookdown |
|
|
Hello
I have the folowing code:
Code: |
$regfile = "m8def.dat"
$crystal = 16000000
$baud = 57600
$hwstack = 128
$swstack = 128
$framesize = 128
Dim Ti As Integer
Dim Tb1 As Byte
Tb1 = "1"
Ti = Lookdown(tb1 , Chars , 10)
Print Ti
End
Chars:
Data "0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9"
|
The result is:
3
Is this correct?
I think the correct value is 2. If I search 2 the result is 5, 3 is 7 ...
What is wrong ? |
|
Back to top |
|
|
DToolan
Joined: 14 Aug 2004 Posts: 1384 Location: Dallas / Fort Worth, Texas (USA)
|
Posted: Sun Jul 10, 2011 6:17 pm Post subject: |
|
|
The correct index of the string value "1" is 2 and the string value "2" is 4 (at least it used to be). DATA indexes had always been zero based (starting at zero). It seems the LOOKDOWN data indexes are off by 1 as though they are 1 based now. I am guessing this is not intended and is a bug. |
|
Back to top |
|
|
cucuruzel
Joined: 18 Mar 2010 Posts: 53 Location: Cluj-Napoca
|
Posted: Sun Jul 10, 2011 6:49 pm Post subject: |
|
|
This is my opinion:
If the first element must be at position 1, then LOOKDOWN should return 0 in case it does not find the searched value, not -1, which forces to use integer instead of byte (where byte is enough)
If the data is string, and lenght is 1 (single char), the data should be considered as the char's ascii code (single byte element), as it works if it is used in a condition:
Code: |
dim b as byte
b=65
if b="A" then print "true"
|
An official position would be great |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sun Jul 10, 2011 6:53 pm Post subject: |
|
|
This is no bug, Lookdown wasn't intended to be used with strings.
Code: | Data "0" , "1" , "2", ... |
each character is considered a string and this way encoded with a trailing zero,
Code: | &h30, &h00, &h31, &h00, &h32, &h00, ... |
Lookdown still can be used with single chars, but the result has to be divided by 2.
Edit:
And of course in var = LOOKDOWN( value, label, entries), entries has to be twice the actual char count. |
|
Back to top |
|
|
DToolan
Joined: 14 Aug 2004 Posts: 1384 Location: Dallas / Fort Worth, Texas (USA)
|
Posted: Sun Jul 10, 2011 7:39 pm Post subject: |
|
|
Well, here is the way it works (when working correctly). DATA in flash begins with the index 0... so if there is a first element match, the return should be 0 (not 1).
The LOOKDOWN command does not really work with string DATA blocks. For instance, you have dimensioned Tb1 as a BYTE variable. You have given Tb1 a value of "1" ( which is an ASCII "1"... or equal to &H31). The LOOKDOWN command will search from the beginning of the Chars: label for 10 steps (bytes), NOT 10 strings (which, in this case, are 2 bytes each).
Each STRING in your data set takes up 2 places. One place for it's ASCII value and one place for it's string terminator. It is laid out in FLASH like so...
Code: | Chars: &H30, &H00, &H31, &H00, &H32, &H00, &H33, &H00, &H34, &H00, &H35, &H00, &H36, &H00, &H37, &H00, &H38, &H00, &H39, &H00 |
You have told LOOKDOWN that you have 10 data elements (strings) but it will only search through 10 bytes (the size that Tb1 was dimensioned). Your data set actually takes up 20 bytes worth of space in flash (as shown above). If you search for "1", it will search for the hex value &H31. It should return the value 2 because that is it's index (it returns a 3 instead and that is a bug). If you search for anything above "4", it will return a -1 because "5" is the 11th ASCII byte and the search won't get there to find it. |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sun Jul 10, 2011 7:47 pm Post subject: |
|
|
DToolan,
yes, that's the way I wrote it.
The 1 for the first entry may be inconsistent with lookup, but the sample for lookdown shows
Code: | Print Idx ' return 3 |
what's to expect. |
|
Back to top |
|
|
DToolan
Joined: 14 Aug 2004 Posts: 1384 Location: Dallas / Fort Worth, Texas (USA)
|
Posted: Sun Jul 10, 2011 8:12 pm Post subject: |
|
|
That's a good point. I don't know why the index is inconsistent between LOOKUP and LOOKDOWN. The index should be the same either way. Either make it 0 or 1 based but not both. |
|
Back to top |
|
|
cucuruzel
Joined: 18 Mar 2010 Posts: 53 Location: Cluj-Napoca
|
Posted: Sun Jul 10, 2011 9:01 pm Post subject: |
|
|
That is what I mean, it should be 0 based, to be consisteny with LOOKUP .
From LOOKDOWN help:
"The LookDown function is the counterpart of the LookUp function."
I am writing a function to convert some text (numbers and strings) into 7-segment displays compatible output. The best way of doing it (in my opinion, for easy porting and mantainance) is to use a combination of LOOKDOWN and LOOKUP:
Declare the ascii codes that are to be supported in one DATA statement, and the corresponding 7-segment data bytes in another DATA block. Then use LOOKDOWN to search if given text (each character, in a loop) can be displayed and find the index, and use that index with LOOKUP to return the byte to be send to display.
Of course it can be done by substacting 1 form index, but that is not "the counterpart of the LookUp function".
P.S.
I know that "1" is a string, encoded with &H31 and trailing &H00, but if all "strings" in a DATA statement are sigle characters why not treat them as bytes and not double the footprint ?
It can be done like this:
Chars:
Data "0123456789" |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Sun Jul 10, 2011 9:43 pm Post subject: |
|
|
The lookdown() was the result of a customer request.
I agree it would better if they are both using the same index but i can not change it now : it would break old code.
if you want it, you can put the lookup code from mcs.lib into your own lib and mod it so it will work as you like it. _________________ Mark |
|
Back to top |
|
|
|
|
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
|
|