/* Calculate the factorial of the number held in variable A. * The number in variable A must be greater than 0 and less than or * equal to 8! = 40,320 * Store factorial of A into 16 bit variable C. Byte ordering is little endian. * C = A! * Tip: Copy and paste subroutine muls16x8_24 into your assembly file. */ .INCLUDE .DSEG A: .BYTE 1 C: .BYTE 2 .CSEG Fact1_8: lds r22, A ; calculate r26! clr r25 clr r24 inc r24 ; r25:r24 = 0x0001 fact1_8a: rcall muls16x8_24 ; r4:r3:r2 = r25:r24 x r26 movw r25:r24, r3:r2 dec r22 brne fact1_8a sts C, r0 ; least significant byte (little end) sts C+1, r1 ; most significant byte (big end) rjmp Fact1_8 /* Multiply a 16-bit unsigned number in the r25:r24 register pair by * an 8-bit number in r22. Return the answer in r4:r3:r2 */ muls16x8_24: push r15 ; save contents of registers modified by this subroutine in r15, SREG push r24 push r1 push r0 clr r4 mul r24,r22 ; multiply LSB movw r3:r2,r1:r0 ; copy result to result register mul r25,r22 ; multiply MSB add r3,r0 adc r4,r1 pop r0 pop r1 pop r24 out SREG, r15 pop r15 ret