John West wrote:
This will be a good test for my 65020. I'll have a go at implementing both algorithms tonight. The simulator gives an approximate cycle count, so we can do hand-wavy comparisons.
That was fun. I've got a 65020 translation of the second Pascal version running. The source is below, although it isn't pretty.
The times are only estimates, and it's very possible it's counting them wrong. I've assumed that mul, div, and mod take the usual cycles to fetch opcode and operands, plus one cycle per bit (that doesn't sound unreasonable for mid-1980s technology). The C640 will probably end up running at 5MHz (which also doesn't sound unreasonable for an improved Commodore 64), and I've translated the cycle counts with that assumption
A: 194368 cycles = 39ms
B: 331858 cycles = 66ms
C: 4603325 cycles = 0.92s
D: 12183830 cycles = 2.44s
E: 23183225 cycles = 4.64s
F: 700264537 cycles = 140.05s
Code:
Input file primes.asm:
= 000003e8 1 range = 1000
= 00000014 2 mindiff = 20
3 ;range = 2000
4 ;mindiff = 30
5 ;range = 9999
6 ;mindiff = 35
7 ;range = 32000
8 ;mindiff = 50
9 ;range = 32000
10 ;mindiff = 70
11 ;range = 500000
12 ;mindiff = 100
13
14 * = $c000
15
0000c000: 00a9 0015 16 lda #$15
0000c002: 0085 d018 17 sta $d018
0000c004: 00a9 0006 18 lda #6
0000c006: 0085 d021 19 sta $d021
20
0000c008: 00a9 0020 21 lda #32
0000c00a: 20a9 000e 22 lda a1, #14
0000c00c: 01a2 03e7 23 ldx.w #999
0000c00e: 24 clear
0000c00e: 0095 0400 25 sta $0400,x
0000c010: 2095 d800 26 sta a1, $d800, x
0000c012: 01ca 27 dex.w
0000c013: 0010 00f9 28 bpl clear
29
30
0000c015: 02a5 df00 31 lda.l $df00
0000c017: 0285 c04f 32 sta.l startTime
33
= 00000001 34 prim0 = 1
= 00000002 35 incr = 2
36
0000c019: 02a2 0001 0000 37 ldx.l x0, #prim0 ; x0 = cnt
0000c01c: 22a2 0001 0000 38 ldx.l x1, #prim0 ; x1 = loprim
0000c01f: 42a2 0001 0000 39 ldx.l x2, #prim0 ; x2 = hiprim
40
0000c022: 41 loop
0000c022: 22e0 00e8 0003 42 cpx.l x1, #range
0000c025: 8010 002a 43 bge loopEndFail
0000c027: 428a 44 mov.l a0, x2
0000c028: 96fc 45 sub.l a0, x1 ; a0 = hiprim - loprim
0000c029: 02c9 0014 0000 46 cmp.l a0, #mindiff
0000c02c: 8010 0009 47 bge loopEndPass
48
0000c02e: 22e8 49 inx.l #incr, x0
0000c02f: 90f0 003b 50 bra.l prim
0000c031: 00f0 00ef 51 beq loop
0000c033: 5e9a 52 mov.l x1, x2
0000c034: 129a 53 mov.l x2, x0
0000c035: 80f0 00eb 54 bra loop
55
0000c037: 56 loopEndPass
0000c037: 01a0 040a 57 ldy.w y0, #$0400 + 10
0000c039: 3a9a 58 mov.l x0, x1
0000c03a: 0020 0083 00c0 59 jsr printNum
0000c03d: 01a0 0432 60 ldy.w y0, #$0400 + 40 + 10
0000c03f: 5a9a 61 mov.l x0, x2
0000c040: 0020 0083 00c0 62 jsr printNum
0000c043: 02a6 df00 63 ldx.l x0, $df00
0000c045: 92eb 004f 00c0 64 sbx.l x0, startTime
0000c048: 01a0 045a 65 ldy.w y0, #$0400 + 80 + 10
0000c04a: 0020 0083 00c0 66 jsr printNum
0000c04d: 80f0 001b 67 bra done
68
0000c04f: 69 startTime
0000c04f: 0000 0000 70 .long 0
71
0000c051: 72 loopEndFail
0000c051: 02a9 0058 00c0 73 lda.l a0, #failMessage
0000c054: 90f0 0007 74 bra.l print
0000c056: 80f0 0012 75 bra done
76
0000c058: 77 failMessage
0000c058: 0006 0001 0009 000c 78 .byte 6, 1, 9, 12, 0 ; "FAIL"
0000c05c: 0000
79
0000c05d: 80 print
0000c05d: 00a2 0000 81 ldx #0
0000c05f: 82 printLoop
0000c05f: 30b5 0000 83 lda a1, 0, a0
0000c061: 00f0 0006 84 beq printDone
0000c063: 2095 0400 85 sta a1, $0400, x
0000c065: 00e8 86 inx
0000c066: 12e8 87 inx.l a0
0000c067: 80f0 00f6 88 bra printLoop
0000c069: 89 printDone
0000c069: 0060 90 rts
91
0000c06a: 92 done
0000c06a: 80f0 00fe 93 bra done
94
95 ; input X0
96 ; output A0
0000c06c: 97 prim
0000c06c: 2048 98 pha a1
0000c06d: 20a9 0003 99 lda a1, #3 ; a1 = i
0000c06f: 100 primLoop
0000c06f: 32a8 101 mov.l a0, a1
0000c070: 0283 102 mul.l a0, a0
0000c071: 12dc 103 cmp.l a0, x0
0000c072: 8010 000b 104 bge primEndTrue
0000c074: 028a 105 mov.l a0, x0
0000c075: 06a3 106 mod.l a0, a1
0000c076: 00f0 0003 107 beq primEndFalse
0000c078: 36e8 108 inx.l #2, a1
0000c079: 80f0 00f4 109 bra primLoop
0000c07b: 110 primEndFalse
0000c07b: 2068 111 pla a1
0000c07c: 00a9 0000 112 lda #0
0000c07e: 0060 113 rts
0000c07f: 114 primEndTrue
0000c07f: 2068 115 pla a1
0000c080: 00a9 0001 116 lda #1
0000c082: 0060 117 rts
118
0000c083: 119 printNum
0000c083: 068a 120 mov.l a1, x0
0000c084: 121 printNumLoop
0000c084: 32a8 122 mov.l a0, a1
0000c085: 02a7 000a 0000 123 mod.l a0, #10
0000c088: 8069 0030 124 add a0, #48
0000c08a: 1085 0000 125 sta 0, y0
0000c08c: 0188 126 dey.w y0
0000c08d: 2297 000a 0000 127 div.l a1, #10
0000c090: 00d0 00f2 128 bne printNumLoop
0000c092: 0060 129 rts
130