; File: isLeapyear.asm ; Purpose: ; To test a function to see if a given year is a leap year ; .ORIG x3000 TEST LD R2, YEAR ; R3 = 1 -> LY JSR isLeapYear HALT YEAR .FILL 2024 ; ; isLeapyear(R2): ; returns in R3 (=1, if fleap year, =0 o/w) ; ; Algorithm: ; A year is a leap year if divisible by 4 but not by 100 ; or if divisible by 400 isLeapYear ; Save resgisters used ST R0, ILR0 ST R1, ILR1 ST R7, ILR7 AND R3, R3, #0 ; R3 <- 4 ADD R3, R3, #4 JSR DIVMOD ; R0 <- Q, R1 <- Remainder ; if R1 == 0 (if R2 is divisible by 4) ADD R1, R1, #0 BRnp DoneFalse ; then if R2 is not divisible by 100 LD R3, ONEH JSR DIVMOD ; R0 <- Q, R1 <- Remainder ADD R1, R1, #0 ; then BRp DoneTrue ; if R2 is divisible by 400 LD R3, FOURH JSR DIVMOD ; R0 <- Q, R1 <- Remainder ; if R1 == 0 (if R2 is divisible by 4) ADD R1, R1, #0 BRz DoneTrue DoneFalse AND R3, R3, #0 ADD R3, R3, #0 BR Done DoneTrue AND R3, R3, #0 ADD R3, R3, #1 Done LD, R0, ILR0 LD R1, ILR1 LD R7, ILR7 RET ONEH .FILL #100 FOURH .FILL #400 ILR0 .BLKW 1 ILR1 .BLKW 1 ILR7 .BLKW 1 ; ; DIVMOD ; R2 contains dividend (A), R3 contains divisor (B) ; Result is put in R0 (Quotient) and R1 (remainder) ; R4 is used to store -R3 ; R5 is used to store the result of subtraction (A - B) ; Registers: R0 and R1 will return results. ; DIVMOD ; save registers used ST R4, DMR4 ST R5, DMR5 ST R7, DMR7 ADD R1, R2, #0 ; R1 = R2 (A) NOT R4, R3 ; R4 = -R3: R4 = NOT(R3) and Adding 1 to R4 ADD R4, R4, #1 AND R0, R0, #0 ; R0 = 0 ; while R1 >= R3 : R5 = R1 - R3 : R5 = R1 + R4 LOOP ADD R5, R1, R4 BRn DMDONE ; do ADD R1, R1, R4 ; R1 = R1 - R3 ADD R0, R0, #1 ; R0 = R0 + 1 BR LOOP DMDONE LD R4, DMR4 LD R5, DMR5 LD R7, DMR7 RET DMR4 .BLKW 1 DMR5 .BLKW 1 DMR7 .BLKW 1 .END