Integer Factor Program for the Z80 Computer

This program finds the highest factor of an integer. The integer is entered on the input port, and the highest factor is found and displayed on the output port. The integer factor routine was the first program ever run on a stored program computer (the Manchester Mark I "Baby" ).

Line no.LocationContentsLabelInstructionComment
00010000 ;Z-80 Highest Factor Program
00020000 ;Donn Stewart
00030000 ;2/22/03
00040000 0800h;Start of RAM--load program here.
00060800DB 00strtin a,(0);Get number to factor from input port 0
0007080232 21 08ld (original_number),a;Store it
0008080532 22 08ld (test_factor),a;Use it as the initial test_factor
000908083A 22 08loop1ld a,(test_factor);Main loop of program
0010080B3Ddec a;Make new test_factor by decrementing
0011080C28 0Ejr z,end;If zero, quit (all done)
0012080E32 22 08ld (test_factor),a;If not, store and test
0013081147ld b,a;Put test_factor in reg b
001408123A 21 08ld a,(original_number);Put original_number in reg a
0015081590loop2 sub b;Test by subtracting repeatedly
0016081628 04jr z,end;Exact factor--zero result
0017081830 FBjr nc,loop2;Result non-negative, keep trying
0018081A18 ECjr loop1;Result negative,not a factor
0019081C78end ld a,b;Display factor, or zero if prime
0020081DD3 00out(0),a
0021081F18 DFjr strt;Start over
0022082100original_number.byte 00h
0023082200test_factor.byte 00h

Return to main Z-80 computer page.

© Donn Stewart 2004