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

    Lightbulb Retrieving 32bit Multiply Result

    Here's an example that I find rather useful from time to time.
    You may have seen in the manual that when you multiply two 16bit numbers together, the result is a 32bit number that can then be used with the DIV32 command. But the 32bit multiply result is not available to the user, it's only in PBP system variables where you can't get at it.

    This little routine let's you retrieve that 32 bit result.
    Code:
    A32bitVar       var word[2]
    Dummy           var word
    
    ASM
    GetMulResult macro Dword
        MOVE?WW    R2, Dword         ; Low Word
        MOVE?WW    R0, Dword + 2   ; High Word
        endm
    ENDASM
    
    Dummy = 1000
    Dummy = Dummy * Dummy
    @ GetMulResult  _A32bitVar
    
    end
    The 32 bit result for 1,000,000 is now contained in the two words of A32bitVar.

    HTH,
    Darrel Taylor

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


    Did you find this post helpful? Yes | No

    Arrow

    And here's the other side of the coin.

    Suppose you want to put a 32 bit number in the PBP system vars where it can be used by DIV32.
    But you don't want to have to multiply two numbers together to get it.

    This allows you to enter a constant, up to 2,147,483,648 which can then be used by DIV32.

    Code:
    W1   var  word
    
    ASM
    PutMulResult macro Const32
        MOVE?CB   low Const32, R2
        MOVE?CB   low (Const32 >> 8), R2 + 1
        MOVE?CB   low (Const32 >> 16), R0
        MOVE?CB   low (Const32 >> 24), R0 + 1
      endm
    ENDASM
    
    @ PutMulResult 1000000   ; Load PBP internal vars  -  Max= 2,147,483,648
    W1 = DIV32 300           ; Divide 1000000/300
    
    '  W1 will now contain 3333
    
    @ PutMulResult 10562010
    W1 = DiV32 1000        ; Divide 10562010/1000
    
    '  W1 is now 10562
    -OR-

    If the number is contained in a 2 word variable. You can load the "MUL result" like this...
    Code:
    Asm
    PutMulResult?D macro Din
        MOVE?BB   Din, R2
        MOVE?BB   Din + 1 , R2 + 1
        MOVE?BB   Din + 2, R0
        MOVE?BB   Din + 3, R0 + 1
        RST?RP
      endm
    EndAsm
    
    DWord  VAR  WORD[2]  ; holds a 31-bit value
    
    @ PutMulResult?D _DWord   ; Load PBP internal vars  -  Max= 2,147,483,648
    W1 = DIV32 10000           ; 31-bit value / 10,000
    The possibilties, are endless

    HTH,
    Darrel Taylor

  3. #3
    kasapo's Avatar
    kasapo Guest


    Did you find this post helpful? Yes | No

    Question Math problem

    Hi darel,

    i have a motor control application. in my application pic 16f877 is reading 2 position and then take the ratio of these values and multiply with the constant velocity. for example
    x=5 y=10 then Vy = Vc (constan velocity) , Vx = Vc*x/y
    so two motor begin and stop at the same time. i wrote some code by using Div32 but sometimes i cannot get the values thet it must be.

    // for Trial i give posy greater than posx
    POSX VAR WORD
    POSX1 VAR POSX.BYTE1
    POSX0 VAR POSX.BYTE0

    POSY VAR WORD
    POSY1 VAR POSY.BYTE1
    POSY0 VAR POSY.BYTE0

    V VAR WORD
    V0 VAR V.BYTE0
    V1 VAR V.BYTE1

    HV VAR WORD
    HV0 VAR HV.BYTE0
    HV1 VAR HV.BYTE1

    A VAR WORD
    A0 VAR A.BYTE0
    A1 VAR A.BYTE1


    C VAR WORD
    C1 VAR C.BYTE1
    C0 VAR C.BYTE0

    DUMMY VAR WORD


    Asm
    PutMulResult macro Const32
    MOVE?CB Low Const32, R2
    MOVE?CB Low (Const32 >> 8), R2 + 1
    MOVE?CB Low (Const32 >> 16), R0
    MOVE?CB Low (Const32 >> 24), R0 + 1
    endm
    EndAsm

    GoSub USBREAD
    POSX1=HB
    POSX0=LB
    GoSub USBREAD
    POSY1=HB
    POSY0=LB

    IF POSY > 6554 Then
    DUMMY = POSY * 10 // i scale posy by 10
    C= Div32 POSX
    @ PutMulResult 2236920 // i also enter here the scaled c constant velocity by 10
    V= Div32 C
    HV = 34 / C
    Else
    DUMMY = POSY * 10
    C = DUMMY / POSX
    @ PutMulResult 2236920
    V= Div32 C // when this is greter than 16 bits it returns ffff
    HV = 34 / C
    EndIF

    how can i get the V when it is greater than 16 bit ?
    thanks

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

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

  6. #6
    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 00:24.

Similar Threads

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

Members who have read this thread : 4

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