Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC Maths

Status
Not open for further replies.

hantto

Member
Hello!

How is maths done with pics using assembler? The basic operations multiply, divide, add and substract.

For eg, I have a value of say 50 (110010) in a register and I want to multiply it with 60 (111100) and store it back in the register. How is this done? And the answer is over 255 so i can't use an 8-bit register anymore.

Thanks for your time.
 
hantto said:
Hello!

How is maths done with pics using assembler? The basic operations multiply, divide, add and substract.

For eg, I have a value of say 50 (110010) in a register and I want to multiply it with 60 (111100) and store it back in the register. How is this done? And the answer is over 255 so i can't use an 8-bit register anymore.

You have to use more than one register, two registers will give you 16 bit maths, three gives 24 bit, and so on.

If you look on the PICList there's various maths routines, also at Dontronics http://www.dontronics.com you can download a version of a PIC Book which gives 16 bit maths routines. If you want 24 bit, there are routines in the LCF meter project at EPE Magazine http://www.epemag.wimborne.co.uk, look in the download PIC section.
 
If your multiplier / divisor is 2^n then you can just shift left / right. Use a loop to shift one bit at a time and copy the carry bit over to a second register and you've got 16 bit math.

Dan East
 
Integer math above 8 bits is pretty easy on the PIC. 2 8-bit numbers added together can be no larger than 9-bits. The PIC has a carry bit which is set when your sum is larger than 0xFF.

Integer multiplication is not that difficult either. Think of the problem the same way you do multiplication on paper with base 10 numbers. You line up the numbers, multiply by each digit, shifting to the left one space for every new digit you multiply by. When you are done you add all of the values together. Binary multiplication works the same way but since each digit can only be 1 or 0, you are either adding a shifted number or not. I know that is a bad explanation but look at it in an actual problem in base 10 and base 2 (I will work with 170*85):

Code:
Base 10:

          170
         x 85
         ----
          850
       +13600   <--- shift left
       ------
        14450    <------ answer in base 10


Base 2  (still 170*85)

          10101010
         x01010101
         ---------
          10101010
                0   <--shift left
        1010101000
              0
      101010100000
            0
    10101010000000
   +      0
  ----------------
    11100001110010  <----- answer in Base 2 (still equals 14,450)

That type of multiplication is pretty easy to write and executes pretty quickly as well. If you have an 18F series of PIC, the hardware multiplier will do that much faster, but you can use the same type of algorithm to break down larger multiplcation problems.

Hope that helps!

-Bill
 
You PIC-head true believers crack me up. There you are, still banging rocks together to make a fire. How many registers you guys got? Like two? Three? Pfft. Why don't you move over to a uC that is optimized for C, single cycle instructions, hardware multiply...

Let me tell you, it's been a long time since I've had to worry about doing floating point calculations in assembly. I've felt better ever since.

AVRin' it and lovin' it,
j.

Ok : Flame away 8)
 
okay i dont mean to flame u. but u dont need an AVR to use C. the 18 series PICs are optimized for C. actually the guy who asked the question was wanting to know how to do maths in assembly. i wont comment on the C Vs Assembly topic because i havent started using C with PICs. in my opinion it doesnt hurt to try a bit of assembly.

and if u wont make fire with banging rocks how would u appreciate the modern day fire producing techniques. its just my dumb theory. that u should once re-invent the wheel. and see how things were in the first place and compare them with the new things. u never know u might come up with a something new.

as for AVR, id say that i would love to get enough time to play with them too.
 
John Sorensen said:
Why don't you move over to a uC that is optimized for C, single cycle instructions, hardware multiply...

Let me tell you, it's been a long time since I've had to worry about doing floating point calculations in assembly. I've felt better ever since.

I use C with PIC18 and love it, it's got single cycle instructions and HW mult... but really if I had to take the same code and compile for a PIC16 I probably wouldn't notice the difference.

Floating point ops are massive resource users and C doesn't change that. If you're writing assembly I'd assume you'd not sweat writing the routine yourself but bring in a library of floating point math routines- basically the same thing C does.
 
He He.

Actually, what should I care if another likes PIC or Zilog, or any other lesser uC. I'm just pullin your string.

However, seriously, if you're using a uC that has the resources and architecture to support C, I'd recommend doing it. Programming is more productive and code is more readable. Yes, I've programmed in assembly, too, but I get more done in C. Unless there is a compelling reason to use assembly, but it is also possible to use inline assembly in C.

j.
 
I use C with PIC16F87xA's and the only problem i found is that 24bit math is a real hog of progam space.

But truly, when you have created your C code you should go through the ASM code, even remake it there. Chances are you will get it going 5% faster using 10% less code!

Only until a chip actually reads C code rather than ASM code, ASM will only be perfect. Just think, even language translators (the speaking kind) get 2% of words wrong and they have been to uni etc. to learn the language, what hope does a forty-something programmer who's studdied Com Sci and done a masters in Comms or AI or somming gonna have translating C into fully optimised ASM. Ok over exagerating a little bit, ok a lot ;)
 
You're right, but most applications aren't that demanding. And with the increasing capacity of uCs, you can get away with a little sloppiness. Like I said, unless there is a compelling reason to use ASM, like if you're squeezed for space or speed.

j.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top