Working with 3 byte numbers


Closed Thread
Results 1 to 6 of 6
  1. #1

    Default Working with 3 byte numbers

    Ok so i made i post but nobody seemed to get interest so i will post something more specific.

    Consider i am using 3 byte numbers (i want to store money) and other byte for cents.

    How do you add 3 byte numbers?, how do you write these numbers in an LCD? LCDOUT works only with words...

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Option #1: You try to figure it out by yourself. Hours, days, months, weeks, loss of hair, new keyboard, etc, etc,...

    Option #2 You rely on our resident genius to have already done it for you..;o}

    Link to option #2. http://www.picbasic.co.uk/forum/showthread.php?t=1942

    It may not be a full working code snippet for your specific application, but the
    answer is there...;o}
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3


    Did you find this post helpful? Yes | No

    Default Great Idea.. now..

    Thanks for the link Bruce
    It gave me direction... But now i need more advice..

    How can i add two of this Dwords?
    http://www.picbasic.co.uk/forum/showthread.php?t=1942

    how do i know if the add of two words overflows?
    i.e. $1FFFE + $2E

    my guess is to add the lower words $FFFE+$2E. But how do i know if there is an overflow? so that i can add one to the higher WORD? even worse..

    i.e. $EFFF8 + $6FFE4

    My guess again: add two lower words... if over flow add two higher words +1, else add two higher words....

    I Hope you or Darrel Can help me..

    Thanks

  4. #4


    Did you find this post helpful? Yes | No

    Default I Found it... sorry

    The status.0 is the carry bit... thanks for any bother

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    You don't even need to mess with assembler. Here's a really simple example,
    and it's all in BASIC;
    Code:
      A VAR WORD
      B VAR WORD
      X VAR BYTE
      NumToAdd VAR BYTE
      NumToAdd = $2E
        
    MAIN:
        A = $0001
        B = $FFFE
        FOR X = 1 TO NumToAdd
          B = B + 1
          IF B = 0 THEN A = A + 1
        NEXT X
    You can extend this to pretty much any size variable you need.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Why so complex? Again all in PBP... assuming positive numbers only...

    A, B and C must all be the same type (ie all WORDS or all BYTES).

    C=A+B
    If C < A then Goto SumOverflow

    Actually, B can be a BYTE if C & A are WORDS and it still will hold water.


    Extending this to the original post...
    Code:
    AHighWord var Word
    ALowWord var Word
    BByte var Byte
    CHighWord var Word
    CLowWord var Word
    
    AHighWord=$0001
    ALowWord=$FFFE
    BByte=$2E
    
    CHighWord=AHighWord
    CLowWord=ALowWord+BByte
    If CLowWord < ALowWord then CHighWord=CHighWord+1
    or without using the variable C...
    Code:
    ALowWord=ALowWord+BByte
    If ALowWord < BByte then AHighWord=AHighWord+1
    Actually in both above examples, BByte can be replaced with BWord and it'll still work.

Similar Threads

  1. LCD freeze
    By harryweb in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 5th October 2009, 08:01
  2. Replies: 4
    Last Post: - 15th April 2009, 01:54
  3. byte compression
    By Norbert in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 16th June 2007, 18:04
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  5. 16F877 RAM Question
    By Art in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 6th August 2005, 11:47

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts