# ASM 4bit by 4bit multiplication by software

Hello

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.)
Hmm.

``````;; 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.

oqibidipo:
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