; Shawn Nematbakhsh section 22 ; login: snematbakhsh ; as1 - this program takes a sixteen bit 2's compliment integer from ; the user and reports back if the number is prime or not. .ORIG x3000 ; R6 holds the number of iterations we should go through to take input LD R6,ITERATIONS ; R5 stores negative ascii zero LD R5,ASCII_OFFSET ; R2 will hold the input once we process it all correctly AND R2,R2,#0 ; combined input ; R1 counts through the iterations so we know which bit we are ; processing currently AND R1,R1,#0 ADD R1,R1,R6 ; output the prompt LEA R0,PROMPT PUTS ; take a bit in and then output it INPUT_LOOP GETC OUT ; R3 counts through the number of times we need to double a bit to get ; it to the right spot ADD R3,R1,#-1 ; subtract the ascii zero offset leaving us with either hard zero or one ADD R4,R0,R5 ; loop to double the user input R3 times DOUBLE ADD R3,R3,#-1 BRn ACCUMULATE ; double R4 ADD R4,R4,R4 ; see if we have to double again ADD R3,R3,#0 BRp DOUBLE ; once the user entered bit is in the right position we add it to the ; master register R2 ACCUMULATE ADD R2,R2,R4 ; decrement the bit counter because we just processed a bit ADD R1,R1,#-1 ; jump to get input again if necessary BRnp INPUT_LOOP ; check to see if the entered an integer greater than one ADD R1,R2,#-1 ; if not print an error and quit BRnz BAD_INPUT ; R0 = the numbers to check divisibility by, starting with two AND R0,R0,#0 ADD R0,R0,#2 ; R1 = the negative of the user entered number ; flip the bits and add one CHECK NOT R1,R2 ADD R1,R1,#1 ; check to see if we've checked all divisors < n ADD R3,R1,R0 BRzp PRIME ; add the number we are checking to the inputed number CHOP ADD R1,R1,R0 ; if we have zero then the number isn't prime BRz NOT_PRIME ; if the number is negative then decrease it again ADD R1,R1,#0 BRn CHOP ; if the result is positive then go check another number ADD R0,R0,#1 BRnzp CHECK ; not prime output message NOT_PRIME LEA R0,NOT_PRIME_OUT PUTS BRnzp FINISH ; prime output message PRIME LEA R0,PRIME_OUT PUTS BRnzp FINISH ; error output message BAD_INPUT LEA R0,ERROR PUTS FINISH HALT ; the user prompt PROMPT .STRINGZ "Input a Number>" ; error prompt ERROR .STRINGZ "\nERROR: Number Must Be Positive and Greater Than 1!" ; prime prompt PRIME_OUT .STRINGZ "\nThis Number IS Prime" ; composite prompt NOT_PRIME_OUT .STRINGZ "\nThis Number is NOT Prime" ; bits to take in ITERATIONS .FILL #16 ; negative ascii zero ASCII_OFFSET .FILL #-48 .END