Numbers bigger than 65535

donrecardo

Senior Member
Hi
I want to count upto and store numbers that could
go as high as 999999

Also when I display them I want to include any
leading zeros , so the number 1724 displays as 001724 ,
always showing the full 6 figures

How can I count , and store to eeprom a number
larger than 65535 which I believe is the largest number
I can store in one word like w0 or w1

Don
 

hippy

Technical Support
Staff member
You could count upto a thousand in one word then how many thousands in another. BIN2ASCII can split them into individual digit characters then it's just a matter of showing the digits in the right order for display or output ...

Code:
Do
  w0 = w0+1
  If w0 > 999 Then
    w0 = 0
    w1 = w1+1
  End If
  :
Loop
You can store word values in Eeprom as two separate bytes, and the WRITE command simplifies coding for word storage.
 

donrecardo

Senior Member
You could count upto a thousand in one word then how many thousands in another. BIN2ASCII can split them into individual digit characters then it's just a matter of showing the digits in the right order for display or output ...

Code:
Do
  w0 = w0+1
  If w0 > 999 Then
    w0 = 0
    w1 = w1+1
  End If
  :
Loop
You can store word values in Eeprom as two separate bytes, and the WRITE command simplifies coding for word storage.
Thanks Hippy that was excellent. It even displays the leading zeros just like I wanted. I will let you know how I get on with it.
Don
 

inglewoodpete

Senior Member
I remember Jeremy Leach posted a number of "big number" maths routines a couple of years ago.

Have a look at Avoiding Overflow for ideas on PICAXE mathematics for big numbers. The post is from the old forum, so is a bit cluttered with html stuff.
 

Jeremy Leach

Senior Member
Hippy's idea is good, but another idea is simply to store each digit in a separate byte in RAM (or EEPROM) and have a custom increment routine...

Code:
IncrementNumber:
    Carry = 1
    For Address = LSAddress To MSAddress 'least sig digit to most sig digit
        Peek Address, NumValue
        NumValue = NumValue +  Carry
        
        If NumValue = 10 Then
            Carry = 1
            NumValue = 0
        Else 
            Carry = 0
        EndIf

        Poke Address, NumValue
    Next
Then have another routine to simply read out the digits to the display.
 
Last edited:

womai

Senior Member
Hi Jeremy,

the technical term for your proposed method is "BCD" (binary coded decimal). That's actually the format pretty much all pocket calculators use (and Cobol, if I remember correctly; but that language was already outdated even when I was a teenager :)

Wolfgang
 

hippy

Technical Support
Staff member
Jeremy's ideas a good one, and you can have very big numbers that way. There is an optimisation which can get speed up and code size down ...

Code:
IncrementNumber:
  Address = LSAddress
  Do
    Peek Address, NumValue
    NumValue = NumValue + 1 % 10
    Poke Address, NumValue
    Address = Address+1
  Loop While Address <= MSAddress And NumValue = 0  
  Return
 

evanh

Senior Member
Two comments:

- BCD is really 4 bits per decimal digit, packed, and looks the same as Hexadecimal except that the upper 6 codes are not used. So, the above method is not quite BCD.

- When that method of one byte per digit is used, the ASCII numeral codes are often chosen since it doesn't take any more memory and this makes them readable in a text editor.
 

Andrew Cowan

Senior Member
If you just want a slightly larger number, you can add a seperate bit to determine if it has overflowed or not, then add the number on later.

A
 
Top