#include "c:\miniide\hcs12.inc" CR equ $0D LF equ $0A i equ 0 ; distance of variable i from stack top ilimit equ 2 ; distance of variable ilimit from stack top prime_flag equ 4 ; distance of variable isprime from stack top num equ 9 ; distance of variable num from the stack top plocal equ 5 ; number of bytes allocated to local variables upper equ 2000 ; upper limit for testing prime lower equ 1000 ; lower limit for testing prime org $1000 out_buf rmb 10 prime_cnt rmb 1 k rmb 2 temp rmb 2 org $1500 ldx #upper stx temp pshx ldx #lower stx k ; initialize k to 100 for prime testing pshx ldd #format0 jsr [printf,PCR] leas 4,sp clr prime_cnt again ldd k cpd #upper bhi done ; stop when k is greater than upper pshd jsr prime_tst ; test if k is prime leas 2,sp ; deallocate space used by outgoing parameters tstb beq next_k ; test next integer if k is not prime inc prime_cnt ; increment the prime count ldd k pshd ldd #format1 jsr [printf,PCR] ; output k leas 2,sp ldaa prime_cnt cmpa #8 ; are there 8 prime numbers in the current line? blo next_k ; output a CR, LF if there are already 8 prime numbers in the current line ldd #format2 jsr [printf,PCR] clr prime_cnt ldx k inx stx k bra again next_k ldx k inx stx k jmp again done swi prime_tst pshx leas -plocal,sp ; allocate local variable ldaa #1 staa prime_flag,sp ; set the flag to true ldd num,sp cpd #1 beq not_prime ; 1 is not a prime number cpd #2 beq is_prime ; 2 is a prime number lsrd ; compute num/2 as the test limit std ilimit,sp ; " ldd #2 std i,sp ; set first test number to 2 test_loop ldx i,sp ldd num,sp idiv cpd #0 ; if num can be divided by i, then beq not_prime ; it is not a prime ldd i,sp cpd ilimit,sp beq is_prime ; num is prime at the end of test loop ldx i,sp inx stx i,sp bra test_loop not_prime clr prime_flag,sp is_prime ldab prime_flag,sp ; put the result in B leas plocal,sp pulx rts format0 db CR,LF,"The prime numbers between %d and %d are as follows: " db CR,LF,CR,LF,0 format1 db " %d ",0 format2 db " ",CR,LF,0 end