Question

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

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.)

1 1
Add a comment Improve this question Transcribed image text
Answer #1
.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  "\n    "

#initialization of registers

i:
.word   2

p:
.word   0

k:
.word   2   

c:
.word   1


d:
.word   2
Add a comment
Know the answer?
Add Answer to:
Write and test a MIPS assembly language program to compute and display the first prime numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CDA-3101 – MIPS Assembly Programming 1. Write the following code segment in MIPS assembly language code:...

    CDA-3101 – MIPS Assembly Programming 1. Write the following code segment in MIPS assembly language code: 3. Write a MIPS program to find the sum of squares from 1 to n. Where n=10. For example, the sum of squares for 10 is as follows: 12+22+32+……+n2=385

  • . Write an 8086 assembly language program to find the prime numbers among 100 bytes of...

    . Write an 8086 assembly language program to find the prime numbers among 100 bytes of data in an array stored from the address 4000H: 1000H in the data segment and store the result from the address 4000H: 3000H. write the code using 8086 assembly language only i do not want any other language If you Do not sure please do not solve it

  • Name B. (7 pts) MIPS short answer 1. (3pt) For the following MIPS assembly language program:...

    Name B. (7 pts) MIPS short answer 1. (3pt) For the following MIPS assembly language program: loop: addi Sto, $to,-1 bne $to, $zero, loop Translate the second instruction into MIPS machine language and write it in hex. 2. (2 pt) Which best describes the reason that we maintain the stack pointer in a register? (circle one) i. The hardware forces use of a stack pointer. ii. We need a local pointer because we are often limited to relative addressing. ili....

  • MIPS ASSEMBLY PROGRAM: PLEASE Write in MIPS Assembly language. Take strings as input and calculate and...

    MIPS ASSEMBLY PROGRAM: PLEASE Write in MIPS Assembly language. Take strings as input and calculate and print a simple checksum for each string. Make your string long enough to hold 50 characters. Don't forget to leave space for the null byte. Our checksum algorithm will produce a value which you can print with the syscall for printing a character. Stop reading strings when the user enters ".". The syscall to read a string (sycall code 8) adds a newline to...

  • Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence....

    Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence. Your program should prompt for an integer input n from the user. The program should call a recursive function to compute the nth fibonacci number. Your program must follow programming convention. You should submit program and screenshot of output in a single word/pdf file. You should use following recursive definition of fibonacci function: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) +fib(n-2)

  • I need MIPS code for this program Translate the following C++ program to MIPS assembly program...

    I need MIPS code for this program Translate the following C++ program to MIPS assembly program (Please explain each instruction of your code by a comment and submit a .asm file) // Example program #include <iostream> #include <string> using namespace std; int main() { const int ADULT_CHOICE= 1, CHILD_CHOICE= 2, SENIOR_CHOICE= 3, QUIT_CHOICE= 4, ADULT = 250, CHILD = 200, SENIOR = 350; int choice, months; int charges = 0; do { cout <<"\n\t\tHealth Club Membership Menu\n\n" <<"1. Standard Adult...

  • MIPS - assembly language Task I (1 mark): Write a program to calculate the nth Fibonacci...

    MIPS - assembly language Task I (1 mark): Write a program to calculate the nth Fibonacci number based on an iterative approach where value n is read from the keyboard and stored in your main program that is calling fib.  The main program reads number ‘n’ -- restrict range of ‘n’ to 2-50, and display an error message if this condition is not satisfied.  The main program calls fib with parameter passing. Procedure fib should return the calculated...

  • 1. [2 points] Write a MIPS assembly language program of the following C function and the...

    1. [2 points] Write a MIPS assembly language program of the following C function and the code to call the function: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; مهه Arguments g, h, i, and j are passed to the function in registers $a0, $al, Şa2, and $a3, respectively while f in $50 (hence, need to save $50 on stack), and the result is to be stored...

  • 2. Searching a String: Write a MIPS assembly language program to do the following: Read a...

    2. Searching a String: Write a MIPS assembly language program to do the following: Read a string and store it in memory. Limit the string length to 100 characters. Then, ask the user to enter a character. Search and count the number of occurrences of the character in the string. The search is not case sensitive. Lowercase and uppercase letters should be equal. Then ask the user to enter a string of two characters. Search and count the number of...

  • Write a simple and short MIPS assembly language program that asks the user for 6 numbers,...

    Write a simple and short MIPS assembly language program that asks the user for 6 numbers, merge-sorts them, and then prints them out in ascending order comment each and every programme .(USING WINDOWS QtSpim) Note : This question is not giving desired solution may you please try it in a simple manner for six number inputed by the user.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT