home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol171 / list3.lst < prev    next >
Encoding:
File List  |  1984-05-30  |  5.3 KB  |  189 lines

  1. Screen # 8
  2.  
  3.  0 ( Eratosthenes Sieve Prime Number program in FORTH )
  4.  1 ( by Jim Gilbreath, BYTE September 1981 page 190 )
  5.  2 FORTH DEFINITIONS DECIMAL
  6.  3 8190 CONSTANT SIZE      0 VARIABLE FLAGS        SIZE ALLOT
  7.  4 
  8.  5 : DO-PRIME      FLAGS SIZE 1 FILL
  9.  6                 0 SIZE 0
  10.  7                 DO FLAGS I + C@
  11.  8                    IF I DUP + 3 + DUP I +
  12.  9                         BEGIN DUP SIZE <
  13. 10                         WHILE 0 OVER FLAGS + C! OVER + REPEAT
  14. 11                         DROP DROP 1+
  15. 12                    THEN
  16. 13                 LOOP
  17. 14                 .  ." primes " ;
  18. 15 ;S
  19.  
  20. Screen # 9
  21.  
  22.  0 ( Eratosthenes Sieve Prime Number program for M68K compiler )
  23.  1 ( Original by Jim Gilbreath, BYTE September 1981 page 190 )
  24.  2 DECIMAL 0 M68CON #0 ( Note.. 0 is used a lot in the following )
  25.  3 8190 CONSTANT SIZE      SIZE M68CON SIZE ( Both forms needed )
  26.  4 M68VAR FLAGS    SIZE M68ALLOT
  27.  5 
  28.  6 :M68MAC DO-PRIME FLAGS SIZE 1 LITERAL FILL
  29.  7                 #0 SIZE #0
  30.  8                 DO FLAGS I + C@
  31.  9                    IF I DUP + 3 LITERAL + DUP I +
  32. 10                         BEGIN DUP SIZE <
  33. 11                         WHILE #0 OVER FLAGS + C! OVER + REPEAT
  34. 12                         DROP DROP 1+
  35. 13                    THEN
  36. 14                 LOOP ;M68MAC
  37. 15 -->
  38.  
  39. Screen # 10
  40.  
  41.  0 ( Test program to run the prime number program )
  42.  1 HEX 7000. M68INIT ( Open the output file )
  43.  2 :M68MAC INIT ( Initialize all the registers )
  44.  3         800.    A5LD    ( Load variable pointer )
  45.  4         4000.   A6LD    ( Load data stack pointer )
  46.  5         7800.   A7LD    ( Load return stack pointer )
  47.  6 ;M68MAC
  48.  7 DECIMAL
  49.  8 :M68K TEST ( Run the prime number test ten times )
  50.  9         INIT
  51. 10         10 LITERAL #0 DO DO-PRIME DROP LOOP
  52. 11 ;M68K
  53. 12 M68END ( Close the output file )
  54. 13 ;S
  55. 14 
  56. 15 
  57.  
  58. Screen # 11
  59.  
  60.  0 ( Eratosthenes Sieve Prime Number program improved version )
  61.  1 ( Original by Jim Gilbreath, BYTE September 1981 page 190 )
  62.  2 DECIMAL 0 M68CON #0 ( Note.. 0 is used a lot in the following )
  63.  3 8190 CONSTANT SIZE      SIZE M68CON SIZE ( Both forms needed )
  64.  4 SIZE M68CARY FLAGS
  65.  5 
  66.  6 :M68MAC DO-PRIME #0 FLAGS SIZE 1 LITERAL FILL
  67.  7                 #0 SIZE #0
  68.  8                 DO I FLAGS C@
  69.  9                    IF I 2* 3 LITERAL + DUP I +
  70. 10                         BEGIN DUP SIZE <
  71. 11                         WHILE #0 OVER FLAGS C! OVER + REPEAT
  72. 12                         2DROP 1+
  73. 13                    THEN
  74. 14                 LOOP ;M68MAC
  75. 15 -->
  76.  
  77. Screen # 12
  78.  
  79.  0 ( Test program to run the prime number program )
  80.  1 HEX 7000. M68INIT ( Open the output file )
  81.  2 :M68MAC INIT ( Initialize all the registers )
  82.  3         800.    A5LD    ( Load variable pointer )
  83.  4         4000.   A6LD    ( Load data stack pointer )
  84.  5         7800.   A7LD    ( Load return stack pointer )
  85.  6 ;M68MAC
  86.  7 DECIMAL
  87.  8 :M68K TEST ( Run the prime number test ten times )
  88.  9         INIT
  89. 10         10 LITERAL #0 DO DO-PRIME DROP LOOP
  90. 11 ;M68K
  91. 12 M68END ( Close the output file )
  92. 13 ;S
  93. 14 
  94. 15 
  95.  
  96. ******************************************************************************
  97.  
  98.     .PROC   PRIME
  99. ;
  100. ;  Eratosthenes Sieve Prime Number program in M68000 assembly language.
  101. ;  This program provides a baseline for evaluating the performance of the
  102. ;  M68K compiler.
  103. ;
  104. ;  Register variables:
  105. ;       D0.. Temporary storage
  106. ;       D1.. Number of iterations
  107. ;       D2.. I - DO loop counter
  108. ;       D3.. P - candidate prime number
  109. ;       D4.. K - array index used with P
  110. ;       D5.. COUNT - number of primes
  111. ;       D6.. SIZE - size of the flags array
  112. ;
  113. ;       A0.. FLAGS - base address of the FLAGS array
  114. ;       A1.. Temporary address register used for initializing FLAGS
  115. ;
  116. ;
  117. ;  Note..
  118. ;       This program does not correspond exactly to the FORTH version but
  119. ;       the algorithm is the same so this should be a fair comparison.
  120. ;       The portions of the FORTH code which correspond to the sections of
  121. ;       assembler code are indicated in the comments.
  122. ;
  123. ;
  124. SIZE    .EQU    8190
  125. FLAGS   .EQU    800H            ;Base address of the FLAGS array
  126. ITER    .EQU    10              ;Number of iterations of the sieve
  127. ;
  128.     MOVE.W  #ITER,D1
  129.     MOVE.W  #SIZE,D6
  130.     MOVEA.W #FLAGS,A0
  131.     BRA.S   ENDIL           ;Enter the iteration loop at the proper place
  132. STARTIL                         ;Start of the iteration loop
  133. ;
  134. ;  FORTH code:
  135. ;       FLAGS SIZE 1 FILL
  136. ;
  137.     MOVEQ   #1,D0
  138.     MOVE.W  D6,D2           ;Load SIZE into D2
  139.     MOVEA.L A0,A1           ;Address of element of FLAGS to set
  140.     BRA.S   $02
  141. $01     MOVE.B  D0,(A1)+
  142. $02     DBRA    D2,$01
  143. ;
  144. ;  FORTH code:
  145. ;       0 SIZE 0 DO
  146. ;
  147.     CLR.W   D5              ;Clear prime counter
  148.     CLR.W   D2              ;Clear DO loop counter
  149. DOLOOP
  150. ;
  151. ;  FORTH code:
  152. ;       FLAGS I + C@ IF
  153. ;
  154.     BTST    #0,0(A0,D2.W)
  155.     BEQ.S   THEN            ;If false, skip the true part of IF structure
  156. ;
  157. ;  FORTH code:
  158. ;       I DUP + 3 + DUP I +
  159. ;
  160.     MOVE.W  D2,D3
  161.     ADD.W   D3,D3
  162.     ADDQ.W  #3,D3           ;P=I+I+3
  163.     MOVE.W  D3,D4
  164.     ADD.W   D2,D4           ;K=P+I
  165. ;
  166. ;  FORTH code:
  167. ;       BEGIN DUP SIZE <
  168. ;       WHILE 0 OVER FLAGS + C! OVER + REPEAT
  169. ;       DROP DROP 1+
  170. ;
  171. BEGIN   CMP.W   D6,D4
  172.     BGE.S   $03
  173.     CLR.B   0(A0,D4.W)
  174.     ADD.W   D3,D4
  175.     BRA.S   BEGIN
  176. $03     ADDQ.W  #1,D5           ;Update prime counter
  177. ;
  178. ;  FORTH code:
  179. ;          THEN
  180. ;       LOOP
  181. ;
  182. THEN    ADDQ.W  #1,D2
  183.     CMP.W   D6,D2
  184.     BLT.S   DOLOOP
  185. ;
  186. ;
  187. ENDIL   DBRA    D1,STARTIL      ;Repeat for requested number of iterations
  188.     .END
  189.