The ";" symbol introduces a comment below.
When I gave this as an assignment question in a past year, the students were
not required to be as comprehensive as the following solution in saving
registers, especially in dealing with the way the register restores clear the
overflow flag.
ROUTINE:
; save registers
MOV R1, -(R6)
MOV R2, -(R6)
MOV R3, -(R6) ; used implicitly by the DIV!
; start with n=N-1
DEC R0
OLOOP: ; compute (n**2+n)/2 into R2
; must DIV into an even register; easiest to MUL into an odd register
MOV R0, R1
MUL R0, R1
BVS OVERFLOW
ADD R0, R1
BVS OVERFLOW
MOV R1, R2
DIV #2, R2
; subtract 1..n from R2
; descending loop index is kept in R1
MOV R0, R1
ILOOP: SUB R1, R2
BVS COUNTER ; an overflow at this point indicates a counterexample
SOB R1, ILOOP
; subtracting 0 from R2 is unnecessary
; we've now computed RHS-LHS into R2
; cond codes represent last SUB as SOB does not affect them
BNE COUNTER
SOB R0, OLOOP
; no counterexamples found for the 1..N-1 cases.
; The 0 case has been verified by hand!
; Since the last result was zero (see BNE), we know that C and V are clear.1
MOV (R6)+, R3
MOV (R6)+, R2
MOV (R6)+, R1
RTS R7
COUNTER:; counterexample found
; the n is in R0
; we must clear V, but these MOVs will do it
MOV (R6)+, R3
MOV (R6)+, R2
MOV (R6)+, R1
SEC
RTS R7
OVERFLOW:; overflowed when calculating n**2+n; not easily avoided
; V flag is set when we get here, but will be cleared by the register restores
MOV (R6)+, R3
MOV (R6)+, R2
MOV (R6)+, R1
SEV
RTS R7
1 To be precise, also since it wasn't -32768 + -32768.
[on to problem #6]