# PIC ASM math

Discussion in 'Embedded Systems and Microcontrollers' started by wannaBinventor, Jul 2, 2010.

1. ### wannaBinventor Thread Starter Member

Apr 8, 2010
179
4
I was talking a few days back about using a C for quick and easy math in my assembly programs. I quickly discovered that this idea was more trouble than it was worth.

As such, I took the advice of t06afre (thanks man) to look at this website:
http://www.piclist.com/techref/microchip/math/basic.htm

DIVIDING:
I have taken and reworked the code a bit and renamed the user files to more user friendly names. Only 4 user files are required for the division, counting the ones where you designate the numerator and divisor.

I chopped the website's 40 lines of code into about 14 (18 counting setting up the numerator and divisor). This code probably isn't as capable as the website's code, but it still does what I need it to do which will be dividing ADC readings by 10 which will then be routed to lookup tables which then will....long story short, make calls for clocking digits into an LCD.

Numbers 255 and lower can be divided. Division by zero yields 255 and puts the numerator you specified in the remainder. If you divide to an answer less than 1, it gives ANSWER = 0, REMAINDER = difference in divisor and numerator.

PLEASE TEST THIS CODE ON YOUR OWN BEFORE YOU RELY ON IT. I'M A NEWB AT THIS, AND CAN'T PROMISE THAT IT WON'T GO HAYWIRE UNDER CERTAIN CONDITIONS, BURNING YOUR PIC UP, YOUR CIRCUIT, YOUR HOUSE, YOUR CITY BLOCK, EVENTUALLY BECOMING A GREAT CONFLAGRATION THAT SOMEHOW LEADS TO THE VIOLATION OF THE SECOND LAW OF THERMODYNAMICS, CREATING ANTI-MATTER THAT DESTROYS THE UNIVERSE. I THINK I'VE MADE MY POINT.

First four lines below are just setting up the numerator and divisor as an example. The actual division code could be set up in the subroutine section.

Code ( (Unknown Language)):
1.
2.         MOVLW    .55
3.         MOVWF    NUMERATOR
4.         MOVLW    .6
5.         MOVWF    DIVISOR
6.
7. DIVIDE  movfw     NUMERATOR
9.         clrf     REMAINDER
10.         movlw     0x08
11.         movwf     NUMERATOR
13.         rlf     REMAINDER, f
14.         movfw     DIVISOR
15.         subwf     REMAINDER, w
16.         skpnc
17.         movwf     REMAINDER
18.         decfsz     NUMERATOR, f
19.         goto     DIV2
21.
MULTIPLICATION:
This only works if you are multiplying two numbers whose product is less than 255. Uses may be fairly limited, but I think it may come in handy for me a time or two. This requires only two user files: MULTIPLIER and MULTWORK. You could add a third if you chose because the answer spits out in the MULTIPLIER file, which may be confusing. This could be overcome by just moving it to another register called ANSWER. I can't remember what code I started with from that website, just know that this may be (probably is) less efficient/effective than those offered there. See above disclaimer.

Code ( (Unknown Language)):
1.
2.             MOVLW        .8          ;***FIRST MULTIPLIER HERE!!!***
3.             MOVWF        MULTIPLIER
4.             MOVLW        .10  ;MULTIPLICAND - Just put in W before calling multiply sub
5.
6. MULTIPLY    CLRF         MULTWORK
7.             CLRF         COUNT
8.             BSF         COUNT,3    ;Puts decimal "8" in file count,= b'00001000'
9.             RRF         MULTIPLIER,F
10.
11. MULTILOOP    SKPNC                     ;built in macro to skip on no carry
12.             ADDWF         MULTWORK,F    ;part of multiplication procedure
13.             RRF         MULTWORK,F    ;part of multiplication procedure
14.             RRF         MULTIPLIER,F    ;part of multiplication procedure
15.             DECFSZ         COUNT        ;part of multiplication procedure
16.             GOTO         MULTLOOP    ;answer spits out here in "MULTIPLIER"
17.

2. ### MMcLaren Well-Known Member

Feb 14, 2010
759
116
Can you tell us more about what you're trying to do? I ask because you can often avoid division by using reciprocal multiplication.

You might also check out the code repository at PICLIST for math routines.

Regards, Mike

3. ### atferrari AAC Fanatic!

Jan 6, 2004
2,663
784
It started by entering there, it seems...