Forums

How to output a decimal number

Started by Gabe October 20, 2005
does anyone know how to print a decimal number to the screen:

I get:
92
91
90
8f
8e

I want:
92
91
90
89
88

thanks dudes.




----- Original Message -----
From: Gabe
To: m68HC11@m68H...
Sent: Thursday, October 20, 2005 5:39 AM
Subject: [m68HC11] How to output a decimal number does anyone know how to print a decimal number to the screen:

I get:
92
91
90
8f
8e
> That is hexadecimal notation


I want:
92
91
90
89
88

> This is decimal notation

> You need to convert the hexadecimal to decimal notation before displaying it.

> The method you use depends on the language you are programming in and your method of displaying.

>>> Good luck, Bob Smith
thanks dudes.
------
YAHOO! GROUPS LINKS

a.. ------


>
> ----- Original Message -----
> From: Gabe
> To: m68HC11@m68H...
> Sent: Thursday, October 20, 2005 5:39 AM
> Subject: [m68HC11] How to output a decimal number > does anyone know how to print a decimal number to the screen:
>
> I get:
> 92
> 91
> 90
> 8f
> 8e
> > That is hexadecimal notation > I want:
> 92
> 91
> 90
> 89
> 88

Gabe,

Are you sure you don't want:

146
145
144
143
142
?

Paul > > This is decimal notation
>
> > You need to convert the hexadecimal to decimal notation before displaying it.
>
> > The method you use depends on the language you are programming in and your method of displaying.
>
> >>> Good luck, Bob Smith >
> thanks dudes. >
> ------
> YAHOO! GROUPS LINKS
>
> a.. > ------ >
> Yahoo! Groups Links >


--- In m68HC11@m68H..., "Gabe " <Gabemejia@m...> wrote:
>
> does anyone know how to print a decimal number to the screen:
>

The example you gave is not entirely clear, but I'm assuming that you
have a 8-bit or 16-bit integer value (say, in a register or memory
location) that you wish to output in base-10 decimal notation rather
than base-16 hexadecimal notation. Hence, the value 90 hex (9 * 16 +
0) would display as "144" and the value 8E hex (8 * 16 + 14) would
display as "142".

There are two common approaches to solving this problem, each with
their own pros and cons. Method #1 involves creating a table of
decimal divisors, like this:

Power10 DW 10000,1000,100,10,1

then convert your number using this (pseudocode) algorithm:

Index = 0
While (Number < Power10[Index])
Index = Index + 1
While (Index < 5)
Digit = Number / Power10[Index]
Number = Remainder of above division
Digit = Digit + $30 ;$30 = ASCII "0"
Output <ASCDigit>
Index = Index + 1

For a 8- or 16-bit <Number>, the above can be done quite easily using
HC11 assembly language. The total number of instructions would be
approximately 2x to 2.5x the number of pseudocode statements shown above.

The other common method does not require a power-of-10 lookup table
and is somewhat more efficient, but has the disadvantage of converting
the number backwards, requiring that the converted digits be saved to
memory or some buffer until the number is fully converted and ready
for display. This method works like this:

Index = 0
Do
Number = Number / 10
Digit = Remainder of above division (e.g. Number MOD 10)
Buffer[Index] = Digit + $30 ;$30 = ASCII "0"
Index = Index + 1
Until (Number = 0)

Do
Index = Index - 1
Output <Buffer[Index]>
Until (Index = 0) Either of the above algorithmns can be easily implemented in HC11
assembly language, esp. since the HC11 provides a convenient 16-bit
IDIV integer division instruction. Hopefully, you are familiar enough
with the HC11 instruction set so you can take the pseudocode
algorithmns above and translate them into assembly language (I have,
on numerous occasions ;) This task is left as a "excercise for the
student".