Computer Engineering Project Collection

-->

Friday, February 13, 2009

MIPS - Prime Number

This is the last of the three MIPS problems. If you recall, the first one was Determinant of 2x2 matrix, the second one was Bit Counter, and this last one is about Prime Number.
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. As usual the simulation is performed using MIPS32 Simulator program called PC-SPIM.

Labels: , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home