Shop OBEX P1 Docs P2 Docs Learn Events
Slick way to generate Modulus 3 in PASM - Any ideas? — Parallax Forums

Slick way to generate Modulus 3 in PASM - Any ideas?

Beau SchwabeBeau Schwabe Posts: 6,568
edited 2009-11-02 00:43 in General Discussion
Anyone know of a slick way to generate Modulus 3 or any other user defined Modulus value in PASM?

I could use a lookup table or partial lookup table and shift it up or down, but yuck!

This will only be needed for BYTE wide variables, and of course the fastest solution the better.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe

IC Layout Engineer
Parallax, Inc.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-11-01 07:30
    By modulus 3, do you mean the base 3 equivalent of BCD, i.e. 00, 01, and 10, but not 11 in every two-bit digit?

    -Phil
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2009-11-01 14:18
    Phil Pilgrim,

    Yes, basically just division by 3 where all I'm interested in is the remainder, I don't care about the whole number. I can put something together that works pretty good for just Modulus 3, but I wanted to provide an option that doesn't confine it to a specific number.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-11-01 21:36
    Would this work?

    mod           [b]shl[/b]       modulus,#7
                  [b]cmpsub[/b]    dividend,modulus
                  [b]shr[/b]       modulus
                  [b]cmpsub[/b]    dividend,modulus
                  [b]shr[/b]       modulus
                  [b]cmpsub[/b]    dividend,modulus
                  [b]shr[/b]       modulus
                  [b]cmpsub[/b]    dividend,modulus
                  [b]shr[/b]       modulus
                  [b]cmpsub[/b]    dividend,modulus
                  [b]shr[/b]       modulus
                  [b]cmpsub[/b]    dividend,modulus
                  [b]shr[/b]       modulus
                  [b]cmpsub[/b]    dividend,modulus
                  [b]shr[/b]       modulus
                  [b]cmpsub[/b]    dividend,modulus
    mod_ret       [b]ret[/b]  
    
    
    


    -Phil
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2009-11-01 23:22
    Phil,

    Thanks!... I think that will work just fine. Although I think you meant to add a ,#1 after each shr modulus.


    TestCode:
    [b]CON[/b]
    
      [b]_CLKMODE[/b] = [b]XTAL1[/b] + [b]PLL16X[/b]
      [b]_XINFREQ[/b] = 5_000_000
    
    
    [b]VAR[/b]
    
    [b]long[/b]    modulus,dividend,remainder1,remainder2
    
    
    [b]PUB[/b] MOD_Test| ERR_fail
    
        modulus := 3
    
        ERR_fail := 0
    
        [b]cognew[/b](@MODtest, @modulus)
    
        [b]dira[/b][noparse][[/noparse]­16..23]~~
       
        [b]repeat[/b] dividend [b]from[/b] 0 to 255
    
          remainder2 := dividend // modulus
    
          [b]waitcnt[/b]([b]cnt[/b]+clkfreq/20000)
    
          [b]if[/b] remainder1 <> remainder2
             ERR_fail := 1
             
        [b]if[/b] ERR_Fail == 1
           [b]outa[/b][noparse][[/noparse]&#173;16..23&#093; := %11111111                           'ALL LEDs on = FAIL
        [b]else[/b]
           [b]outa[/b][noparse][[/noparse]&#173;16..23&#093; := %10101010                           'Alternating LED pattern = PASS
        [b]repeat[/b]
    
    [b]DAT[/b]
                  [b]org[/b]        
    MODtest       [b]mov[/b]       _t1,                    [b]par[/b]
                  [b]rdlong[/b]    _modulus,               _t1
                  [b]add[/b]       _t1,                    #4
                  [b]rdlong[/b]    _dividend,              _t1
                  [b]add[/b]       _t1,                    #4
    '----------------------------------------------------------
    mod           [b]shl[/b]       _modulus,               #7
                  [b]cmpsub[/b]    _dividend,              _modulus
                  [b]shr[/b]       _modulus,               #1
                  [b]cmpsub[/b]    _dividend,              _modulus
                  [b]shr[/b]       _modulus,               #1
                  [b]cmpsub[/b]    _dividend,              _modulus
                  [b]shr[/b]       _modulus,               #1
                  [b]cmpsub[/b]    _dividend,              _modulus
                  [b]shr[/b]       _modulus,               #1
                  [b]cmpsub[/b]    _dividend,              _modulus
                  [b]shr[/b]       _modulus,               #1
                  [b]cmpsub[/b]    _dividend,              _modulus
                  [b]shr[/b]       _modulus,               #1
                  [b]cmpsub[/b]    _dividend,              _modulus
                  [b]shr[/b]       _modulus,               #1
                  [b]cmpsub[/b]    _dividend,              _modulus
    '----------------------------------------------------------              
                  [b]wrlong[/b]    _remainder,             _t1
    
                  [b]jmp[/b]                               #MODtest              
    
    _modulus      [b]long[/b]      0
    
    '-----------------------------------------------------------------------------------
    _dividend                       'alias dividend and remainder to same long variable
    _remainder
                  [b]long[/b]      0
    '-----------------------------------------------------------------------------------
    
    
    _t1           [b]long[/b]      0
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-11-02 00:43
    Beau Schwabe said...
    I think you meant to add a #1 after each shr modulus.
    Oops! SX and PIC habits don't fade easily, do they? That'll teach me not to post something I didn't even try to compile.

    -Phil
Sign In or Register to comment.