Today I was explaining to a AVR ASM novice about math asm routines, while he told me one unsigned mult 8x8 routine he copied from the net was failing all the time.
Reading his routine from the net, I noticed it was to fail completely. Then I start to dig google for 8x8 routines and found the next, that also fails.
I can not understand how a "coder" can post a routine with quantity of cycles and instructions, etc, looking like as a nice tested routine, but doesn't work at all.
What should we do? contact the coder to fix it? or just ignore it, and leave the bait for other novices end up with headaches for not understanding why it fails?
mul08u: sub var11,var11 ;initialize variables ldi lc,9 ; lc = 9; brcc PC+2 ;---- calcurating loop add var11,var20 ; ror var11 ; ror var10 ; dec lc ;if (--lc > 0) brne PC-5 ; continue loop; ret
You don't even need to read all the routine to see it will fail.
Just see the two ROR instructions over the result right before it could end the loop by "lc" reaches zero.
It means that the result will always be half of the real result should be.
If you are multiplying 0x01 times 0x80, the result will be x040.
Although, I like the idea of a ROR following the ADD, since it also retrieves the lost carry bit into the shift.