How to use LOOKUP with a DAT segment list?
Larry Martin
Posts: 101
I am trying to use Lookup to verify that an incoming opcode is in the list of allowable opcodes, and am not getting the right syntax to make it work.
Here is my code (bear with me on indentation):
DAT
_HostOpcodes byte "QOVRrMmSsTtPpLKG", 0
OBJ
Host : "FullDuplexSerial"
PUB HPS_Opcode(p_byte) | ok
ok := lookup(p_byte: _HostOpcodes)
Host.str(string(13,10,"HPS_Opcode: "))
Host.hex(p_byte, 2)
Host.tx($20)
Host.dec(ok)
DumpMemoryToHost(@_HostOpcodes, 16)
if lookup(p_byte: _HostOpcodes) > 0
RESULT := TRUE
elseif lookup(CharToUpper(p_byte): _hex) > 0
RESULT := TRUE
else
RESULT := FALSE
And here is the terminal output from entering a 'V', which is third in the list:
HostProcessSerial: 56 0
HPS_Opcode: 56 0
00000124 51 4F 56 52 72 4D 6D 53 73 54 74 50 70 4C 4B 47
Note that lookup(p_byte: _HostOpcodes) returned 0, as do:
lookup(p_byte: @_HostOpcodes)
lookup(p_byte: $51, $4F, $56 )
The last one looks like the book examples, but still doesn't work.
Can anyone spot my mistake?
Thanks,
Larry
Here is my code (bear with me on indentation):
DAT
_HostOpcodes byte "QOVRrMmSsTtPpLKG", 0
OBJ
Host : "FullDuplexSerial"
PUB HPS_Opcode(p_byte) | ok
ok := lookup(p_byte: _HostOpcodes)
Host.str(string(13,10,"HPS_Opcode: "))
Host.hex(p_byte, 2)
Host.tx($20)
Host.dec(ok)
DumpMemoryToHost(@_HostOpcodes, 16)
if lookup(p_byte: _HostOpcodes) > 0
RESULT := TRUE
elseif lookup(CharToUpper(p_byte): _hex) > 0
RESULT := TRUE
else
RESULT := FALSE
And here is the terminal output from entering a 'V', which is third in the list:
HostProcessSerial: 56 0
HPS_Opcode: 56 0
00000124 51 4F 56 52 72 4D 6D 53 73 54 74 50 70 4C 4B 47
Note that lookup(p_byte: _HostOpcodes) returned 0, as do:
lookup(p_byte: @_HostOpcodes)
lookup(p_byte: $51, $4F, $56 )
The last one looks like the book examples, but still doesn't work.
Can anyone spot my mistake?
Thanks,
Larry
Comments
Why "lookup(p_byte: $51, $4F, $56 )" doesn't work I'd put down to p_byte not having the value of any of the numbers in that lookup ( although it's not clear where you put that test lookup in your code ). You could change "Host.hex(p_byte, 2)" to "Host.hex(p_byte, 8)" to ensure there aren't any upper bits being set in p_byte confusing things.
PS : add [noparse][[/noparse] code ] and [noparse][[/noparse] /code ] ( no spaces ) around code to show your indentation.
I ended up just writing a function that does the lookup, so I'm good for now.
Is it possible to see the PASM implementation of LOOKUP? That might help me understand the semantics.
Thanks,
Larry
PS Thanks for the [noparse][[/noparse]code] tip
Source: http://forums.parallax.com/showthread.php?p=711386
As guessed, that generates a one item lookup. It should match the first entry ($51/"Q") but not any others.
This program, which exercises all the permutations suggested today (thanks, guys):
creates this output:
00000056 0 0 0 0 0
00000056 0 0 0 0 0
Since I am using LOOKUP, not lookupZ, that means it's all errors, right?
Same thing happens with Propeller Tool and Propellent (both v1.1)
I'm going to try and understand the PASM now, and maybe learn something about the system. Thanks to Hippy and Mike Green. Will post anything I learn.
Lookup gets the value from the list at the given index. I was trying to get the index of the given value.
Guess I needed a function after all.
Hippy, Mike, thanks for your help, sorry for wasting time.
Larry
Output:
00000056 1 2 0 1 3 0
LookDOWN does what I need when the list is included in the function call, but I do this in several places and don't want to encode the list in each function call. I'd really like to have the list centralized, and use it with a pointer (last form).
The C analogy would be "if (strchr(_HostOpcodes, p_byte)){do_stuff();}"
Can that be done with Lookdown?