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 : Integer overflow into sign bit
Author | Message | ||||
flip Senior Member Joined: 18/07/2016 Location: AustraliaPosts: 114 |
Hi all! I've been using Maximite and Micromite for some years, and having as much fun as when I started playing with DEC Basic with 4k magnetic core using time-share using big freaky old 110 baud dot-matrix printing terminals back in 1975 doing computer studies in year 11... But I know I am just a pup...there are people on this forum whose experience goes back a lot further This is to note that there seems to be a problem with integer overflow/bounds checking... (I thought I recalled previous discussion but searched and could not find any previous post on subject) It happened to me with 2 different MX470 (both are at 5.03.02)...(all following done from command prompt) > x%=2 > for i%=1 to 20:x%=x%*i%:next i% > ? x% 4865804016353280000 > x%=x%*2 > ? x% -8715136041002991616 > This behaviour is likely to be correct and to be expected if we understand how the bits work in machine code, and a result of carry-over of the most significant bit into the sign bit. And of course it should always be the programmer's responsibility to ensure our variables don't get out of hand. If it can be fixed (generate an error) it would be great but maybe not at the expense of performance or huge chunks of checking code in the firmware? But that's entirely up to Geoff of course. Thanks all for your great contributions and helping keeping my brain ticking over as I head towards old age... Regards, Phil |
||||
Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3165 |
Yes, this is something I have bee thinking about. Languages like C do no checking but a more programmer friendly language like BASIC should raise a warning. The problem is, as you noted, the overhead in checking each operation. Given the very large range of integers it is not often that you get overflows in real life programs. So, do you want to slow down all programs to check for a rare event? This needs to be benchmarked. This could get out of hand but my first computer was a wire wrapped board with an Intel 8080 and 256 bytes of RAM. Code had to be hand assembled. Geoff Geoff Graham - http://geoffg.net |
||||
twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1137 |
Maybe this could be done with a OPTION CHECKINTEGEROVERFLOW or something like that. Then it's up to the user to accept the deceleration. BTW. Windows 7 behaves in the same way : 4865804016353280000 x 2 gives: Regards Michael |
||||
robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2292 |
how about just having an overflow flag, such as MM.INTOVERFLOW that is set to 1 whenever an integer operation overflows? is a 64-bit integer overflow not currently reflected in a processor flag? personally, i'm quite used to the concept of integers wrapping around instead of overflowing, and would likely find it more of a nuisance if an error was raised. cheers, rob :-) |
||||
robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2292 |
SNAP! yor wrote your reply as i was writing mine rob :-) |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3675 |
If MMBasic is using C then it can be a bit awkward to do, even though the CPU may (probably has) set an overflow flag. Is this really a problem? Or is it perceived by some to be a problem when really it isn't? Next: how about shift left? Multiplication? (I'm not going near division.) John (whose first computer was before even the 4004 never mind 8008) |
||||
Paul_L Guru Joined: 03/03/2016 Location: United StatesPosts: 769 |
You're right Geoff, this could get out of hand, so I'll end it quickly. My first computer was an IBM 701. Can anyone top my geezer status? Paul_L |
||||
IanT Regular Member Joined: 29/11/2016 Location: United KingdomPosts: 84 |
PDP 9 and "Straight" PDP 8 - fairly early DEC Minis - don't know how they compare to IBM products though... Regards, IanT |
||||
flip Senior Member Joined: 18/07/2016 Location: AustraliaPosts: 114 |
Yes PDP8 that rings a bell! but definitely early IBMs and 4004 4-bitter were before me...I feel in sage company! Back on integer overflows, and and after testing in detail, I agree totally and personally think it is a thing of beauty as it is - shifting,xor, multiplication,\,MOD all work exactly the way digital logic in the early 8-bit micros do (albeit with 64 bits), so I support the view that it shouldn't crash out or be altered. There are many routines that would find existing behaviour useful. I am working on a hash table algorithm that I suspect should actually work faster because of this feature (de-facto mod 2^64 function is performed during all operations - if we think of it as 64-bit unsigned number) Maybe all that's required is a simple warning in the doco that care needs to be taken when operating integers around their limits, but point out it has been designed to allow fully predictable bitwise behaviour...maybe refer to some good digital logic article? At the very least this thread may help somebody in future. Regards, Phil |
||||
Print this page |