Retrieving 32bit Multiply Result


Closed Thread
Results 1 to 40 of 43

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    There's no way to do that with DIV32. The Quotient must be less than 65535.

    You would need a TRUE 32 bit divide routine.

    Or you could use the Floating Point Math routines from melabs.
    http://www.melabs.com/resources/fp.htm

    Darrel

  2. #2
    kasapo's Avatar
    kasapo Guest


    Did you find this post helpful? Yes | No

    Question

    hi darrel ,
    do you have some example about the usage of floating point numbers.

    after i do my multiplication and division how can i use aint variable?
    i have to send the aint to my motor controller byte by byte.

    thanks

  3. #3
    mytekcontrols's Avatar
    mytekcontrols Guest


    Did you find this post helpful? Yes | No

    Question Any way to use a variable for DIV32 divisor?

    Hi Darrel,

    I like your ideas about getting at DIV32 system variables, but I have a slightly different situation. How do I use a variable for the divisor that wont violate the 15 bit rule?

    I tried the following example, but it don't work (I get: Error Bad expression):
    Code:
    divisor var word
    dummy var word
    
    dummy = DIV32 divisor
    I really need the ability to alter the divisor via program control, and at times it will need to be bigger then an 8-Bit value. Any ideas?

    Thanks,

    Edit: Opps! my mistake. It does work properly with a word variable as the divisor, just so long as you stay with the 15 bit rule (32767). This is good news.
    Last edited by mytekcontrols; - 18th June 2005 at 01:24.

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    hehe,

    DT

  5. #5


    Did you find this post helpful? Yes | No

    Default Adding 2 Dwords

    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,

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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 think i can manage myself once i am sure abour the overflow
    I'm sure you could. But that's just not my way ...

    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
    RETURN
    Which displays ...
    Code:
    A=   000E:FFF8
    B=   0006:FFE4
    Res= 0015:FFDC
    OVRFLOW32 = 0
    HTH,
    DT

  7. #7


    Did you find this post helpful? Yes | No

    Default Checking for a smaller result

    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
    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
    Would this be ok? i made some tests and it seems ok but your experience might see anything wrong here.
    Last edited by Josuetas; - 28th July 2007 at 16:03.

Similar Threads

  1. Strugling without floating point
    By pjsmith in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 27th March 2011, 07:29
  2. Math help please!!!
    By jbirnsch in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 10th August 2007, 15:45
  3. 32-bit Variables and DIV32, Hourmeter 99999.9
    By Darrel Taylor in forum Code Examples
    Replies: 9
    Last Post: - 23rd November 2006, 08:23
  4. PBP 16-bit ADC result math
    By sonic in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 13th March 2005, 15:21
  5. Retrieving Div32 Remainder
    By Darrel Taylor in forum Code Examples
    Replies: 4
    Last Post: - 20th August 2003, 04:53

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