Multiplying by a power of 2 is easy, just use N ASLs. Multiplying by, e.g. 10, isn't much harder:
Code:
STA TMP
ASL
ASL ; a*4
CLC
ADC TMP ; a*4+a = a*5
ASL ; (a*4+a)*2 = a*10
To use a different multiplier, e.g. 13, you must change the code (again, untested):
Code:
STA TMP
ASL
CLC
ADC TMP ; a*2+a = a*3
ASL
ASL ; (a*2+a)*4 = a*12
CLC
ADC TMP ; (a*2+a)*4+a = a*13
Likewise multiply-by-31 can be implemented as a*32-a. Some mulitpliers are more convenient than others. 10 works out great, whether you're multiplying an 8-bit, 16-bit, or 32-bit number, because 10 has only 2 bits that are ones. On the other hand, using this technique to multiply, e.g., a 32-bit number by $A9D6 may not be worth it because there are a whole bunch of one bits in $A9D6.
Calculating the remainder when the divisor is a power of 2 is also easy, e.g. mod 8 is AND #7. The analogous technique when the divisor is not a power of 2 is here:
http://6502org.wikidot.com/software-math-fastmodLikewise, the routine must be changed when a different divisor is used, and some divisors will be more convenient than others.
Likewise, calculating the quotient when the divisor is a power of 2 is easy, just use N LSRs. The routine linked in the original post is the analog for calculating quotients when the divisor is not a power of 2. Again, the routine must be changed for a different divisor and some divisors will be more convenient than others.
Remember repeating decimals back when you learned them in school? e.g.:
Code:
______
1/13 = .076923
i.e. 1/13 = (7/100 + 6/1000 + 9/10000 + 2/100000 + 3/1000000) + ...
In base 2:
.1 = 1/2
.01 = 1/4
.001 = 1/8
etc.,
Code:
____________
1/13 = .000100111011
i.e. 1/13 = (1/16 + 1/128 + 1/256 + 1/512 + 1/2048 + 1/4096) + ...
So the basic technique is to shift right and add. There are a couple of complications, however. First, there are an infinite number of terms, so you have to determine how many terms you need. You'll need more terms to divide a 32-bit number by 13 than to divide an 8-bit number by 13. (This differs from multiplication by 13, where there are always 3 terms, N*8 + N*4 + N, regardless of whether N is 8, or 16, or 32 bits.)
Second, since you are shifting right, you are losing the least significant bit as it gets shifted out of the register/variable. Thus, there are three possible things to precede the ADC: a CLC, a SEC, or nothing (i.e. use the bit you just shifted out as the carry). Here is the 8-bit routine for divide by 13:
Code:
STA TMP
LSR
LSR ; a/4
ADC TMP ; a/4 + a
ROR ; a/8 + a/2
ADC TMP ; a/8 + a/2 + a
ROR ; a/16 + a/4 + a/2
ADC TMP ; a/16 + a/4 + a/2 + a
ROR
LSR
LSR ; a/128 + a/32 + a/16 + a/8
SEC
ADC TMP ; a/128 + a/32 + a/16 + a/8 + a
ROR
LSR
LSR
LSR ; a/2048 + a/512 + a/256 + a/128 + a/16
Here is the 8-bit routine for divide-by-10:
Code:
; ____
; 1/10 = .1 decimal = .00011 binary
; = (1/16 + 1/32) + (1/256 + 1/512) + (1/4096 + 1/8192) + ...
STA TMP
LSR
LSR
LSR ; a/8
ADC TMP ; a/8 + a
ROR ; a/16 + a/2
ADC TMP ; a/16 + a/2 + a
ROR
LSR
LSR ; a/128 + a/16 + a/8
ADC TMP ; a/128 + a/16 + a/8 + a
ROR ; a/256 + a/32 + a/16 + a/2
SEC
ADC TMP ; a/256 + a/32 + a/16 + a/2 + a
ROR
LSR
LSR
LSR ; a/4096 + a/512 + a/256 + a/32 + a/16