Retrieving Div32 Remainder


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Cool Retrieving Div32 Remainder

    Here's an easy way to get the remainder or Modulus result after a Div32 statement.
    This result is not directly available thru PBP commands, but is contained in PBP system var R2.

    To retrieve it only takes 1 line and a place to put the word value.

    Code:
    Remainder Var Word
    
    @ MOVE?WW   R2, _Remainder   ; Get remainder of Div32
    The variable Remainder now contains the Modulus of the Div32

    Here's an example
    Code:
    Define LOADER_USED 1
    
    ' SetUp Hardware USART
    DEFINE  HSER_TXSTA  24h
    DEFINE  HSER_SPBRG  25   ' 9600 Baud @ 4mhz
    
    
    Result    Var Word
    Remainder Var Word
    DivideBy  Var Word
    
    hserout ["Value     = 1,000,000",13,10]
    hserout ["DivideBy  = ", Dec DivideBy,13,10]
    
    DivideBy = 55
    Result = 1000
    Result = Result * Result              ' create 1,000,000 for Div32
    Result = Div32 DivideBy
    @ MOVE?WW   R2, _Remainder            ; Get remainder of Div32
    
    hserout ["Result    = ", dec Result,13,10]
    hserout ["Remainder = ", dec Remainder,13,10]
    
    ' Change remainder to Decimals
    Remainder = Remainder * 10000        ' Multiply remainder * 10,000
    Remainder = div32 DivideBy           ' Divide remainder by original divisor
    
    ' Display result with 4 decimal places
    Hserout ["Decimal   = ", Dec Result,".",dec4 Remainder,13,10,10]   
    
    
    stop
    And this is the output via HyperTerminal
    Code:
    Value     = 1,000,000
    DivideBy  = 55
    Result    = 18181
    Remainder = 45
    Decimal   = 18181.8181
    HTH,
    Darrel Taylor

  2. #2
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel,
    Result Var Word
    ... and later...
    Result = Result * Result ' create 1,000,000 for Div32

    a 32 bit word?
    I must look at how the decimal value of 1 million is handled.


    A $5 calculator from the shop is beginning to look like a great deal
    more than $5 worth of programming. Consider the value you get
    when you pay $5 for a calculator with 8 decimal places and a
    decimal point thrown in.

    My calculator program can add decimal integers so far.
    When a key is pressed, the value is shifted into an array that is
    rotated.. leading zero blanking makes it look like a calculator too.

    The bytes of the work array are limited 0-9, and I have created
    my own carry flag for decimal math. Then I can write routines
    that do integer math as we do it on paper.

    Is that a stupid way to do it?
    I assume a real calculator works on the hex array, and converts
    the result to decimal only for the silly human to read, as is done
    in the example you posted above.
    Cheers, Art.

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


    Did you find this post helpful? Yes | No

    Default

    No, I don't think that's a stupid way to do it. In fact, it could have some advantages. For one, your not limited by a certain number of bits 8,16,32 Your only limited by the number of digits you can fit in the array. I'll bet the divide routine's going to be a pain in the butt though.

    Result Var Word
    ... and later...
    Result = Result * Result ' create 1,000,000 for Div32

    a 32 bit word?
    I must look at how the decimal value of 1 million is handled.
    Actually, 1,000,000 isn't stored in the Result variable. The 32 bit result is stored in PBP system variables for use with DIV32, which then has to be the next statement.

  4. #4
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    Lucky the application is a customised stock counting calc.
    that's why no decimals are needed, and neither are div and
    subtract routines.
    I will have a go at anything I can do on paper though,
    and add a decimal place just for division.
    Cheers, Art.

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


    Did you find this post helpful? Yes | No

    Default

    Well, the more I learn about PBP, the more I look back on my older programs and say to myself "What was I thinking!".

    In my example above, I stated that the PBP system variables were not available to PBP commands. enhhhh, wrong answer.

    I just found out that all the PBP system vars are just like any other variables. They are defined in the PBPPICxx.RAM file.

    Knowing this, getting the remainder of a Div32 is even easier. It's in R2.

    So with the previous example, after the
      Result = Div32 DivideBy
    you don't need the @ MOVE?WW, The remainder is in R2

    Remainder = R2

    Still learning,
    Darrel

Similar Threads

  1. Optimizing DIV
    By skimask in forum mel PIC BASIC Pro
    Replies: 41
    Last Post: - 22nd September 2008, 04:58
  2. Problem with Div32
    By lerameur in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 22nd April 2008, 02:54
  3. RPM - DIV32 Problem
    By davewanna in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 11th April 2008, 04:33
  4. Div32
    By Archangel in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 2nd February 2007, 20:26
  5. 32-bit Variables and DIV32, Hourmeter 99999.9
    By Darrel Taylor in forum Code Examples
    Replies: 9
    Last Post: - 23rd November 2006, 07:23

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