PDA

View Full Version : Working with 3 byte numbers



Josuetas
- 26th July 2007, 15:28
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...

Bruce
- 26th July 2007, 15:44
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}

Josuetas
- 26th July 2007, 20:47
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

Josuetas
- 26th July 2007, 21:50
The status.0 is the carry bit... thanks for any bother

Bruce
- 26th July 2007, 22:22
You don't even need to mess with assembler. Here's a really simple example,
and it's all in BASIC;


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.

Melanie
- 26th July 2007, 22:59
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...


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...


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.