home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 181.img / TASM-101.ZIP / CHAPXMPL.ARC / PRIMES.ASM < prev    next >
Assembly Source File  |  1989-05-02  |  4KB  |  97 lines

  1. ;
  2. ; Sample 80386 code to calculate all primes between
  3. ; 0 and a specified value (inclusive).
  4. ;
  5. ; Input (assumes a large far call, with 6 bytes of return address
  6. ; pushed on the stack):
  7. ;
  8. ;    ESP+06h on entry (last parameter pushed) - the 
  9. ;    doubleword value of the maximum number to be checked as
  10. ;    to whether it is a prime.
  11. ;
  12. ;    ESP+0Ah on entry (first parameter pushed) - a large far
  13. ;    (6 byte offset) pointer to the table in which to store a
  14. ;    1 at the offset of each number that is a prime and a 0 at
  15. ;    the offset of each number that is not a prime. The table
  16. ;    must be at least [ESP+06h]+1 bytes in length, where
  17. ;    [ESP+06h] is the other parameter.
  18. ;
  19. ; Output: None
  20. ;
  21. ; Registers destroyed:
  22. ;    EAX, EBX, EDX, EDI
  23. ;
  24. ; Based on an algorithm presented in "Environments,"
  25. ; by Charles Petzold, PC Magazine, Vol. 7, No. 2.
  26. ;
  27.      .386
  28.  
  29. Code_Seg   SEGMENT   USE32
  30.      ASSUME    CS:Code_Seg
  31. CalcPrimes     PROC FAR
  32.      push es                             ;save caller's ES
  33.      push fs                             ;save caller's FS
  34. ;
  35. ; Get parameters.
  36. ;
  37.      mov  ecx,[esp+4+06h]
  38.      lfs  edx,[esp+4+0ah]
  39. ;
  40. ; Assume all numbers in the specified range are primes.
  41. ;
  42.      push fs
  43.      pop  es                             ;point ES to table's
  44.                                          ;segment
  45.      mov  al,1
  46.      mov  edi,edx
  47.      cld
  48.      push ecx                            ;save maximum number to
  49.                                          ;check
  50.      inc  ecx                            ;set up to maximum
  51.                                          ;number, inclusive
  52.      rep  stosb
  53.      pop  ecx                            ;get back maximum number
  54.                                          ;to check
  55. ;
  56. ; Now eliminate all numbers that aren't primes by calculating all
  57. ; multiples (other than times 1) less than or equal to the
  58. ; maximum number to check of all numbers up to the maximum number
  59. ; to check
  60. ;
  61.      mov  eax,2                          ;start with 2, since 0 &
  62.                                          ;1 are primes,
  63.                                          ; and can't be used for
  64.                      ;elimination of
  65.                                          ; multiples
  66. PrimeLoop:
  67.      mov  ebx,eax                        ;base value to calculate
  68.                                          ;all multiples of
  69. MultipleLoop:
  70.      add  ebx,eax                        ;calculate next multiple
  71.      cmp  ebx,ecx                        ;have we checked all
  72.                                          ;multiples of this
  73.                                          ; number?
  74.      ja   CheckNextBaseValue             ;yes, go to next number
  75.      mov  BYTE PTR fs:[edx+ebx],0        ;this number is not
  76.                                          ;prime, since
  77.                                          ; it's a multiple of
  78.                      ;something
  79.       jmp MultipleLoop                   ;eliminate the next
  80.                                          ;multiple
  81. CheckNextBaseValue:
  82.       inc eax                            ;point to next base
  83.                                          ;value (the
  84.                                          ; next value to
  85.                      ;calculate all
  86.                                          ; multiples of)
  87.       cmp eax,ecx                        ;have we eliminated all
  88.                                          ;multiples?
  89.       jb  PrimeLoop                      ;no, check the next set
  90.                                          ;of multiples
  91.       pop fs                             ;restore caller's FS
  92.       pop es                             ;restore caller's ES
  93.       ret
  94. CalcPrimes     ENDP
  95. Code_Seg       ENDS
  96.            END
  97.