Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 22:55 28 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MMbasic : no overflow underflow error ?

Author Message
vincenthimpe
Newbie

Joined: 14/10/2018
Location: United States
Posts: 24
Posted: 06:31pm 10 May 2023
Copy link to clipboard 
Print this post

it looks like there is no error on overflow.

Option default integer
Option explicit
Dim integer x=1

t: Print x
  x=x*2
  pause 100
   GoTo t

-snip-
36028797018963968
72057594037927936
144115188075855872
288230376151711744
576460752303423488
1152921504606846976
2305843009213693952
4611686018427387904
-9223372036854775808
0
0
0
0
0
0

THe program does not stop on overflow and keeps going with the variable stuck at 0 ...

There should be an error thrown for overflow/underflow that can be trapped with an "on error "
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6800
Posted: 08:35pm 10 May 2023
Copy link to clipboard 
Print this post

To be fair, that's a very unusual situation. In almost all cases a programmer will be deciding on boundaries and checking for them as part of the program. At least they should be. You should always check that there are no more green bottles standing on the wall and that you haven't loaded your six-shooter with 10000000 bullets. :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
vincenthimpe
Newbie

Joined: 14/10/2018
Location: United States
Posts: 24
Posted: 09:07pm 10 May 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  To be fair, that's a very unusual situation.

every Basic (or other programming language , including assembly) has an overflow detection mechanism. (assembly has a flag in the cpu)

The problem is that the contents first overflow negative , then lock to 0. This is unwanted behaviour from the interpreter. An error condition needs to be thrown when a mathematical overflow / underflow occurs. That way i can trap this with an "on error" clause.

Besides , it works with floats, so it should work in integers as well


Value     :     2.47182806e+307
Error No  :     0
Error msg :
--------------------------------------------
Value     :     4.943656121e+307
Error No  :     0
Error msg :
--------------------------------------------
Value     :     9.887312242e+307
Error No  :     0
Error msg :
--------------------------------------------
[6] t: x = x*2
Error : Overflow
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9131
Posted: 09:12pm 10 May 2023
Copy link to clipboard 
Print this post

  Quote  it works with floats, so it should work in integers as well


It doesn't, it won't. If this doesn't suit then don't use MMBasic
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4044
Posted: 09:50pm 10 May 2023
Copy link to clipboard 
Print this post

  Quote  it works with floats, so it should work in integers as well


Unfortunately it doesn't and is unlikely to do so in the near future unless you'd like to checkout the source code and add the feature for yourself.

MMBasic has many fine features, but it's error handling is a little weak. I dare say this is a legacy of having originally required a relatively small footprint so as to be able to run on a previous generation of microcontrollers.

I hope you will judge it on its merits and perhaps stay around and tell us about your project.

Best wishes,

Tom
Edited 2023-05-11 07:57 by thwill
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
vincenthimpe
Newbie

Joined: 14/10/2018
Location: United States
Posts: 24
Posted: 11:36pm 10 May 2023
Copy link to clipboard 
Print this post

  matherp said  
  Quote  it works with floats, so it should work in integers as well


It doesn't, it won't. If this doesn't suit then don't use MMBasic


That's a bit of a bummer.
 
zeitfest
Guru

Joined: 31/07/2019
Location: Australia
Posts: 483
Posted: 12:29am 11 May 2023
Copy link to clipboard 
Print this post

Maybe check thePOSIX  standard reporting. Most of it is applicable for larger systems of course but there are a number of conditions that small micro systems probably should manage. I think it impacts commercial insurability and so on.
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3805
Posted: 10:03am 12 May 2023
Copy link to clipboard 
Print this post

  zeitfest said  Maybe check thePOSIX  standard reporting. Most of it is applicable for larger systems of course but there are a number of conditions that small micro systems probably should manage. I think it impacts commercial insurability and so on.

Bearing in mind MMBasic is standalone (no OS), with both RAM & flash memory very limited and so on, which part(s) of POSIX do you mean?

John
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3805
Posted: 10:29am 12 May 2023
Copy link to clipboard 
Print this post

  vincenthimpe said  every Basic (or other programming language , including assembly) has an overflow detection mechanism

How do you think C does it?

Bear in mind that if MMBasic had originally been written in assembly it would likely not be on the ARM devices at all let alone so many, or on Windows & Linux...

  vincenthimpe said  Besides , it works with floats, so it should work in integers as well

It's a reasonable thing to hope, but if you choose to use INTEGERs in MMBasic they don't work that way.  So you either live with that or don't use INTEGERs.

(Or tweak the code but there's quite a bit - to make sure you find every case.)
edit: if you do change the code successfully and it's not much bigger or slower, maybe it will be included in the future

John
Edited 2023-05-12 23:58 by JohnS
 
zeitfest
Guru

Joined: 31/07/2019
Location: Australia
Posts: 483
Posted: 03:52am 13 May 2023
Copy link to clipboard 
Print this post

I think it would be good to have a small standard API for error handling in small systems.  [not that I have done much of it  - utter hypocrisy here !!    ]

The POSIX standard is mostly for much larger systems but it does suggest things like

Floating Point Exceptions
Segmentation / Memory Violations
Illegal Instructions

Usually a processor has register(s) error flags that are set by the hardware and checked, other things can be checked in programming and IDE's  usually provide functionality along those lines. Most small system applications check for possible runtime errors as well of course, I gather MMBasic has a good number of checks anyway as part of being user-friendly.

Although many small systems do not have  a formal OS managing memory and so on, there is practically always a persistent CLI running a user-prompt. It would be maybe easy enough to separate the CLI and interpreter, and set a list of standard errors that an interpreter should catch and flag, the CLI then reporting as needed.

Probably not much point unless trying programs as well as MMBasic I guess. But it would provide an identifiable assurance point.
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3805
Posted: 07:01am 13 May 2023
Copy link to clipboard 
Print this post

  zeitfest said  Floating Point Exceptions
Segmentation / Memory Violations
Illegal Instructions

You get those (if the CPU does them and the ones with MMBasic so far do).

  zeitfest said  Most small system applications check for possible runtime errors as well of course, I gather MMBasic has a good number of checks anyway as part of being user-friendly.

Yes, though not with INTEGER overflow (where you pretty much get what C gives you).

  zeitfest said  there is practically always a persistent CLI running a user-prompt.

Many small-system BASICs, including MMBasic, do not have that (in effect MMBASIC is the CLI when not running a program).

  zeitfest said  It would be maybe easy enough to separate the CLI and interpreter

Not really any point with a small-system BASIC, and inevitably you'd increase the memory overhead.  However, memory is typically a tight constraint already.

The OP was about INTEGER overflow type issues which I don't think POSIX addresses but in any case where the underlying hardware (which is in effect C here!) doesn't provide convenient fast mechanisms for detecting problems then it's tough.

If it was desired to have overflow etc detected with INTEGERs, I suppose the best (fastest) way would be to drop into assembler for such but then the code will be ugly, harder to port and maybe error-prone.

However, avoiding the issues is so easy for the user - just use non-INTEGER i.e. floating point (which is all you have anyway in most BASICs) when doing calculations which might overflow (or for all calculations).

John
Edited 2023-05-13 17:07 by JohnS
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6800
Posted: 07:21am 13 May 2023
Copy link to clipboard 
Print this post

Usually a *microprocessor* has stuff like overflow and underflow detection. These are not necessarily present on all *microcontrollers* as they are generally much simpler devices. I don't know what Geoff had to work with when he wrote MMBasic, but I'm pretty sure that all such low level error checking will have been handled by the compiler in some way - but only if there was space for it.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4044
Posted: 07:42am 13 May 2023
Copy link to clipboard 
Print this post

If you did want to change it Peter (and I'm not offering an opinion) then GCC provides these builtins:

   __builtin_add_overflow
   __builtin_sub_overflow
   __builtin_mul_overflow

Best wishes,

Tom
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
IanRogers

Senior Member

Joined: 09/12/2022
Location: United Kingdom
Posts: 151
Posted: 07:46am 13 May 2023
Copy link to clipboard 
Print this post

I think the overflow has been over estimated. An integer in the basics of binary is a whole number.  The "signed/unsigned" has always been down to  the individual programmer.

C will "warn" in compile time that you "MAY" exceed the integer size. but run time it will not.

I had to do a project "in some other basic" which didn't do negative numbers, so I had to check it myself.

In 8 bit ASM 127 + 2 = 129 but a C compiler sees -2 signed if signed was selected, this is still regarded as a real number in ASM so no flag will be set.
I'd give my left arm to be ambidextrous
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9131
Posted: 08:29am 13 May 2023
Copy link to clipboard 
Print this post

  Quote  f you did want to change it Peter (and I'm not offering an opinion) then GCC provides these builtins:


Don't work on the Pico as 64-bit ops call ROM subroutines
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4044
Posted: 09:00am 13 May 2023
Copy link to clipboard 
Print this post

  matherp said  Don't work on the Pico as 64-bit ops call ROM subroutines


Ah, ok, well I'm always surprised that anyone needs to count above 65535 anyway  .

I suppose if someone really needed then that much they could implement "safe" MATH ADD, MATH SUBTRACT and MATH MULTIPLY functions the slow way.

Best wishes,

Tom
Edited 2023-05-13 19:03 by thwill
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024