home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 9.ddi / CHAPXMPL.ZIP / PRIMES.ASM < prev    next >
Encoding:
Assembly Source File  |  1991-02-13  |  3.8 KB  |  101 lines

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