If you see any Dead link in Any Post...
Then please report immediately on our FACEBOOK Account or COMMENT Below of that Post...
It'll Help us to maintain our Blog and assurance of 100% Original Links.
Thanks... ADMIN...

March 14, 2012

MIPS - Prime Number Code


This is the problem :
write a program that store the first 20 prime numbers into memory!

Well, the idea of checking prime number is, as you should already know, by dividing a number with another number less than itself. The program will check if there are any remainder, and will perform another division with lesser number if there aren't. If there aren't any remainder until the divisor reach number one, then the number is a prime number. If there are remainder within the division progress, then the program will check another number that is more than the previously checked number. It obviously the easiest method, but could take some time for a very large numbers.

This is the code :
.data 0x1001000
array: .space 80
.text
.globl main

main: addu $s0,$ra,$0
lui $s1,0x1001
addi $s2,$s1,80
addi $t0,$0,2 
addi $t5,$0,1
sw $t0,0($s1)
addi $s1,$s1,4

loop1: addi $t0,$t0,1
or $t1,$t0,$0

loop2: addi $t1,$t1,-1
slt $t4,$t5,$t1
bne $t4,$0,skip
sw $t0,0($s1)
addi $s1,$s1,4
beq $s1,$s2,exit 
j loop1
skip: div $t0,$t1
mfhi $t3
bne $t3,$0,loop2
j loop1

exit: addu $ra, $0, $s0
jr $ra

the program will write the first 20 prime numbers checked, and it will be stored in memory. 

0 comments:

Post a Comment