;Z-80 Highest Factor Program
;Donn Stewart
;2/22/03
  
		.org     0800h		;Start of RAM--load program here.
strt    	in      a,(0)		;Get number to factor from input port 0
    		ld      (original_number),a	;Store it
    		ld      (test_factor),a	;Use it as the initial test_factor
loop1   	ld      a,(test_factor)	;Main loop of program
		dec     a		;Make new test_factor by decrementing
		jr	z,end		;If zero, quit (all done)
		ld      (test_factor),a	;If not, store and test
		ld      b,a		;Put test_factor in reg b
		ld      a,(original_number);Put original_number in reg a
loop2   	sub     b		;Test by subtracting repeatedly
		jr      z,end		;Exact factor--zero result		
		jr      nc,loop2	;Result non-negative, keep trying
		jr      loop1		;Result negative, not a factor
end     	ld      a,b		;Display factor, or zero if prime
		out     (0),a
		jr      strt		;Start over
original_number .byte	00h
test_factor     .byte	00h
	.end