Summary Files Commits Branches @commit @diff @stats
path: root / isprime / isprime.oph.s
blob: 618bd6d3b44a477f589cdde6743a20e9e17798c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
; .org is ONLY used for internal (assembler/label/alias/...) values, it doesn't output anything!
; So you cannot use .org to place data on a given location.

.outfile "isprime.prg"

.require "basic_starter.oph.s"
.require "kernal.oph.s"

.alias check_first 0
.alias check_last  254

main:
         `lowercase
         ;`primes_yn check_first, check_last
         `primes_list check_first, check_last
         rts

.macro primes_list
         ldx #_1         ; x = check_first
_loop:   jsr isprimex_print ; do  print isprime(x)
         inx             ;     ++x
         cpx #_2         ; while (x <= check_last)
         bcc _loop
         beq _loop
.macend

.macro primes_yn
         ldx #_1         ; x = check_first
_loop:   jsr isprimex_yn ; do  print isprime(x)
         inx             ;     ++x
         cpx #_2         ; while (x <= check_last)
         bcc _loop
         beq _loop
.macend

; prints x if it is a prime
isprimex_print:
         jsr isprimex  ; a = isprime(x)
         beq +         ; if (a != 0)
         jsr printx    ;   print x
         `printchar 32 ;   print " "
*        rts           ; return

; prints "$x Yes\n" when x is prime and "$x No\n" otherwise
isprimex_yn:
         jsr printx    ; print x
         `printchar 32 ; print " "
         jsr isprimex  ; a = isprime(number)
         beq +         ; if (a != 0)
         `print yes    ;   print "Yes"
         `printchar 13 ;   print "\n"
         rts           ;   return
*        `print no     ; else print "No"
         `printchar 13 ;   print "\n"
         rts           ;   return

max: .byte 0

; accumulator is zero if not prime and nonzero otherwise
isprimex:
.scope
         cpx #2     ; if (x < 2)   // x <= 1
         bcc _false ;   return False

         cpx #4     ; if (x < 4)   // x <= 3
         bcc _true

         txa        ; a = x
         and #1     ; if (a % 2 == 0)  // a & 0b0001 == 0
         beq _end   ;   return False

         txa        ; a = x
         `mod 3     ; if (a % 3 == 0)
         beq _end   ;   return False

         ldy #6     ; y = 6
         stx max    ; max = x
         lsr max    ; max /= 2

         cpy max    ; if (y >= max)
         bcs _true  ;   return True

_loop:   dey        ; do  --y
         txa        ;     a = x
         `mody      ;     if (a % y == 0)
         beq _end   ;       return False

         iny        ;     ++y
         iny        ;     ++y
         txa        ;     a = x
         `mody      ;     if (a % y == 0)
         beq _end   ;       return False

         iny        ;     ++y
         iny        ;     ++y
         iny        ;     ++y
         iny        ;     ++y
         iny        ;     ++y
         cpy max    ; while (y < max)
         bcc _loop

_true:   lda #1     ; return True
_end:    rts

_false:  lda #0     ; return False
         rts
.scend

.charmap 'A, "abcdefghijklmnopqrstuvwxyz"
.charmap 'a, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

yes:     .byte "Yes",0
no:      .byte "No",0