How do coders handle 16-bit numbers? [message #160776] |
Sat, 18 March 2006 22:25 |
David Murray
Messages: 1017 Registered: January 2005
Karma: 0
|
Senior Member |
|
|
I am sort of at a loss here. I'm the one that asked about Sweet16 a
few posts ago. But that option is looking very unlikely after much
research. Or to make it work would be more trouble than its worth.
So, how do most coders for the 6502 handle lots of 16-bit numbers? In
the past I've only had a few areas that needed numbers higher than 256
so I would just make a routine that would handle it. But it takes a
lot of custom code. What I need to do is add and subtract, multiply,
and divide 16-bit numbers. Are there any usefull open-source
subroutines around that handle these well? I'd appriciate any advice.
Are there any easy ways to use the built-in Kernal / BASIC routines for
this?
You know, Sweet16 is really cool, I'd love to make it work. For those
that are not familiar with it, It is an interperetor that basicaly
runs in 6502 code but emulates a 16-bit CPU. It is supposed to run 10
times slower than standard 6502 code. But, if you are dealing with a
lot of 16-bit numbers, it might not affect performance that much. It
would be a lot faster than BASIC, but not limit your numbers to 255.
I'm still going to keep in mind possibly trying to impliment it. I
could use the zero-page area that BASIC uses, if I'm not going to use
BASIC at all.
|
|
|
Re: How do coders handle 16-bit numbers? [message #160778 is a reply to message #160776] |
Sat, 18 March 2006 22:51 |
Leif Bloomquist
Messages: 1065 Registered: January 2012
Karma: 0
|
Senior Member |
|
|
One approach I've seen is macros used to handle A+X as a sort of
super-register with 16-bits, i.e.
ldax #$1234
(would put #$34 in A, and #$12 in X, etc.)
stax $d020
(would put the contents of A in $d020, and the contents of X in $d021)
I think MagerValp's IP65 code uses this approach if you want an example. I
don't think it gets into any math though.
|
|
|
Re: How do coders handle 16-bit numbers? [message #160783 is a reply to message #160776] |
Sun, 19 March 2006 00:02 |
Craig Taylor
Messages: 177 Registered: June 2005
Karma: 0
|
Senior Member |
|
|
To add 16 bit numbers
lda #>num1
clc
adc #>num2
sta #>result
lda #<num1
adc #<num2
sta #<num2
To subtract
lda #>num1
sec
sbc #>num2
sta #>result
lda #<num1
sbc #<num2
sta #<result
Multiply and divide I'll have to follow up with later as the 6502
doesn't have any direct multiply and divide routines. The common method
is to do a add and shift routine. I'll try and post a follow up tomorrow
with the details.
A quick google reference of similair code is here:
(http://www.atariarchives.org/roots/chapter_10.php) - it's for the Atari
but should still apply.
- Craig Taylor
David Murray wrote:
> I am sort of at a loss here. I'm the one that asked about Sweet16 a
> few posts ago. But that option is looking very unlikely after much
> research. Or to make it work would be more trouble than its worth.
>
> So, how do most coders for the 6502 handle lots of 16-bit numbers? In
> the past I've only had a few areas that needed numbers higher than 256
> so I would just make a routine that would handle it. But it takes a
> lot of custom code. What I need to do is add and subtract, multiply,
> and divide 16-bit numbers. Are there any usefull open-source
> subroutines around that handle these well? I'd appriciate any advice.
> Are there any easy ways to use the built-in Kernal / BASIC routines for
> this?
>
> You know, Sweet16 is really cool, I'd love to make it work. For those
> that are not familiar with it, It is an interperetor that basicaly
> runs in 6502 code but emulates a 16-bit CPU. It is supposed to run 10
> times slower than standard 6502 code. But, if you are dealing with a
> lot of 16-bit numbers, it might not affect performance that much. It
> would be a lot faster than BASIC, but not limit your numbers to 255.
> I'm still going to keep in mind possibly trying to impliment it. I
> could use the zero-page area that BASIC uses, if I'm not going to use
> BASIC at all.
>
--
============================================================ ==============
C Taylor ctalkobt@gmail.com
http://www.ctalkobt.net - Random mumblings of a disturbed mind
============================================================ ==============
|
|
|
Re: How do coders handle 16-bit numbers? [message #160786 is a reply to message #160776] |
Sun, 19 March 2006 00:19 |
jimbo
Messages: 72 Registered: July 2003
Karma: 0
|
Member |
|
|
On 18 Mar 2006 19:25:14 -0800, "David Murray" <adric22@yahoo.com>
wrote:
> What I need to do is add and subtract, multiply,
> and divide 16-bit numbers.
Take a look at the online copy of the July 1987 Transactor ... V8 N01
( in particular, the article "High-speed Multiplies and Divides -- ML
Math Routines Explained"
Go to Craig Bruce's ( www.csbruce.com/~csbruce ) Go to his CBM link
then Transactor archive.
Note that there are specific routines that can be performed for
constant-multpliers.
Multiplies by two are easily done by shift-left operations.
To multiply by 10:
Assume you have to work variables A and B and you want to multiply
A by 10.
Copy A to B
Shift A left 3 bits ( A = A * 2 * 2 * 2 ) or ( A = A * 8 )
Shift B left 1 bit ( B = B * 2 )
Add B to A
....etc.
Smaller multiplies for constant multipliers might be better done by
successive addition ( inlined, without a loop )
Jim Lawless
|
|
|
|
Re: How do coders handle 16-bit numbers? [message #160801 is a reply to message #160783] |
Sun, 19 March 2006 08:57 |
Jim Butterfield
Messages: 32 Registered: January 2005
Karma: 0
|
Member |
|
|
David Murray wrote:
> I am sort of at a loss here. I'm the one that asked about Sweet16 a
> few posts ago. But that option is looking very unlikely after much
> research. Or to make it work would be more trouble than its worth.
Missed your earlier post. As you likely know, Sweet 16 was originally
published in Doctor Dobbs Journal (when it was a non-copyright vehicle
for public domain dissemination), and was used in the early Apple ][
computers as part of their "integer Basic". Apple subsequently
switched to Microsoft Basic; some users regretted the loss of speed
(due to floating point usage in MS Basic)
> So, how do most coders for the 6502 handle lots of 16-bit numbers? In
> the past I've only had a few areas that needed numbers higher than 256
> so I would just make a routine that would handle it. But it takes a
> lot of custom code. What I need to do is add and subtract, multiply,
> and divide 16-bit numbers. Are there any usefull open-source
> subroutines around that handle these well? I'd appriciate any advice.
You've received some good replies already on this thread. It's been
pointed out that you can tighten up code for specific operations (say,
multiplying by ten, dividing by four); general routines for
multiply/divide have to be a little less efficient, of course.
Two items: Signed numbers need to be handled slightly differently
from unisigned ones, especially with multiplation/division. And watch
carefully for the possibility of overflow and divide-by-zero,
depending on the application.
In the long-ago KIM-I days, I ran across an elegant want to convert
from binary to decimal, by the way (use of decimal mode). If you need
this and nobody else posts it, holler for help here.
> Are there any easy ways to use the built-in Kernal / BASIC routines for
> this?
There are ways, but I'm not sure they are easy or recommended. Basic
does most of its work with floating point (even with integer
variables), and while some integer arithmetic is used here and there,
it would be cleaner to do your own. (And more satisfying).
> ... I
> could use the zero-page area that BASIC uses, if I'm not going to use
> BASIC at all.
There are zero page areas that can be used without worrying too much
about Basic. Program code should be kept out of zero page, howver;
that space is too valuable to waste.
Ask if you're looking for anything specific. I see that there are
already quite a few good answers posted here.
--Jim Butterfield
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
|
|
|
Re: How do coders handle 16-bit numbers? [message #160812 is a reply to message #160783] |
Sun, 19 March 2006 12:52 |
maurice
Messages: 197 Registered: January 2005
Karma: 0
|
Senior Member |
|
|
Craig Taylor <ctalkobt@gmail.com> wrote:
> To add 16 bit numbers
>
> lda #>num1
> clc
> adc #>num2
> sta #>result
> lda #<num1
> adc #<num2
> sta #<num2
>
> To subtract
> lda #>num1
> sec
> sbc #>num2
> sta #>result
> lda #<num1
> sbc #<num2
> sta #<result
Just to prevent confusion and debugging sessions for anyone who might
use your examples, you did some typos.
The "<" and ">" symbols are backwards.
-Maurice
--
** Maurice Randall - Click Here Software Co.
** High-Performance for your Commodore
** email: maurice@cmdrkey.com, support@cmdrkey.com
** web: http://cmdrkey.com
|
|
|
Re: How do coders handle 16-bit numbers? [message #160818 is a reply to message #160812] |
Sun, 19 March 2006 15:09 |
David Murray
Messages: 1017 Registered: January 2005
Karma: 0
|
Senior Member |
|
|
Thanks guys for all the advice. I found two multiply routines on the
internet. One i just couldn't get to work, but I found this one on
'the fridge' (see below body of message) and it works fine. It was
poorly documented but I eventually figured it out. I'm amazed how
short it is. You should have seen my routine I was using to do
multiplication. It was 5 times longer and probably hogged 50 times as
much CPU cycles. So this should work fine. No, I don't need signed
numbers. I'm just trying to make some screen routines to place text on
the screen on the DTV's 256-color graphics mode. I needed to be able
to multiply numbers in order to set the DMA source and destination
correctly. I think this will work fine. Oh, and one more thing about
Sweet16.. I guess it isn't as cool as I thought. It doesn't even
support multiplication and division! So I think I'm better off with
this routine.
* ACC*AUX -> [ACC,EXT] (low,hi) 32 bit result
MULT LDA #0
STA EXT+1
LDY #$11
CLC
]LOOP ROR EXT+1
ROR
ROR ACC+1
ROR ACC
BCC MUL2
CLC
ADC AUX
PHA
LDA AUX+1
ADC EXT+1
STA EXT+1
PLA
MUL2 DEY
BNE ]LOOP
STA EXT
RTS
|
|
|
|
Re: How do coders handle 16-bit numbers? [message #160843 is a reply to message #160786] |
Mon, 20 March 2006 00:17 |
Anton Treuenfels
Messages: 105 Registered: December 2011
Karma: 0
|
Senior Member |
|
|
"Jim Lawless" <jimbo@radiks.net> wrote in message
news:441ce6ff.8901349@news.east.earthlink.net...
> To multiply by 10:
> Assume you have to work variables A and B and you want to multiply
> A by 10.
> Copy A to B
> Shift A left 3 bits ( A = A * 2 * 2 * 2 ) or ( A = A * 8 )
> Shift B left 1 bit ( B = B * 2 )
> Add B to A
Or perhaps:
Shift A left 1 bit. (A*2)
Copy A to B. (B=A*2)
Shift A left 2 bits. (A*8)
Add B to A. (A*10)
IIRC the Basic interpreter does something like this while converting decimal
line numbers to binary form.
- Anton Treuenfels
|
|
|
Re: How do coders handle 16-bit numbers? [message #160863 is a reply to message #160818] |
Mon, 20 March 2006 15:14 |
Charles Richmond
Messages: 2754 Registered: December 2011
Karma: 0
|
Senior Member |
|
|
David Murray wrote:
>
> Thanks guys for all the advice. I found two multiply routines on the
> internet. One i just couldn't get to work, but I found this one on
> 'the fridge' (see below body of message) and it works fine. It was
> poorly documented but I eventually figured it out. I'm amazed how
> short it is. You should have seen my routine I was using to do
> multiplication. It was 5 times longer and probably hogged 50 times as
> much CPU cycles. So this should work fine. No, I don't need signed
> numbers. I'm just trying to make some screen routines to place text on
> the screen on the DTV's 256-color graphics mode. I needed to be able
> to multiply numbers in order to set the DMA source and destination
> correctly. I think this will work fine. Oh, and one more thing about
> Sweet16.. I guess it isn't as cool as I thought. It doesn't even
> support multiplication and division! So I think I'm better off with
> this routine.
>
You might be interested in a book titled _6502 Software Design_,
by Leo J. Scanlon, ISBN: 067-221-6566. It is out of print, of
course. You can find some reasonably priced copies at:
<http://www.abebooks.com>
as well as other used book sites.
It describes how to do Booth's Algorithm for multiplication. This
algorithm works for signed numbers and usually executes faster than
the multiply that you posted. The code may be 2 times larger.
--
+----------------------------------------------------------- -----+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------- -----+
|
|
|
Re: How do coders handle 16-bit numbers? [message #160898 is a reply to message #160863] |
Mon, 20 March 2006 17:38 |
David Murray
Messages: 1017 Registered: January 2005
Karma: 0
|
Senior Member |
|
|
I think I'm going to stick with what I've got. I've already started
writing a large piece of code which calls this subroutine and it seems
to work well. I have to admit I do not fully understand how the
routine works. I'd have to sit down with an abacus or something and
shift bits around before I could see how it actually works. but from
what I can see the max number of loops for the routine is 17. That
still eats some CPU time, but not as bad as making a loop which adds
the number to itself over and over, which is what I was doing before.
However, if somebody understands the routine and could tell me how to
optimize it for smaller numbers, that would be cool. Really I do not
need a 32-bit result. I only need a 16-bit result.
|
|
|