<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Wed, 30 Aug 2006 21:51:04 +0100
From   : Sprow <info@...>
Subject: Re: Assembler & Decimal Notation

In article <793DE32807BC484CBEEB39ABF7841886.MAI@...>,
   Ian Wolstenholme <BBCMailingList@...> wrote:
> That FREE routine has reminded me.  This is in my top ten of
> Things I've Always Wanted To Know...
>
> When using 6502 assembler, how do you translate two- or
> three-byte numbers (or even one-byte for that matter) stored in
> memory into decimal numbers which can be printed on screen?

One approach is to repeatedly subtract powers of 10 (usually from a lookup
table) until the number goes negative, then move to the next power of 10.

For example, you have the number &123E (or 4670 decimal). It's a 2 byte
value so can't exceed 65535 so start at the 10000 column:
  4670 - 10000 = -ve 
                 print digit 0
  4670 - 1000  = +ve so loop again (4 times)
   670 - 1000  = -ve
                 print digit 4
   670 - 100   = +ve so loop again (6 times)
    70 - 100   = -ve
                 print digit 6
    70 - 10    = +ve so loop again (7 times)
     0 - 10    = -ve
                 print digit 7
     0 - 1     = -ve
                 print digit 0

Leading zero supression can be achieved with a flag somewhere.

If your number was in BCD you can also make use of the 6502's handy BCD
mode, or on the ARM coprocessor just use one of the OS_Convert SWIs as
required.

> It was always my intention to translate the Free routine into
> machine code but I got stuck because a) I could never get my
> head round how to do 2 or 3 byte number addition or subtraction
> in assembler and b) even if I had mastered all that Carry Flag,
> Overflow and Z business (can't even remember what Z is for now)
> then I wouldn't have the faintest idea how to get the answer printed
> on the screen in decimal numbers.

Infact there's one in 
  http://www.sprow.co.uk/bbc/library/sidewrom.pdf
on page 18, though that's restricted to one byte (ie. outputs 0-255 decimal)
the general concept is the same,
Sprow.
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>