/* A factorial programs you have written up to this point have not * allowed an input of 1. Modify your factorial program to return 1 * if the input is 0 and turn on the green LED wired to Port B bit 0 * on the CSULB shield if the input is greater than 8. */ .INCLUDE .DSEG A: .BYTE 1 B: .BYTE 1 C: .BYTE 2 D: .BYTE 2 .CSEG .ORG 0x0000 RST_VECT: rjmp setup .ORG 0x0100 ; bypass IVT .INCLUDE "spi_shield.inc" setup: rcall initShield ; initialize the CSULB shield Fact0_8: lds r26, A ; calculate r26! clr r25 clr r24 inc r24 ; r25:r24 = 0x0001 cbi PortB,0 ; turn OFF error LED movw r1:r0,r25:r24 ; guess input is 0 tst r26 breq fact0_8b cpi r26, 9 ; if input >= 9 (complement of test is input < 9) brlo fact0_8a ; error sbi PortB,0 ; then turn ON the error LED rjmp fact0_8b fact0_8a: ; else, calculate factorial rcall muls16x8_24 ; r4:r3:r2 = r25:r24 x r26 movw r25:r24, r3:r2 dec r26 brne fact0_8a fact0_8b: sts C, r0 ; least significant byte (little end) sts C+1, r1 ; most significant byte (big end) rjmp Fact0_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