Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Const & IF

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR old unsupported versions
View previous topic :: View next topic  
Author Message
Alex-Dan

Bascom Member



Joined: 25 Jan 2025
Posts: 22
Location: S.Posad

russia.gif
PostPosted: Thu Nov 13, 2025 4:40 am    Post subject: Const & IF Reply with quote

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
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2361

blank.gif
PostPosted: Thu Nov 13, 2025 2:48 pm    Post subject: Re: Const & IF Reply with quote

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
View user's profile
plouf

Bascom Member



Joined: 19 Jan 2012
Posts: 111
Location: Athens,Greece

greece.gif
PostPosted: Thu Nov 13, 2025 6:18 pm    Post subject: Reply with quote

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
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2361

blank.gif
PostPosted: Thu Nov 13, 2025 8:40 pm    Post subject: Reply with quote

plouf wrote:
i dont think Alex speaking was offensive. !

I found the wording kinda odd.
Quote:
Also most other languanges support conts=const so its legimate query, for a number of reasons like cleanwriting or even temporary disabling if

Bascom supports it in combination with #IF/#ELSEIF/#ELSE/#ENDIF, the concept is to resolve non-changing logic at compile-time.
It actually works nicely.

In case this fixed logic should - because of completeness - work together as Alex requested with regular run-time code too, there is a trade-off between amount of work to put in and the resulting benefit.
Till now I noticed only an allegation it would be preferable if dealing with EEPROM data, which sounded odd.

Nonetheless I bet suggestions for real improvements will be noticed - but to this moment there was only this fuzzy allegation.
If this changes, I will be happy to cheer or trash it.

Quote:
i.e. personaly i use
if 1=1 ' Old statement commented out here
.. in order to temporary test condition to be always true etc

This won't work with Bascom as explained, however for conditional compilation it works like that:
Code:
Const true = 1
Const my_debug_flag = true
#IF my_debug_flag = true
  ' do some debug here
#ENDIF

Normally this issue addressed here is done in 5 minutes of thinking it over - even without more detailed description from the help - and then move on.
Especially if there is an usable #IF/#ENDIF option.
Quote:
if it does not in my opinion should be mentiom more detailed in the manual, helps

You can spend a whole lifetime to work on the help to specify even the smallest detail.
I prefer using brain 2.0 and this is the way it works with every programming language.
If you never ran in undocumented behaviors, you must be a lucky guy. Very Happy
Back to top
View user's profile
port

Bascom Member



Joined: 23 Sep 2025
Posts: 11

germany.gif
PostPosted: Thu Nov 13, 2025 9:29 pm    Post subject: Reply with quote

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
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6274
Location: Holland

blank.gif
PostPosted: Thu Nov 13, 2025 9:48 pm    Post subject: Re: Const & IF Reply with quote

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
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2361

blank.gif
PostPosted: Thu Nov 13, 2025 9:53 pm    Post subject: Reply with quote

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
View user's profile
port

Bascom Member



Joined: 23 Sep 2025
Posts: 11

germany.gif
PostPosted: Fri Nov 14, 2025 6:40 am    Post subject: Reply with quote

Yes, I've realized that too. Smile
Back to top
View user's profile
Alex-Dan

Bascom Member



Joined: 25 Jan 2025
Posts: 22
Location: S.Posad

russia.gif
PostPosted: Sun Nov 23, 2025 12:23 pm    Post subject: Re: Const & IF Reply with quote

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. Exclamation

You also blocked my AVR license account. This is the current license for 8051. Question
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6274
Location: Holland

blank.gif
PostPosted: Sun Nov 23, 2025 9:48 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR old unsupported versions All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can 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