Created
November 17, 2010 20:24
-
-
Save danwoods/704013 to your computer and use it in GitHub Desktop.
Computes all the prime numbers between 2 and a given number.
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
;Dan Woodson | |
;Check all primes | |
; This program computes all the prime numbers between 2 (count -1) and a given number (limit). | |
; The total number of primes currently found is stored in prime_count | |
; As primes are found they're displayed in OUTR | |
JMP _start | |
; Variables | |
limit: 6 ; range to check (count, limit) | |
count: 3 ; begin checking @ (usually set to 3) | |
prime_count: 0 ; current number of primes found | |
temp_count: 0 | |
sentinal: e ; range to check for negatives (is actually set within program) | |
sent_count: 0 ; temp count | |
register: 0 ; extra reg | |
neg_flag: 0 ; flag: set in _check_if_neg function | |
orig_value: 0 ; extra reg | |
;;;Functions;;; | |
;;;;;;;;;;;;;;;;;CHECK IF NEG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
_check_if_neg | |
;store acc | |
STOAC register | |
STOAC orig_value | |
;set sentinal to be at most the current temp_count | |
LDA temp_count | |
STOAC sentinal | |
;setup sent_count and neg_flag (clear) | |
CLAC | |
STOAC sent_count | |
STOAC neg_flag | |
_begin_neg_loop | |
;restore value to ACC | |
LDA register | |
; increment and see if result equals zero, if so, orig_value was negative | |
INC | |
STOAC register | |
JMPZ _set_flag | |
;increment loop count | |
LDA sent_count | |
INC | |
STOAC sent_count | |
;if sent_count = sentinal: break/return | |
MOVAC | |
LDA sentinal | |
SUB | |
JMPZ _return_check_if_neg | |
JMP _begin_neg_loop | |
_set_flag | |
INC | |
STOAC neg_flag | |
LDA orig_value ; restore value | |
;return | |
JMP _return_check_if_neg | |
;;;;;;;;;;;;;;;;;;;;CHECK PRIME WHILE;;;;;;;;;;;;;;;;;;;;;;; | |
_check_prime_while | |
STOAC register | |
LDA temp_count | |
MOVAC | |
LDA register | |
SUB | |
JMPZ _return_check_prime ; if zero, non-prime. return to main | |
JMP _check_if_neg ; otherwise, check sign | |
_return_check_if_neg | |
CLAC ; check if neg_flag set | |
MOVAC | |
LDA neg_flag | |
OR | |
JMPZ _return_check_prime_while ; if neg_flag not set (pos), return to check_prime_loop | |
; if neg, prime for this #(count) | |
LDA temp_count ; increment temp_count | |
INC | |
STOAC temp_count | |
MOVAC ; if temp_count == count, this #(count) is fully prime, move to prime_found | |
LDA count | |
SUB | |
JMPZ _prime_found | |
LDA count ; else, reload count and restart loop | |
JMP _begin_check_prime_loop | |
;;;;;;;;;;;;;;;;;;;;CHECK PRIME;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
_check_prime | |
CLAC ; set temp count to 2 | |
INC | |
INC | |
STOAC temp_count | |
LDA count | |
_begin_check_prime_loop | |
JMP _check_prime_while ; while (count-temp_count) !== temp_count | |
_return_check_prime_while | |
LDA orig_value | |
JMP _begin_check_prime_loop ;restart loop | |
;;;;;;;;;;;;;;;;;;;;;;PRIME FOUND;;;;;;;;;;;;;;;;;;;;;;; | |
_prime_found | |
LDA count | |
OUT | |
LDA prime_count | |
INC | |
STOAC prime_count | |
JMP _return_check_prime | |
;;;;;;;;;;;;;;;;;;;;;LIMIT GT COUNT;;;;;;;;;;;;;;;;;;;;; | |
_limit_GT_count | |
LDA count ; Load the first variable into Accumulator | |
MOVAC ; Move the contents of Accumulator to R | |
LDA limit ; Load the second variable into Accumulator | |
SUB ; Subtract count from limit | |
MOVAC ; move answer to R | |
CLAC ; clear ACC | |
OR ; OR answer with zero, return | |
JMP _return_limit_GT_count; return | |
;;;;;;;;;;;;;;;;;;;;INCREMENT COUNT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
_inc_count | |
MOVAC | |
LDA count | |
INC | |
STOAC count | |
MOVR | |
JMP _return_inc_count | |
;;;;;;;;;;;;;;;;;;;;MAIN PROGRAM;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
_start | |
_main | |
;while count != limit | |
JMP _limit_GT_count | |
_return_limit_GT_count | |
JMPZ _end | |
JMP _check_prime | |
_return_check_prime | |
;increment count | |
JMP _inc_count | |
_return_inc_count | |
;try dividing the current count by all previous counts | |
;if the answer is 0, it's a prime, push it to outr and inc prime count | |
JMP _main | |
_end | |
;if you want the final output to be the total number of primes, uncomment the following 2 lines | |
;otherwise the final output is the last prime found | |
;LDA prime_count ; output prime_count | |
;OUT | |
HALT | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment