Created
April 17, 2013 12:42
-
-
Save imduffy15/5403924 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| .data | |
| N: .word 1000 | |
| CONTROL: .word 0x10000 | |
| DATA: .word 0x10008 | |
| max: .word 65536 ; size of primes space | |
| primes: .space 65536 ; space for MAX/8 8-byte primes | |
| .text | |
| ld r20,CONTROL(r0) | |
| ld r21,DATA(r0) | |
| ; ****************************************************************** | |
| ; main | |
| ld r10,N(r0) ; N -> r10 | |
| ld r13,max(r0) ; Max amount the array can hold | |
| ; Put 2 into the array | |
| ;daddi r11,r0,2 | |
| ;daddi r12,r0,0 | |
| ;sd r11,primes(r12) | |
| ; Put 3 into the array | |
| ;daddi r11,r0,3 | |
| ;daddi r12,r12,8 | |
| ;sd r11,primes(r12) | |
| ; Set the number of elements in the array | |
| daddi r12,r12,0 ; Count of elements in the array | |
| ; Start from 5 | |
| daddi r11,r0,2 | |
| mloop: ; main loop ******** | |
| movz r30,r11,r0 ; install argument (i) for prime routine | |
| daddi r11,r11,1 ; i = i + 1 | |
| jal prime | |
| bne r10,r11,mloop ; repeat loop if i != N | |
| done: | |
| halt ; exit | |
| ; ****************************************************************** | |
| ; prime | |
| ; i = 2; | |
| ; while ( True ) | |
| ; { | |
| ; if ( N%i == 0 ) | |
| ; break; | |
| ; i += 1; | |
| ; } | |
| ; | |
| ; if ( i == N ) | |
| ; output(N); | |
| prime: ; prime subroutine | |
| movz r1,r30,r0 ; P -> r1 (P is the number we're checking) | |
| daddi r2,r0,0 ; Get current place in the array | |
| ld r3,primes(r2) ; Load in array element | |
| ploop: ; prime loop ******* | |
| ddiv r4,r1,r3 ; r4 = P/currentPrime | |
| dmul r5,r4,r3 ; r3 = P/currentPrime * currentPrime | |
| beq r1,r5,doneploop ; branch if P/currentPrime * currentPrime == P (divisible) | |
| ; Increment the array counter | |
| daddi r2,r2,8 | |
| ; Check that its not out of bounds | |
| slt r6,r2,r12 | |
| beqz r6,endploop | |
| ; Load in the next known prime | |
| ld r3,primes(r2) | |
| dmul r5,r3,r3 | |
| slt r6,r1,r5 | |
| bnez r6,endploop | |
| j ploop ; prime loop ******* | |
| doneploop: ; done prime | |
| jr r31 ; return | |
| endploop: | |
| ;bne r1,r2,doneploop ; branch if P != j (not prime) | |
| sd r1,primes(r12) | |
| daddi r12,r12,8 | |
| sd r1,0(r21) ; prime! P equals j; write P to DATA | |
| daddi r1,r0,2 ; 2, for writing an integer | |
| sd r1,0(r20) ; write 2 to CONTROL | |
| jr r31 | |
| ; ****************************************************************** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment