ADD with Picaxe

elektriker

New Member
I need help please.

How can I add such numbers with PicAxe?

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

or

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

AllyCat

Senior Member
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.
 

hippy

Technical Support
Staff member
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
 

elektriker

New Member
My problem is solved.

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

Thanks to the specialists.
 
Top