Created
March 24, 2014 13:50
-
-
Save soharu/9740439 to your computer and use it in GitHub Desktop.
Print table of 500 primes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Algorithm P: Print table of 500 primes | |
L IS 500 % The number of primes to find | |
t IS $255 % Temporary storage | |
n GREG 0 % Prime candidate | |
q GREG 0 % Quotient | |
r GREG 0 % Remainder | |
jj GREG 0 % Index for PRIME[j] | |
kk GREG 0 % Index for PRIME[k] | |
pk GREG 0 % Value of PRIME[k] | |
mm IS kk % Index for output lines | |
LOC Data_Segment | |
PRIME1 WYDE 2 % PRIME[1] = 2 | |
LOC PRIME1+2*L | |
ptop GREG @ % Address of PRIME[501] | |
j0 GREG PRIME1+2-@ % Initial value of jj | |
BUF OCTA 0 % Place to form decimal string | |
LOC #100 | |
Main SET n,3 % P1. Start table. n <- 3 | |
SET jj,j0 % j <- 1 | |
2H STWU n,ptop,jj % P2. n is prime. PRIME[j+1] <- n | |
INCL jj,2 % j <- j + 1 | |
3H BZ jj,2F % P3. 500 found? | |
4H INCL n,2 % P4. Advance n | |
5H SET kk,j0 % P5. k <- 2 / k = ptop - kk | |
6H LDWU pk,ptop,kk % P6. n / PRIME[k] ? | |
DIV q,n,pk % q <- floor(n / PRIME[k]) | |
GET r,rR % r <- n mod PRIME[k] | |
BZ r,4B % To P4 if r = 0 for checking the next n | |
7H CMP t,q,pk % P7. PRIME[k] large? | |
BNP t,2B % To P4 if q <= PRIME[k] | |
8H INCL kk,2 % P8. Advance k. k <- k + 1 | |
JMP 6B % To P6. | |
GREG @ % Base address | |
Title BYTE "First Five Hundred Primes" | |
NewLn BYTE #a,0 | |
Blanks BYTE " ",0 % String of three blanks | |
2H LDA t,Title % P9. Print Title | |
TRAP 0,Fputs,StdOut | |
NEG mm,2 | |
3H ADD mm,mm,j0 % P10. Print line | |
LDA t,Blanks % Output " ". | |
TRAP 0,Fputs,StdOut | |
2H LDWU pk,ptop,mm % pk <- prime to be printed | |
0H GREG #2030303030000000 % " 0000",0,0,0 | |
STOU 0B,BUF % Prepare buffer for decimal conversion | |
LDA t,BUF+4 % t <- position of units digit | |
1H DIV pk,pk,10 % pk <- floor(pk/10) | |
GET r,rR % r <- next digit | |
INCL r,'0' % r <- ASCII digit r | |
STBU r,t,0 % Store r in the buffer | |
SUB t,t,1 % Move one byte to the left | |
PBNZ pk,1B % Repeat on remaining digits | |
LDA t,BUF % Output " " and four digits | |
TRAP 0,Fputs,StdOut | |
INCL mm,2*L/10 % Advance by 50 wydes | |
PBN mm,2B | |
LDA t,NewLn % Output a newline | |
TRAP 0,Fputs,StdOut | |
CMP t,mm,2*(L/10-1) % P11. 500 printed? | |
PBNZ t,3B % To P10 if not done | |
TRAP 0,Halt,0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment