/* Calculate A^3 where A is an 8-bit unsigned variable. * The result is placed into 24-bit variable C. * The 24-bit result is saved using little endian byte ordering. * C = A^3 * * Solution: To find the cube of A, I begin by finding the square of A, * load calling arguments needed by muls16x8_24 using the * move word instruction (movw), call muls16x8_24 * subroutine, and finally save the result into C. * */ .INCLUDE .DSEG A: .BYTE 1 C: .BYTE 3 .CSEG A_Cubed: lds r26,A ; load mul r26,r26 movw r25:r24, r1:r0 rcall muls16x8_24 sts C,r2 ; least significant byte (little end) sts C+1,r3 ; most significant byte (big end) sts C+2, r4 rjmp A_Cubed /* Multiply a 16-bit unsigned number * in the r25:r24 register pair by an 8-bit number in r26. * return the answer in r4:r3:r2 * * Multiply * r25:r24 * x r26 * ---------- * 0:r3:r2 = r16*r18 first byte * + r1:r0 = r17*r18 second byte * -------- * r4:r3:r2 = (r1 + c):(r3 + r0):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,r26 ; multiply LSB movw r3:r2,r1:r0 ; copy result to result register mul r25,r26 ; multiply MSB add r3,r0 adc r4,r1 pop r0 pop r1 pop r24 out SREG, r15 pop r15 ret