hehe,
DT
I just saw this link. it is of great interest to me right now...
I have this question... how do you add two of these Dwords?
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 think i can manage myself once i am sure abour the overflow
Thanks,
For words and bytes, you can detect an overflow by testing if the result is less than what you added.
Due to trucation of a 16-bit word ...
$FF00 + $0180 = $0080
So the result will always be less than either of the values added if an overflow occurs.
I'm sure you could. But that's just not my way ...I think i can manage myself once i am sure abour the overflow
Which displays ...Code:;Initialize your hardware first A VAR WORD[2] B VAR WORD[2] Result VAR WORD[2] OVRFLOW32 VAR BIT ASM ; ===== Load 32bit Constant into DWORD ============================= MOVE?CD macro C32in, Dout ; Max= 4,294,967,296 MOVE?CB low C32in, Dout MOVE?CB low (C32in >> 8), Dout + 1 MOVE?CB low (C32in >> 16), Dout + 2 MOVE?CB low (C32in >> 24), Dout + 3 endm ENDASM @ MOVE?CD 0xEFFF8, _A ; Load 983,032 into A @ MOVE?CD 0x6FFE4, _B ; Load 458,724 into B GOSUB Add32 LCDOUT $FE,1, "A= ",HEX4 A[1],":",HEX4 A[0] LCDOUT $FE,$C0,"B= ",HEX4 B[1],":",HEX4 B[0] LCDOUT $FE,$90,"Res= ",HEX4 Result[1],":",HEX4 Result[0] LCDOUT $FE,$D0,"OVRFLOW32 = ", BIN1 OVRFLOW32 stop ; ===== Add 2 32bit variables ====================================== Add32: OVRFLOW32 = 0 Result[0] = A[0] + B[0] IF Result[0] < B[0] then Result[1] = 1 Result[1] = Result[1] + A[1] IF Result[1] < A[1] then OVRFLOW32 = 1 Result[1] = Result[1] + B[1] IF Result[1] < B[1] then OVRFLOW32 = 1 RETURNHTH,Code:A= 000E:FFF8 B= 0006:FFE4 Res= 0015:FFDC OVRFLOW32 = 0
DT
Checking for a smaller result is a simple trick, thanks,
right now my code is never suposed to exced the Max= 4,294,967,296
so i am ignoring the OVRFLOW32 = 0.
This is the code that i am using
Would this be ok? i made some tests and it seems ok but your experience might see anything wrong here.Code:Bigword var word [2] BigwordL var BigWord(0) BigwordH var BigWord(1) Bigword1 var word [2] Bigword1L var BigWord(0) Bigword1H var BigWord(1) AddTwoBigWords: 'Adds Bigword1 and BigWord2 result in BigWord BigWordH = BigWordH + BigWord1H ;ignoring any overflow BigWordL = BigWordL + BigWord1L if BigWordL < BigWord1L then BigWordH = BigWordH+1 ;ignoring any overflow endif return
Last edited by Josuetas; - 28th July 2007 at 16:03.
I was so confused about the Multiply operation that i posted here but.... i am now just using ** and * :s sorry
Last edited by Josuetas; - 31st July 2007 at 21:11.
Well i am really proud of this, i Hope its ok.
For anyone in need of a Division of a BigWord by some number i found this problem:
Lets think of a BigWord as considered before ^
Now you might want to Divide a value stored in tBigWord1 by some other value, in this case (EXAMPLE CODE) the value is stored in factoriva.
One could use the DIV32 function as proposed by Darrel in this or other post BUT if factoriva is too small the result CAN be higher than a word size FFFF, and the the result is no good.
For example:
4:01F5 DIV32 2 the result of DIV32 is
Result = FFFF, Remainder = 01F7 !! Wrong i guess
This code is possible just thanks to Darrel`s @ PutMulResult?D MACRO
This code in the serial port os The value tBigWord1 Divided for Factors 1 to 100Code:Bigword var word [2] BigwordL var BigWord(0) BigwordH var BigWord(1) Bigword1 var word [2] Bigword1L var BigWord(0) Bigword1H var BigWord(1) MainLoop: lcdout $fe,1 tbigword1H = 4 ' My Dollars Value High Word tbigword1L = 501 ' My Dollars value Low Word cents = 24 ' My cents value GOSUB DIVBIGWORD HSEROUT [#FactorIva," ",HEX4 tBigWordH,":", HEX4 tBigWordHL,10,13] FactorIva= FactorIva+ 1 IF FactorIva= 101 THEN END ENDIF GOTO MainLoop 'This SUB receives tBigWord1(Dword) a value to be divided by 'FactorIva (word), Result in tBigWord, works for any value of FactorIva 'and tBigWord1 in their limits. DIVBigWord: tBigWordH= tBigWord1H / FactorIva tBigWord1H = tBigWord1H // FactorIva @ PutMulResult?D _tBigWord1 tBigWordL = DIV32 FactorIva Remainder = R2 RETURN
Seems to work.
Factor Result
2 0002:00FA
Remember to Add the @ PutMulResult?D MACRO posted by Darrel at the begining of the Code.
Bookmarks