Decimal out algorithm

This is a discussion on Decimal out algorithm within the Tech Board forums, part of the Community Boards category; Hey, I'm not sure if this is the right place to post this but anyway. Does anyone know of an ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    36

    Decimal out algorithm

    Hey, I'm not sure if this is the right place to post this but anyway.

    Does anyone know of an algorithm which can be used to print out decimal values from a register using machine code.

    Basically, the machine code debugger I am using has an instruction that will output a character corresponding to value of a byte. At the moment I am doing the following:

    $1 - stores number to be output
    $2 - counter
    $3 - unit
    $4 - tens

    basically I increment $2 and $3 until $2 is equal to $1. Meanwhile, if $3 becomes equal to 10 (in decimal) I reset $3 to 0 and increment $4 by one. I then take the number of tens and unit and add 48 to convert them into the correct ascii character for writing out to the screen.

    Obviously, if the number is greater than two digits my algorithm doesn't work. Sure, I could implement a hundredth counter, thousandth counter etc but the code becomes poor and messy.

    Thanks,
    Last edited by rocketman50; 02-28-2009 at 04:06 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    What debugger are you using?

    I'm not entirely sure what you're asking, but maybe the following will help . . .
    Code:
    do {
        printf("%d", number % 10);
        number /= 10;
    } while(number > 0);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  2. Damn binary to decimal algorithm
    By xmltorrent in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2006, 04:30 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21