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. | Location | Contents | Label | Instruction | Comment |
|---|---|---|---|---|---|
| 0001 | 0000 | ;Z-80 Highest Factor Program | |||
| 0002 | 0000 | ;Donn Stewart | |||
| 0003 | 0000 | ;2/22/03 | |||
| 0004 | 0000 | ||||
| 0005 | 0800 | .org 0800h | ;Start of RAM--load program here. | ||
| 0006 | 0800 | DB 00 | strt | in a,(0) | ;Get number to factor from input port 0 |
| 0007 | 0802 | 32 21 08 | ld (original_number),a | ;Store it | |
| 0008 | 0805 | 32 22 08 | ld (test_factor),a | ;Use it as the initial test_factor | |
| 0009 | 0808 | 3A 22 08 | loop1 | ld a,(test_factor) | ;Main loop of program |
| 0010 | 080B | 3D | dec a | ;Make new test_factor by decrementing | |
| 0011 | 080C | 28 0E | jr z,end | ;If zero, quit (all done) | |
| 0012 | 080E | 32 22 08 | ld (test_factor),a | ;If not, store and test | |
| 0013 | 0811 | 47 | ld b,a | ;Put test_factor in reg b | |
| 0014 | 0812 | 3A 21 08 | ld a,(original_number) | ;Put original_number in reg a | |
| 0015 | 0815 | 90 | loop2 | sub b | ;Test by subtracting repeatedly |
| 0016 | 0816 | 28 04 | jr z,end | ;Exact factor--zero result | |
| 0017 | 0818 | 30 FB | jr nc,loop2 | ;Result non-negative, keep trying | |
| 0018 | 081A | 18 EC | jr loop1 | ;Result negative,not a factor | |
| 0019 | 081C | 78 | end | ld a,b | ;Display factor, or zero if prime |
| 0020 | 081D | D3 00 | out(0),a | ||
| 0021 | 081F | 18 DF | jr strt | ;Start over | |
| 0022 | 0821 | 00 | original_number | .byte 00h | |
| 0023 | 0822 | 00 | test_factor | .byte 00h | |
| 0024 | 0823 | .end |
© Donn Stewart 2004