home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
assemblr
/
asm
/
wasm
/
primes.asm
< prev
next >
Wrap
Assembly Source File
|
1987-08-13
|
3KB
|
87 lines
Title 'Wolfware Assembler Sample Program', 'Prime Number Generator'
;=============================================================================
; Prime Number Generator.
;
; This program will generate all the 16 bit prime numbers, i.e. between 1 and
; 65535. The numbers 1, 2, and 3 are assumed to be prime and are skipped. It
; takes about 18 minutes and 16 seconds on a standard PC to run the complete
; program. The output can be saved by redirecting it to a file, for instance
; the following will generate and save the output to the file PRIME.LST:
;
; PRIME > PRIME.LST
;
; The program works by calculating the primes in order and saving them in a
; a table. Each prime candidate is divided by previously defined primes in
; the table. If a factor is found in the table, the candidate is discarded
; and the program goes on to the next number. Even numbers are automatically
; skipped.
;
; For assembly, this program requires the files IO.MAC, DOS.MAC, IO.RED, and
; DISPATCH.ASM. For execution, this program requires IO.BIN on the default
; drive/path.
;================================================
; Main program.
Mov Sp, Offset End ;set stack
Reallocate Offset End ;reallocate present memory
Allocate 0ffffh ;allocate memory for list of primes
Mov Ds, Ax ;set data segment
;--- starting data, assume 3 is first prime and starting number is 5
Mov Word [0], 3 ;load assumed prime number to table
Mov Di, 1 ;DI has number of primes in table, start with one
Mov Bx, 5 ;BX has prime candidate, start at 5
;--- test if number in BX has factor in prime table
Main1
Mov Cx, Di ;number of primes in table
Sub Si, Si ;go to start of table
Main2
Mov Ax, Bx ;number to check
Sub Dx, Dx ;clear high part of dword
Mov Bp, [Si] ;load prime
Div Ax, Bp ;divide
Or Dx, Dx ;check if remainder
Jz Main4 ;jump if so, not prime
Add Si, 2 ;next entry
Loop Main2 ;loop back if more prime entries
;--- factor not found in table, must be prime
Main3
Inc Di ;another prime in table
Mov Si, Di ;get entries
Shl Si ;times two
Mov [Si], Bx ;save new prime
Decimal Bx ;make decimal string
Display ;display
Line ;start new line
;--- next number
Main4
Add Bx, 2 ;skip even number
Jnc Main1 ;jump if more
Exit
;================================================
; External files and stack.
Include 'Io.Mac'
Include 'Dos.Mac'
Include 'Io.Red'
Include 'Dispatch.Asm'
Org +250 ;room for stack
End