​ ​ ​ ​ ADD with Picaxe
Results 1 to 5 of 5

Thread: ADD with Picaxe

  1. #1
    New Member
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    19

    Default ADD with Picaxe

    I need help please.

    How can I add such numbers with PicAxe?

    $ FFFE
    + $ 0001
    + $ 0001
    -----------
    = $10000

    or

    $FFFE
    + $FFFE
    + $0001
    + $0001
    ----------
    = $1FFFE
    thank you

  2. #2
    Senior Member
    Join Date
    May 2012
    Location
    Macduff
    Posts
    1,061

    Default

    FFFF is the limit of a Picaxe word and will roll over to 0000 when exceeded.
    Time is natures way of stopping everything going wrong at once

  3. #3
    Senior Member
    Join Date
    Feb 2012
    Location
    London
    Posts
    2,896

    Default

    Hi,

    Hippy has posted code snippets to detect an overflow (carry or borrow) in "word" (16-bit) calculations, and I have posted various subroutines in that section for more complex "double-word" addition, multiplication and division, etc..

    Cheers, Alan.

  4. #4
    Technical Support
    Join Date
    Jan 1970
    Location
    UK
    Posts
    24,438

    Default

    One example to show how 32-bit addition can be done which will run under PE6's simulator ...

    Code:
    Symbol reserveW0 = w0 ; b1:b0
    
    Symbol acc.lsw   = w1
    Symbol acc.msw   = w2
    Symbol num.lsw   = w3
    Symbol num.msw   = w4
    
    SerTxd( "$FFFE + $0001 + $0001 ...", CR, LF )
    
    acc.msw = $0000 : acc.lsw = $FFFE ; acc = $0000FFFE
    Gosub PrintAcc
    
    num.msw = $0000 : num.lsw = $0001 ; num = $00000001
    Gosub Add
    Gosub PrintAcc
    
    num.msw = $0000 : num.lsw = $0001 ; num = $00000001
    Gosub Add
    Gosub PrintAcc
    
    SerTxd( "$FFFE + $FFE + $0001 + $0001 ...", CR, LF )
    
    acc.msw = $0000 : acc.lsw = $FFFE ; acc = $0000FFFE
    Gosub PrintAcc
    
    num.msw = $0000 : num.lsw = $FFFE ; num = $0000FFFE
    Gosub Add
    Gosub PrintAcc
    
    num.msw = $0000 : num.lsw = $0001 ; num = $00000001
    Gosub Add
    Gosub PrintAcc
    
    num.msw = $0000 : num.lsw = $0001 ; num = $00000001
    Gosub Add
    Gosub PrintAcc
    
    End
    
    Add:
      w0      = acc.lsw
      acc.lsw = acc.lsw + num.lsw
      acc.msw = acc.msw + num.msw
      If acc.lsw < w0 Then
        acc.msw = acc.msw + 1
      End If
      Return
    
    PrintAcc:
      SerTxd( "$" )
      b0 = acc.msw / $1000 : Gosub PrintHexDigit
      b0 = acc.msw / $100  : Gosub PrintHexDigit
      b0 = acc.msw / $10   : Gosub PrintHexDigit
      b0 = acc.msw / $1    : Gosub PrintHexDigit
      b0 = acc.lsw / $1000 : Gosub PrintHexDigit
      b0 = acc.lsw / $100  : Gosub PrintHexDigit
      b0 = acc.lsw / $10   : Gosub PrintHexDigit
      b0 = acc.lsw / $1    : Gosub PrintHexDigit
      SerTxd( CR, LF )
      Return
    
    PrintHexDigit:
      b1 = b0 & $0F + "0" : If b1 > "9" Then : b1 = b1 + 7 : End If
      SerTxd( b1 )
      Return

  5. #5
    New Member
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    19

    Default

    My problem is solved.

    You should write a book about integer math.
    In this forum you can learn a lot.

    Thanks to the specialists.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •