View previous topic :: View next topic |
Author |
Message |
mmarlette
Joined: 30 Nov 2007 Posts: 311 Location: Delano, MN
|
Posted: Fri Feb 25, 2011 3:58 pm Post subject: CONST error 242 |
|
|
I have looked but can't seem to locate the solution in the forums here. I thought I have covered this before in previous code but can't seem to resolve.
I have defined CONST that defines the number of bytes per entry in a look up table. When I try to do math on the CONST to calculate the offset into the table I get the error 242, Source variable does not match the target. The CONST is defined in another .bas module that is $include'd.
If I make the TableMultipler = 1 in function below, no error.
Here is a snippet of the code:
This is from the keydefinition file.bas
Code: |
Const KeyTableMultipler = 1 'define the number of byte(s) per key table entry.
'This will be used in the module Local.bas, KeyLookup
'function.
Function KeyLookup(KeyValue as byte) as byte
Local LabelADR As Word
Local TableOffset as Word
Local TableMultipler as Word
LabelADR = Loadlabel(LowerCaseTable)-1 'point to the based address
TableMultipler = KeyTableMultipler 'convert Const to word???
TableOffset = TableMultipler * KeyValue 'calculate correct offset into the keytable
LabelADR = LabelADR + TableOffset 'address of specific key
KeyLookup = Cpeek(LabelADR) 'read value and setup for return to caller
End Function
|
What I am overlooking. Seems to be a data type conversion issue. ???
TIA,
Mark |
|
Back to top |
|
|
Evert :-)
Joined: 18 Feb 2005 Posts: 2156
|
Posted: Fri Feb 25, 2011 4:38 pm Post subject: |
|
|
Hard to test, no errors here with the code provided.
But Keyvalue is dimmed as byte and Tableoffset as word, so you can try this.
Code: |
Local Tempword as word
Tempword = Keyvalue
TableOffset = TableMultipler * Tempword
|
_________________ www.evertdekker.com Bascom code vault |
|
Back to top |
|
|
mmarlette
Joined: 30 Nov 2007 Posts: 311 Location: Delano, MN
|
Posted: Fri Feb 25, 2011 6:58 pm Post subject: |
|
|
Your code example was basically what mine was but it did make point me in a direction of the problem.
I will try to explain the best I can. I don't understand the internal workings of the compiler but it has something to do with the ordering of the $include'd modules.
I come from a 2 pass compiler, this must be different.
The defined CONST KeyTableMultipler = 1 resides in the keytable.bas module.
The function KeyLookup resided in the local.bas module.
All are brought together in Main.bas. Trying to simplify here.....Main does way more than this but......
Main.bas is the module that gets the compiler invoked on.
Problem sequence within Main.bas is this:
Code: |
$include local.bas
$include keytable.bas
|
The problem appears that the compiler doesn't know what the value of the CONST is yet as it hasn't included it yet.
The solved sequence within Main.bas is this:
Code: |
$include keytable.bas
$include local.bas
|
Now the function knows the CONST in keytable.bas.
Can someone give me the correct terminology/compile process of what the compiler is doing in relationship to my example?
I see what it is doing, makes sense, but I didn't see it till now.
Thanks Evert for the shove in the right direction. |
|
Back to top |
|
|
mattcro
Joined: 03 Oct 2007 Posts: 327 Location: Scotland
|
Posted: Fri Feb 25, 2011 9:42 pm Post subject: |
|
|
The Bascom compiler seems to simply insert the included file contents at the location of the $include statement, so it forms a single long linear lump of code (monolithic?), then compiles that to binary output.
$include is basically a way of moving a chunk of code out of your main source file, rather than modularizing the code.
There doesn't seem to be any linking stage like for modular C code compilation - and that means that the order and position of your $includes matters a lot.
I am guessing at some of this though, so don't take it as definitive! _________________ If all else fails, read the manual. Even better: read the manual before something fails. If you can't find it in the manual, search the forum.
BascomAVR 2.0.8.5 |
|
Back to top |
|
|
JohnCannon
Joined: 04 Jan 2006 Posts: 162
|
Posted: Sat Feb 26, 2011 12:11 am Post subject: |
|
|
Hi,
Didn't study your problem but did recently experience const located AFTER it was needed by compiler and caused error. Noted in HELP that CONST must be located ahead of where it's needed.
JC |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Sat Feb 26, 2011 1:48 am Post subject: |
|
|
My experience is the same as mattcro.
Including a file simply concatenates it into the main code at the insertion point. Constants must be declared before they are used.
Similar things apply to Declares and their associated Subs and Functions. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
|