| View previous topic :: View next topic |
| Author |
Message |
Alex-Dan
Joined: 25 Jan 2025 Posts: 22 Location: S.Posad

|
Posted: Thu Nov 13, 2025 4:40 am Post subject: Const & IF |
|
|
hi!
A colleague from the forum writes:
| Code: | $regfile = "m328pdef.dat" ' ATmega328P.
$crystal = 16000000 ' 16 MHz.
$hwstack = 64
$swstack = 64
$framesize = 64
const TestConst = 0
if TestConst = 0 Then
endif |
Error : 93 Line : 9 Variable not dimensioned [0]
I couldn't find any restrictions on this kind of code in the help for the if and const keywords.
I don't recommend using #if , otherwise you'll have to rewrite a lot of code, and if you need to return to reading data from the EEPROM later, you'll have to rewrite it again.
What's wrong with Bascom?
As it turns out, this code also doesn't compile.
| Code: | if 0 = 0 Then
endif |
(BASCOM-AVR version : 2.0.8.6 , Latest : 2.0.8.7 ) |
|
| Back to top |
|
 |
MWS
Joined: 22 Aug 2009 Posts: 2361

|
Posted: Thu Nov 13, 2025 2:48 pm Post subject: Re: Const & IF |
|
|
| Alex-Dan wrote: | | What's wrong with Bascom? | Nothing.
It does not match your taste, but things don't necessarily do.
Comparing const with const never worked.
I'm wondering - if it would work - where you see a benefit.
Code which is fixed at compile-time - and can't be altered in run-time - can be handled with #IF/'#ENDIF
| Quote: | | I don't recommend using #if , otherwise you'll have to rewrite a lot of code, and if you need to return to reading data from the EEPROM later, you'll have to rewrite it again. | What has it all the sudden to do with EEPROM-data?
EEPROM is variable data, related code being able to branch differently at run-time, your construct is always executed exact the same at run-time.
Would you mind to create a short example, where you show the usefulness of const-const vs. #IF/#'ENDIF? |
|
| Back to top |
|
 |
plouf
Joined: 19 Jan 2012 Posts: 111 Location: Athens,Greece

|
Posted: Thu Nov 13, 2025 6:18 pm Post subject: |
|
|
i dont think Alex speaking was offensive. !
Also most other languanges support conts=const so its legimate query, for a number of reasons like cleanwriting or even temporary disabling if
i.e. personaly i use
if 1=1 ' Old statement commented out here
.. in order to temporary test condition to be always true etc
if it does not in my opinion should be mentiom more detailed in the manual, helps _________________ Christos |
|
| Back to top |
|
 |
MWS
Joined: 22 Aug 2009 Posts: 2361

|
|
| Back to top |
|
 |
port
Joined: 23 Sep 2025 Posts: 11

|
Posted: Thu Nov 13, 2025 9:29 pm Post subject: |
|
|
The reason for this is certainly the way in which values are compared (or others) in the AVR.
Two variables are loaded into registers and subtracted, for example.
The status register then contains evaluable flags (positive, negative, overflow ...).
The results of this control the next branches.
Constants, as the name suggests, are not variable.
However, at least one variable value is needed for the subtraction.
For subtraction in AVR RISC processors, the SUB r1,r2 instruction is used,
which subtracts the contents of register r2 from the contents of register r1
and stores the result in r1 (example).
A small addition will help here.
| Code: |
$regfile = "m328pdef.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 64
$framesize = 64
const TestConst = 4
dim a as byte
dim b as byte
a = TestConst
b = 0
if a < 5 Then b = 1
end
|
|
|
| Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6274 Location: Holland

|
Posted: Thu Nov 13, 2025 9:48 pm Post subject: Re: Const & IF |
|
|
| Alex-Dan wrote: |
A colleague from the forum writes:
|
He should post himself. This forum is for registered users. When you relay things i can not check if he has a license or a cracked version.
Nothing wrong when he has a license and does not master the language and you help him but i can not tell. So you should contact support in advance before you do this.
I do not see any benefit for testing a constant in code. And that is the reason it does not work. In 25 years this is the first time there is some need for this.
I like to see an actual example where this is required.
Like mentioned before, #IF is intended for this. So send me some example that shows why this is needed and why #IF does not do its job.
And code like if 1=1 can be better written in a DO LOOP. _________________ Mark |
|
| Back to top |
|
 |
MWS
Joined: 22 Aug 2009 Posts: 2361

|
Posted: Thu Nov 13, 2025 9:53 pm Post subject: |
|
|
| port wrote: | | The reason for this is certainly the way in which values are compared (or others) in the AVR. |
If you've aimed for the eye, you rather hit the tail. |
|
| Back to top |
|
 |
port
Joined: 23 Sep 2025 Posts: 11

|
Posted: Fri Nov 14, 2025 6:40 am Post subject: |
|
|
Yes, I've realized that too.  |
|
| Back to top |
|
 |
Alex-Dan
Joined: 25 Jan 2025 Posts: 22 Location: S.Posad

|
Posted: Sun Nov 23, 2025 12:23 pm Post subject: Re: Const & IF |
|
|
| albertsm wrote: | | Alex-Dan wrote: |
A colleague from the forum writes:
|
He should post himself. This forum is for registered users. |
You're blocking his country, and he can't post here. Although he's had the license for a very long time, probably longer than all my colleagues.
You also blocked my AVR license account. This is the current license for 8051.  |
|
| Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6274 Location: Holland

|
Posted: Sun Nov 23, 2025 9:48 pm Post subject: |
|
|
There is a good reason some countries are blocked. And the same for individual users. The only solution is that the user contact support. But what you did is not right and can result in a ban. If it is not clear just ask support. The reason your other account was blocked is simple : i do not tolerate insults. And when the legal support time is over all support ends in such a case. Do not forget that the extended free support is paid by me and not some right. _________________ Mark |
|
| Back to top |
|
 |
|