32 bit addition in picaxe

JBrookes

Member
I looked through earlier posts, and there doesn't seem to be simple code to accumulate to a 32 bit total.
I am working with sensor inputs and need to add many of them.
SO -
The way this is done, classicly, is to mask and shift carry bits along. And this can be done with operations on the X2 chips.
The question is whether there is a way to add in this code at an assembler level, since putting it in Basic may not be the most efficient.

Perhaps I am missing something here, but this is not covered in the manuals, so I am soliciting comments here.
Thanks JB
 

srnet

Senior Member
The question is whether there is a way to add in this code at an assembler level, since putting it in Basic may not be the most efficient.
Perhaps I am missing something here, but this is not covered in the manuals, so I am soliciting comments here.
Its not covered in the Manuals because you cant have in-line assembler in a PICAXE Basic program.
 

inglewoodpete

Senior Member
Due to the nature of the PICAXE firmware, it is not possible to load assembled modules into the chip without erasing the firmware.

Like it or not, it's how Revolution Education make a living. If you could load an executable binary module into a PICAXE, you could download their proprietary firmware and their market could evaporate very quickly.
 

SAborn

Senior Member
Due to many devices permitting higher resolution data than 16 bit (SPI and I2C devices) it would be nice if Rev-Ed introduced commands to to handle this, like a method to string together variables greater than a word value (16 bit)

It can be done in long hand code but a shorthand command would be useful for today's slave devices.
 

hippy

Technical Support
Staff member
32-bit addition isn't too difficult if you split it into 16-bit parts ...

Code:
Symbol Q0.lsb = w0
Symbol Q0.msb = w1

Symbol Q1.lsb = w2
Symbol Q1.msb = w3

Symbol wTemp  = w4

Q0.msb = $1234 : Q0.lsb = $5678 ' Q0 = $12345678
Q1.msb = $8765 : Q1.lsb = $4321 ' Q1 = $87654321
Gosub AddQ1toQ0                 ' Q0 = Q0 + Q1

Do:Loop

AddQ1toQ0:
  wTemp  = Q0.lsb
  Q0.lsb = Q0.lsb + Q1.lsb
  Q0.msb = Q0.msb + Q1.msb
  If Q0.lsb < wTemp Then
    Q0.msb = Q0.msb + 1
  End If
  Return
 

JBrookes

Member
Bit twiddling help sought

32-bit addition isn't too difficult if you split it into 16-bit parts ...

Code:
Symbol Q0.lsb = w0
Symbol Q0.msb = w1

Symbol Q1.lsb = w2
Symbol Q1.msb = w3

Symbol wTemp  = w4

Q0.msb = $1234 : Q0.lsb = $5678 ' Q0 = $12345678
Q1.msb = $8765 : Q1.lsb = $4321 ' Q1 = $87654321
Gosub AddQ1toQ0                 ' Q0 = Q0 + Q1

Do:Loop

AddQ1toQ0:
  wTemp  = Q0.lsb
  Q0.lsb = Q0.lsb + Q1.lsb
  Q0.msb = Q0.msb + Q1.msb
  If Q0.lsb < wTemp Then
    Q0.msb = Q0.msb + 1
  End If
  Return
=======================

Hippy,
Thank you for response.
I am a C programmer, and the Basic idioms above are unfamiliar to me.
I looked though the picaxe manuals, but didn't find the fundamentals there.
Can you suggest a way to learn this?
Just a basic Basic book? Something focusing on arithmetic Basic?
Thanks in advance,
John B
 

hippy

Technical Support
Staff member
In C, something like this. I should have used msw/lsw in the above rather than msb/lsb ...

Code:
uint16 Q0_lsw;
uint16 Q0_msw;

uint16 Q1_lsw
uint16 Q1_msw;

uint16 wTemp;

void main()
{
  Q0_msw = 0x1234; Q0_lsw = 0x5678; // Q0 = 0x12345678
  Q1_msw = 0x8765; Q1_lsw = 0x4321; // Q1 = 0x87654321
  AddQ1toQ0();                      // Q0 = Q0 + Q1
}

void AddQ1toQ0()
{
  wTemp  = Q0_lsw;
  Q0_lsw = Q0_lsw + Q1_lsw;
  Q0_msw = Q0_msw + Q1_msw;
  if ( Q0_lsw < wTemp )
  {
    Q0_msw = Q0_msw + 1;
  }
}
 
Top