home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / misc / jetprime.lbr / JETPRIME.ZZ0 / JETPRIME.Z80
Text File  |  1987-01-24  |  3KB  |  150 lines

  1. ; JetPrime.Z80
  2. ;
  3. ; Z80 Seive Benchmark.
  4. ; Bridger Mitchell, Plu*Perfect Systems 1/13/87
  5. ; Translated for assembly with ZAS or Z80ASM
  6. ; by Bruce Morgen, North American One-Eighty Group
  7. ; 10:24:28 January 22, 1987.
  8. ;
  9. ; Count the number of primes in [0...SIZE].
  10. ; Z80 hand-coded ASM version of BYTE benchmark.
  11. ;
  12. ; Remark.  I put this together after receiving the Borland
  13. ; Turbo Modula-2 PRIME.MOD file, to get some indication of
  14. ; the relative gain from hand-coding over a using compiler with
  15. ; an early reputation for fast code.
  16. ;
  17. ; I believe it's a fair test, as the high-level language
  18. ; implementations of the benchmark also use compiled constants.
  19. ; However, my routine doesn't call a formatted decimal output routine,
  20. ; and it would be best to remove that from all comparative tests,
  21. ; anyway -- compilers vary a great deal in the overhead required
  22. ; to format.
  23. ;
  24. ; Doubtless someone has done this before ... it was faster to
  25. ; write a few lines than dig into back magazine and newsletter issues.
  26. ;
  27. ; This source file was assembled with the Echelon ZAS assembler.
  28. ; Syntax:    ZAS JETPRIME H    (followed by)
  29. ;        MLOAD JETPRIME
  30.  
  31. ; Allocate flags[] so that flags[SIZE+1] begins on a new page.
  32. ; Use high-byte compare to terminate loops.
  33. ;
  34. SIZE    EQU    8190        ; Largest integer to be checked
  35. ANSWER    EQU    1899        ; Number of primes in [0...SIZE]
  36. ;
  37. TOP    EQU    FREE + [SIZE + [100H - [00FFH AND SIZE]]]
  38. ;
  39. PAGEND    EQU    [HIGH TOP] AND 00FFH ; Page following flag[SIZE]
  40. ;
  41. FLAGS    EQU    TOP - [SIZE +1]    ; There are 0...SIZE = SIZE+1
  42. ;
  43. BDOS    EQU    5
  44. CR    EQU    0DH
  45. LF    EQU    0AH
  46. ;
  47. NITER    EQU    10        ; Number of iterations, for timing.
  48. ;
  49.     ORG    100H
  50. ;
  51. ; use CCP stack
  52. ;
  53. TEST:    LD    DE,SIGNON    ; Banner & prompt
  54.     LD    C,9
  55.     CALL    BDOS
  56.     CALL    WAITCR        ; Wait for CR
  57. ;
  58.     LD    A,NITER        ; Set # iterations'
  59. MORE:    PUSH    AF        ;
  60.     CALL    PRIMES
  61.     POP    AF
  62.     DEC    A
  63.     JP    NZ,MORE
  64. ;
  65.     LD    HL,ANSWER    ; Verify answer
  66.     SBC    HL,BC        ; (cy clear)
  67.     LD    DE,DONMSG
  68.     JR    Z,SAYEND    ; Z - it's correct
  69.     LD    DE,BADMSG
  70. SAYEND:    LD    C,9
  71.     CALL    BDOS
  72. ;
  73. EXIT:    LD    DE,RETMSG    ; Ask for CR again
  74.     LD    C,9        ; And exit to CCP
  75.     CALL    BDOS
  76. ;
  77. WAITCR:    LD    C,6        ; Wait
  78.     LD    E,0FFH
  79.     CALL    BDOS
  80.     CP    CR
  81.     JR    NZ,WAITCR
  82.     LD    E,A        ; Echo a CR
  83.     LD    C,2
  84.     JP    BDOS
  85. ;
  86. SIGNON:    DB    CR,LF
  87.     DB    'JetPrime Z80 -- BYTE Seive Benchmark - 10 iterations'
  88. RETMSG:    DB    CR,LF
  89.     DB    'Hit <RETURN>.$'
  90. ;
  91. DONMSG:    DB    CR,LF
  92.     DB    '1899 Primes$'
  93. ;
  94. BADMSG:    DB    CR,LF
  95.     DB    'Wrong!$'
  96. ;
  97. ;
  98. PRIMES:    LD    HL,FLAGS    ; Set all flags TRUE
  99.     LD    DE,FLAGS+1
  100.     LD    BC,SIZE
  101.     LD    (HL),01H
  102.     LDIR
  103. ;
  104. ; initialize registers
  105.                 ; Bc = count = 0 already
  106.     LD    D,B        ; De = i = 0
  107.     LD    E,B
  108.     LD    HL,FLAGS    ; &flags[0]
  109.     EXX            ; Bc' = &flags[0]
  110.     LD    BC,FLAGS
  111.     EXX
  112.     LD    A,PAGEND-1    ; Last page of flags[]
  113. ;
  114. LP1:    BIT    0,(HL)        ; If flag[i] == TRUE
  115.     JP    Z,NEXT
  116. ;
  117.     PUSH    DE        ; Set up for inner loop
  118.     EXX            ; & count this prime
  119.     POP    HL        ; Hl' = i
  120.     LD    D,H        ; De' = i
  121.     LD    E,L
  122.     ADD    HL,HL        ; *2
  123.     INC    HL
  124.     INC    HL
  125.     INC    HL        ; +3
  126.     EX    DE,HL        ; De' = prime = i + i + 3, hl' = i
  127.     ADD    HL,DE        ; Hl' = k = prime + i
  128.     ADD    HL,BC        ; Hl' = &flag[k]
  129. ;
  130. LP2:    CP    H        ; While k <= SIZE
  131.     JR    C,LP2X
  132.     RES    0,(HL)        ; Flag[k] = FALSE
  133.     ADD    HL,DE        ; Hl' = k = hl' + prime
  134.     JP    LP2
  135. ;
  136. LP2X:    EXX            ; Count the prime
  137.     INC    BC        ; Count++
  138. ;
  139. ; do next i
  140. ;
  141. NEXT:    INC    HL        ; &keys[]++
  142.     INC    DE        ; I++
  143.     CP    H
  144.     JP    NC,LP1
  145.     RET            ; Bc = count of primes
  146. ;
  147.     DS    [100H -    [$ AND 00FFH]]
  148. FREE:    DS    0
  149.     END
  150.