[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Applesoft commands



In article <52vdkg$mf4@login.freenet.columbus.oh.us>,
Dave Althoff <dalloff@freenet.columbus.oh.us> wrote:
>First of all, the formula for translating negative numbers under Applesoft
>is n + 65536; for example, "PEEK (-16384)" is equal to "PEEK (49152)".  I
>think the inclusion of this little trick was originally related to a
>problem with Integer BASIC: 

   That 'problem' didn't start with integer basic; it went a bit
further back. Basically, this comes from trying to store positive and
negative numbers in a n-bit value: how do you determine if something's
negative? Solution: use the highest bit as sort of a sign bit. With
the 2's-complement notation used by the 65xx series (and most other
things), to negate something, you invert each bit, and then add 1.

   2's complement is rather neat-- you don't have to worry about signs
all the time if you know what ranges your values are in. Adding $01 to
$45 is $46, adding $FF (-1) gives $144, but if you keep only the low 8
bits, you're left with $44. (Of course, there are special flags set
when things overflow the lower 8 bits so you can deal with that if you
so desire.)

   Integer basic wanted signed numbers, so they stuck with 2s
complement 16-bit values. Thus, only 15 bits of numbers can be
used. 2^15-1 is 32767, the maximum positive value storable under that
scheme. [Zero is counted as a positive number, so you get 0-32727 on
the positive side of things, and -1 to -32768 on the negative side.]

   ROM and the softswitches start at $C000, and go up. Since the
highest bit is set, that is a "negative" number if you are dealing
with only signed 16-bit values. 

   Other things have allowed unsigned data types, where the sign bit
is replaced by a bit of precision. An unsigned 16-bit value lets you
go from 0 to 65536 within the same 2 bytes. Applesoft allows that
format, so that's how the positive numbers >32768 are the same as the 
negative numbers in integer basic.

Nathan Mates
--
<*> Nathan Mates http://www.visi.com/~nathan/      <*>
# What are the facts? Again and again and again-- what are the _facts_?
# Shun wishful thinking, avoid opinion, care not what the neighbors
# think-- what are the facts, and to how many decimal places?  -R.A. Heinlein