Shift or Rotate from Dragon BASIC

Hardware Hacking, Programming and Game Solutions/Cheats
User avatar
JeeK
Posts: 67
Joined: Fri Aug 16, 2013 10:45 am
Location: Vienna, Austria
Contact:

Re: Shift or Rotate from Dragon BASIC

Post by JeeK »

Rolo wrote:Ahhh, now I see the point. As a Commodore user I only would use the "%"-suffix to define an integer variable (like A%=5). I just looked it up in the booklet ("An Introductiuon to Basic..."). Dragon Basic obviously does not have that special data type. I haven't had realized that until now. Strange, why did they leave that out?
CBM Basic has no integer arithmetics. The (one and only) advantage of integers appears in form of an integer array (where one element takes only 2 bytes instead of 5). Normal integer variables allocate the same space as floats do. Every evaluation of an expressions involving integers is done by converting to float, do the calculation and converting back (just to store the integer value). Obviously, the usage of integer variables is more time consuming in every situation. Never heard of a situation where not. No need for Dragon Basic to implement this kind of crappy integers in exchange to any other Basic feature it currently has. Real integer variables with efficient arithmetics probably would cost to much ROM space ...

Beside this, shifting and rotating using multiplication and division is done with floats regardless of the variable type. No need to use integer variables (at least in case of CBM Basic). Some saving (in time) can be achieved with an assignment to an integer variable which makes an explicit INT() obsolete, but only in rare cases.
The dragon on my side: http://klasek.at/hc/dragon/
User avatar
robcfg
Posts: 1532
Joined: Sat Apr 04, 2009 10:16 pm
Location: Stockholm, Sweden
Contact:

Re: Shift or Rotate from Dragon BASIC

Post by robcfg »

I have to disagree here.

I don't know what kind of reasoning is behind not having integer arithmetics on basic, but from the processor point if view (6809, not modern ones), everything is integer values.

The 6809 has shift opcodes and such so you don't need complex routines. Maybe they used all rom space for emulating float arithmetics...

In any case, integer arithmetics is always faster than floating point arithmetics, on processors without hardware floating point support, for the simple reason that floating point values have to be encoded and packed every time they are used.

The funniest thing, is that mantissa and exponent are integer values, and they are processed by integer arithmetics, and then coded back into whatever format the floating point value is represented...
Rink
Posts: 236
Joined: Mon Sep 05, 2011 7:01 pm

Re: Shift or Rotate from Dragon BASIC

Post by Rink »

Well, it's a damn nuisance not having it (especially with no shifts). :)
User avatar
JeeK
Posts: 67
Joined: Fri Aug 16, 2013 10:45 am
Location: Vienna, Austria
Contact:

Re: Shift or Rotate from Dragon BASIC

Post by JeeK »

Rink wrote:Well, it's a damn nuisance not having it (especially with no shifts). :)
Imagine to code a CRC checksum algorithm in BASIC (expect using tables) which needs a lot shifting and xor-ing.
Just horrible to implement with MS-based Basic dialects (I've done this with CBM Basic just to proof how complicate things are which are just easy done in assembler - listing available on request). ;)
The dragon on my side: http://klasek.at/hc/dragon/
User avatar
JeeK
Posts: 67
Joined: Fri Aug 16, 2013 10:45 am
Location: Vienna, Austria
Contact:

Re: Shift or Rotate from Dragon BASIC

Post by JeeK »

robcfg wrote:I have to disagree here.

I don't know what kind of reasoning is behind not having integer arithmetics on basic, but from the processor point if view (6809, not modern ones), everything is integer values.
I agree and just to note, the first MS Basic interpreters did'nt have floats at all. Just plain integer. Easy to implement (even for larger integers than 16 bit).
robcfg wrote: The 6809 has shift opcodes and such so you don't need complex routines. Maybe they used all rom space for emulating float arithmetics...
Sure, the complexity comes with the *handling* of integer in expression evaluation, implicit casting types back an forward. The plain operations are easy an well supported, especially on a 6809 (that's why a I like Forth that much).
Additionally, 6502 code is not that space efficient compared to a 6809 (the simple V2 Basic
on a C64 takes at least 9 Kbyte in ROM). I think the decision to reduce everything to floats and omit real integer arithmetics was mandatory.
robcfg wrote: In any case, integer arithmetics is always faster than floating point arithmetics, on processors without hardware floating point support, for the simple reason that floating point values have to be encoded and packed every time they are used.
Not quite correct. Floating point values are not "packed". They are stored in variable nearly ready to use. Any basic operation (add, subtract, multiply, divide) are actually integer operations, extended to cope with 4 bytes (mantissa) an 1 byte (exponent). Time consuming is the handling of the normalized representation which takes some extra shifting in case of add- or subtract operations (an more complex operations based on these). In most cases it may roughly comparable to calculate with 5-byte wide integers.
robcfg wrote: The funniest thing, is that mantissa and exponent are integer values, and they are processed by integer arithmetics, and then coded back into whatever format the floating point value is represented...
Right, but the representation is not the real problem. More time is lost in translating constants from the basic text
into whatever representation (integer or float) is needed. It would be nice if a value of an integer variable could be
used in logical and arithmetic operations directly (without going trough a float pseudo-ALU). In case of Dragon basic you have to imagine to add every case in handling integer expressions and add variables of integer type. Even the processor is very efficient in doing this, I don't thing there is much room to add this to the current ROM. I doubt even a "minor solution" lik the "integer" implementation CBM Basic has, is not worth to be included into Dragon Basic. IMO the right decision.
The dragon on my side: http://klasek.at/hc/dragon/
User avatar
robcfg
Posts: 1532
Joined: Sat Apr 04, 2009 10:16 pm
Location: Stockholm, Sweden
Contact:

Re: Shift or Rotate from Dragon BASIC

Post by robcfg »

Not quite correct. Floating point values are not "packed". They are stored in variable nearly ready to use. Any basic operation (add, subtract, multiply, divide) are actually integer operations, extended to cope with 4 bytes (mantissa) an 1 byte (exponent). Time consuming is the handling of the normalized representation which takes some extra shifting in case of add- or subtract operations (an more complex operations based on these). In most cases it may roughly comparable to calculate with 5-byte wide integers.
You're plain wrong here. Integers do not need any shift to be stored in memory, nor for any operations, as the 6809 has opcodes to do the math. What the 6809 doesn't natively have, and what it has to be done by the Basic interpreter, is opcodes to handle floating point numbers.

If you read something on Floating Point Arithmetics, like the IEEE754 standard for floating point number representation, you'll see that sign, mantissa and exponent values, are packed into a number of bytes (unlike your example which uses 4 bytes for the mantissa, none for the sign, wastes 4 bits and uses 1 full byte for the exponent).

Now, as the 6809 only handles integer arithmetics by itself, the Basic interpreter has to separate the three values, operate on them and pack it back, which is obviously slower than just doing any operation between integers.
Right, but the representation is not the real problem. More time is lost in translating constants from the basic text
into whatever representation (integer or float) is needed. It would be nice if a value of an integer variable could be
used in logical and arithmetic operations directly (without going trough a float pseudo-ALU). In case of Dragon basic you have to imagine to add every case in handling integer expressions and add variables of integer type. Even the processor is very efficient in doing this, I don't thing there is much room to add this to the current ROM. I doubt even a "minor solution" lik the "integer" implementation CBM Basic has, is not worth to be included into Dragon Basic. IMO the right decision.
As I explained above, representation IS a problem. Integer arithmetics are done by the processor, while Floating point arithmetics requires a small program, which is stored in inside the Basic itself.

The problem of converting the text on screen to actual data manageable by the processor is both a problem of integers and floats, and it has no solution unless you write your code in assembler or you don't need any input from the user.

The reason not to include integer shifts and other operations is a design decision, not related to integer arithmetic performance as it's handled by the processor natively.
User avatar
JeeK
Posts: 67
Joined: Fri Aug 16, 2013 10:45 am
Location: Vienna, Austria
Contact:

Re: Shift or Rotate from Dragon BASIC

Post by JeeK »

robcfg wrote: You're plain wrong here. Integers do not need any shift to be stored in memory, nor for any operations, as the 6809 has opcodes to do the math. What the 6809 doesn't natively have, and what it has to be done by the Basic interpreter, is opcodes to handle floating point numbers.
I would judge that hard. Maybe I am not able to express myself that exact. What a want to say is that's this packing is nothing special, it's only one of several possibilities of an compound representation of a value which is not a simple byte or 16 bit word. Just because 6809 has some 16 bit operation other operations like logical operations, shifts and other more complex arithmetics has to be done bytewise (in same cases wordwise too) and has to be handled like a compound datatype.
To give an example that a representation may have also its advantage: The ABS() function on floats is in MS interpreters only the test and flip of a single bit, where integers needs a twos-complement (at least 2 bytewise NEG operations with carry handling in 6809). :o
robcfg wrote: If you read something on Floating Point Arithmetics, like the IEEE754 standard for floating point number representation, you'll see that sign, mantissa and exponent values, are packed into a number of bytes (unlike your example which uses 4 bytes for the mantissa, none for the sign, wastes 4 bits and uses 1 full byte for the exponent).

Now, as the 6809 only handles integer arithmetics by itself, the Basic interpreter has to separate the three values, operate on them and pack it back, which is obviously slower than just doing any operation between integers.
I know and read IEEE754 and do not doubt anything what you have stated. Just want to point to fact, that even you say integer, you obviously meant 16-bit integers which fits into 6809 registers. But as soon we talk about greater integers (e.g. 32-bit, which are generally of greater use, see the BBC Basic implementation), we also have to serializing processing of bytes or words packed in a "structure". Agreed, floats are much complexer than compound integers, but everything can be reduced to integer operations and takes more or less time ... ;)
robcfg wrote: As I explained above, representation IS a problem. Integer arithmetics are done by the processor, while Floating point arithmetics requires a small program, which is stored in inside the Basic itself.
Yes, you right in this aspect too. But this is limited to 16 bit integers (and just for some basic operations, think about logical operations, division, multiplication, ... all not natively covered by the processor).
robcfg wrote: The problem of converting the text on screen to actual data manageable by the processor is both a problem of integers and floats, and it has no solution unless you write your code in assembler or you don't need any input from the user.

The reason not to include integer shifts and other operations is a design decision, not related to integer arithmetic performance as it's handled by the processor natively.
Yes, but I wasn't able to focus my point. Just because 6809 has some basic 16 bit operations, it is not said that it you get native integers into the interpreter just for nothing. As explained, one have to add variable handling and type casting in expressions too. So the "design decision" in Dragon basic was (in my eyes) just a decision of space in ROM. No doubt, integers, especially of type 16 bit would be fast and a real "nice to have", but they are not necessary to get a competitive Basic interpreter for those days. And the pointer on how costly are text to number representation is just a hint that even you have fast integers, the interpreter may lost far much time in converting constants (in average programming style). I want give some notion that a Basic coder have to take the interpreters properties into account, with or without fast integers.

I did'nt want to dispute on this, I just want to give some other points of view. I don't say you are wrong. Please have patience if I'm still wrong in expressing my opinion.
The dragon on my side: http://klasek.at/hc/dragon/
User avatar
robcfg
Posts: 1532
Joined: Sat Apr 04, 2009 10:16 pm
Location: Stockholm, Sweden
Contact:

Re: Shift or Rotate from Dragon BASIC

Post by robcfg »

I think we are going nowhere...

The 6809 has integer arithmetics with 16 bit integers as all pointers are 16 bit indeed.

I also found this on the Fractint site:
Limitations of Integer Math (And How We Cope)
By default, Fractint uses 16-bit and/or 32-bit integer math to generate nearly all its fractal types. The advantage of integer math is speed: this is by far the fastest such plotter that we have ever seen on any PC. The disadvantage is an accuracy limit.
Now, if Microsoft decided that integer math was not for everyone and that emulating floating point was more user-friendly, well, as I told you it's a design decision, but has nothing to do with the ability of the machine to use integers.

Let's move on to more interesting topics!
User avatar
JeeK
Posts: 67
Joined: Fri Aug 16, 2013 10:45 am
Location: Vienna, Austria
Contact:

Re: Shift or Rotate from Dragon BASIC

Post by JeeK »

robcfg wrote:I think we are going nowhere...
We do not have to go to a specific point. Let the reader decide where they want to go and let them build their own
picture we are spotting from different angles. In case everyone is addicted in black-white coloring, your statement may fit. ;)
robcfg wrote: The 6809 has integer arithmetics with 16 bit integers as all pointers are 16 bit indeed.
Yes, but what does this predict?
Truly, a 6809 has quite a limited 16 bit integer capability. Just 16 bit add/subtract is not that what a would call "integer arithmetics". I love this CPU (for many other reasons) and I don't want to insult the 6809 programming community but what I call real integer arithmetics is something like the M68K family has ...
Anyway, this has nothing to do with the fact - your pointed this out clearly - that integer (or fix point) arithmetics are much faster than floating point arith. even on comparable bit resolution.
robcfg wrote:
I also found this on the Fractint site:
Limitations of Integer Math (And How We Cope)
By default, Fractint uses 16-bit and/or 32-bit integer math to generate nearly all its fractal types. The advantage of integer math is speed: this is by far the fastest such plotter that we have ever seen on any PC. The disadvantage is an accuracy limit.
Now, if Microsoft decided that integer math was not for everyone and that emulating floating point was more user-friendly, well, as I told you it's a design decision, but has nothing to do with the ability of the machine to use integers.
Yes, nothing to say against this. My point is (let me try it one final time) that even this is a design decision, not all decisions especially in the 8 bit field fall under "design". Many more stringent conditions like code space and execution time determine decisions too. Someones beautiful design isn't worth anything if it does not fit into a ROM ... ;) You said it too, "more user-friendly" - hit it! You can't sell a Basic interpreter without floats (especially if BASIC should serve the purpose its name was given for), but floats + integers however cost additional space in ROM (for different reason). It doesn't matter how good a CPU is able to handle integers...
robcfg wrote: Let's move on to more interesting topics!
Fine, I agree again. Most of the readers surely got all the aspects of this topic. No one has to be convinced ... Let's move on! :D
The dragon on my side: http://klasek.at/hc/dragon/
Post Reply