ASM 4bit by 4bit multiplication by software


I need to replace a ASM operation because there is no such in my architecture (avrtiny).

mul X,Y
Where X and Y is a 4bit number

I wonder what would be the fastest operation to replace mul? What I find in google was 8bit * 8bit what is not really optimal.

Lookup table.

Thank you

I not really needed to program in ASM before. I didn’t know before this will be important: X will be always 14 Y is between [0 .. 10]

So the table will be not so big

Can't you just take the 8x8 multiply and shorten it? If you use the unrolled mpy8u from AVR200b.asm, you can cut the number of steps in half, and get rid of the "high byte" result entirely. That should give you something like 12 instructions and 16 clocks... Slightly slower than a lookup table (which I think would take at least 7 cycles?), and much shorter.

X will be always 14
Y is between [0 … 10]

Multiplying by a constant is even easier. Have you check what the compiler outputs (ok; I did; it’s not very good.)

;; calculate result = Y * 14 by:  result = (Y*16) - 2*y
   mov result, Y  ;; result = Y
   swap result    ;; result = Y*16
   sub result, Y  ;; result = Y*15
   sub result, Y  ;; result = Y*14
;; done.  4 cycles?

Thank you!

It is looks good. Unfortunately the chip not have a SWAP operation (AVR attiny85).

i try to replace with ROL but still got some error regarding: Error: unknown opcode about the 3. ROL

** mov x1, x2 ;;;** __ rol x1 ;;; swap x1 result = Y*16__ ** rol x1 ;;;** ** rol x1 ;;;** ** rol x1 ;;;** __ sub x1, x2 ;;; result = Y*15__ __ sub x1, x2 ;;; result = Y*14__

ebalcsk: Unfortunately the chip not have a SWAP operation (AVR attiny85).

What makes you think so? ATtiny85 does have SWAP.

ATtiny85 does have SWAP.

Yes! It is there in the Instruction Set Summary of ATtiny85.

In the absence of SWAP (which should be there - even the ATtiny10 has it), you'd want a shift left (SWAP does the equivalent of a rotate, but that's an "optimization" assuming that the original values is less than 16.) a shift-left is just adding to yourself:

   mov x1, x2
    add  x1, x1 ;;; *2
    add  x1, x1 ;;; *4
    add  x1, x1 ;;; *8
    add  x1, x1 ;;; *16
    sub  x1, x2 ;;; result = Y*15
    sub  x1, x2 ;;; result = Y*14

(if you look at the instruction set manual, you'll discover that "ROL r" encodes to the same instruction as "ADC r,r", and "LSL r" encodes the same as "ADD r,r")

Thank you!

Yes SWAP work :)

some unrelated stuff from other included file was some error but strangely given error msg location to that line.

Thank you again for the help.