home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / asm / wasm / primes.asm < prev    next >
Assembly Source File  |  1987-08-13  |  3KB  |  87 lines

  1.  
  2.  Title 'Wolfware Assembler Sample Program', 'Prime Number Generator'
  3.  
  4. ;=============================================================================
  5. ; Prime Number Generator. 
  6. ;
  7. ; This program will generate all the 16 bit prime numbers, i.e. between 1 and
  8. ; 65535.  The numbers 1, 2, and 3 are assumed to be prime and are skipped.  It
  9. ; takes about 18 minutes and 16 seconds on a standard PC to run the complete
  10. ; program.  The output can be saved by redirecting it to a file, for instance
  11. ; the following will generate and save the output to the file PRIME.LST:
  12. ;
  13. ;   PRIME > PRIME.LST
  14. ;
  15. ; The program works by calculating the primes in order and saving them in a
  16. ; a table.  Each prime candidate is divided by previously defined primes in 
  17. ; the table.  If a factor is found in the table, the candidate is discarded
  18. ; and the program goes on to the next number.  Even numbers are automatically
  19. ; skipped.
  20. ;
  21. ; For assembly, this program requires the files IO.MAC, DOS.MAC, IO.RED, and
  22. ; DISPATCH.ASM.  For execution, this program requires IO.BIN on the default
  23. ; drive/path.
  24.  
  25. ;================================================
  26. ; Main program.
  27.  
  28.  Mov Sp, Offset End     ;set stack
  29.  Reallocate Offset End  ;reallocate present memory
  30.  
  31.  Allocate 0ffffh ;allocate memory for list of primes
  32.  Mov Ds, Ax     ;set data segment
  33.  
  34. ;--- starting data, assume 3 is first prime and starting number is 5
  35.  
  36.  Mov Word [0], 3 ;load assumed prime number to table
  37.  Mov Di, 1      ;DI has number of primes in table, start with one
  38.  Mov Bx, 5      ;BX has prime candidate, start at 5
  39.  
  40. ;--- test if number in BX has factor in prime table
  41.  
  42. Main1
  43.  Mov Cx, Di     ;number of primes in table
  44.  Sub Si, Si     ;go to start of table
  45.  
  46. Main2
  47.  Mov Ax, Bx     ;number to check
  48.  Sub Dx, Dx     ;clear high part of dword
  49.  Mov Bp, [Si]   ;load prime
  50.  Div Ax, Bp     ;divide
  51.  Or Dx, Dx      ;check if remainder
  52.  Jz Main4       ;jump if so, not prime
  53.  
  54.  Add Si, 2      ;next entry
  55.  Loop Main2     ;loop back if more prime entries
  56.  
  57. ;--- factor not found in table, must be prime
  58.  
  59. Main3
  60.  Inc Di         ;another prime in table
  61.  Mov Si, Di     ;get entries
  62.  Shl Si         ;times two
  63.  Mov [Si], Bx   ;save new prime
  64.  
  65.  Decimal Bx     ;make decimal string
  66.  Display        ;display
  67.  Line           ;start new line
  68.  
  69. ;--- next number
  70.  
  71. Main4
  72.  Add Bx, 2      ;skip even number
  73.  Jnc Main1      ;jump if more
  74.  Exit
  75.  
  76. ;================================================
  77. ; External files and stack.
  78.  
  79.  Include 'Io.Mac'
  80.  Include 'Dos.Mac'
  81.  Include 'Io.Red'
  82.  Include 'Dispatch.Asm'
  83.  
  84.  Org +250       ;room for stack
  85. End
  86.  
  87.