Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write and test a MIPS assembly language program to compute and display the first

ID: 3765570 • Letter: W

Question

Write and test a MIPS assembly language program to compute and display the first prime numbers up to n where n is given. Set n to be 19 but the program should work of any value of n.

For the program to identify primes, the easiest way is to use the algorithm:

for (i = 2; i < x; i++)  
    if ((x % i) == 0) break;   //break out, not prime

where x is the number you are checking to see if prime. The "remu" instruction can do x % i. The suggested algorithm is not an efficient algorithm but easier to code than a more efficient algorithm. (Note for example, the above code does not actually have to iterate from 2 to x to find whether x is prime; stopping at squareroot(x) is sufficient.)

Explanation / Answer

.text .align 2 .globl main main: # compute and display the first prime numbers up to value of n where n is 19.So #set n to be 19 but the program should work of any value of n. li $v0, 4 la $a0, prompt syscall li $v0, 5 # read from keyboard into $v0 (number n is the limit) syscall # call operating sys move $t0,$v0 # store n i.e 19 in $t0 li $v0, 4 # display the message to user la $a0, message syscall # call operating sys li $v0, 4 la $a0, space syscall # Load variables into registers lw $t1, i # $t1 = i lw $t2, k # $t2 = k lw $t3, p # $t3 = p lw $t5, c # $t5 = c lw $t6, d # $t6 = d blt $t0,$t1 L2 li $v0, 1 # print integer function call 1 move $a0, $t1 # integer to print syscall # call operating sys li $v0, 4 # print new line la $a0, space syscall # call operating sys #Outer loop L2: move $t3, $zero # Inner loop L1: remu $t4, $t1, $t2 bne $t4, $zero, I move $t3, $t5 I: add $t2, $t2, $t5 # increment k blt $t2, $t1 L1 bne $t3, $zero, P li $v0, 1 # print integer function call 1 move $a0, $t1 # integer to print syscall li $v0, 4 # print new line la $a0, space syscall # call operating sys P: add $t1, $t1, $t5 # increment i move $t2, $t6 bgt $t1, $t0, E j L2 E: li $v0, 10 # system call code for exit = 10 syscall # call operating sys end: jr $ra .data prompt: .asciiz "Please enter the number you wish to find the prime numbers of : " message: .asciiz "The Prime Numbers are : " space: .asciiz " " #initialization of registers i: .word 2 p: .word 0 k: .word 2 c: .word 1 d: .word 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote