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.
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.
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.
Rich (BB code):
MOVLW .55
MOVWF NUMERATOR
MOVLW .6
MOVWF DIVISOR
DIVIDE movfw NUMERATOR
movwf ANSWER
clrf REMAINDER
movlw 0x08
movwf NUMERATOR
DIV2 rlf ANSWER, f
rlf REMAINDER, f
movfw DIVISOR
subwf REMAINDER, w
skpnc
movwf REMAINDER
decfsz NUMERATOR, f
goto DIV2
rlf ANSWER, f ;ANSWER WITH REMAINDER SPITS OUT HERE
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.
Rich (BB code):
MOVLW .8 ;***FIRST MULTIPLIER HERE!!!***
MOVWF MULTIPLIER
MOVLW .10 ;MULTIPLICAND - Just put in W before calling multiply sub
MULTIPLY CLRF MULTWORK
CLRF COUNT
BSF COUNT,3 ;Puts decimal "8" in file count,= b'00001000'
RRF MULTIPLIER,F
MULTILOOP SKPNC ;built in macro to skip on no carry
ADDWF MULTWORK,F ;part of multiplication procedure
RRF MULTWORK,F ;part of multiplication procedure
RRF MULTIPLIER,F ;part of multiplication procedure
DECFSZ COUNT ;part of multiplication procedure
GOTO MULTLOOP ;answer spits out here in "MULTIPLIER"