Question

Write a MIPS Assembly program to accept two numbers A and B from the user. Print...

Write a MIPS Assembly program to accept two numbers A and B from the user. Print all the prime numbers between A and B. Also, check if A and B are prime.

If no prime number exits print the string "No prime number between numbers A and B"

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

1 #Data declaration section 2data 3 lowerBound: .asciiz Enter A: 4 pperBozand: .ascilz Enter B: s result: .asci1s Prine mmbers are: n é Error: .asciiz Integer should be positive!AnPlease enter again.n 7 BoundcheckError: . asciiz B should be greater or equal than A:nPlease enter again.\n 8 noPrì,e : . asc îiz To prie number betueen numbers λ and B n 9 OneDisplay: .ascils is nelther a prine nor a composite nunber.n 10 space: .asc11г.. 11 main prograa 12 text 13 din 14 #Proupt for first integer S lasao,lowerB ound e li $0,4 17 syscall 18 li 0,5 19 syscall 20 nove 0,470 21 Check negatire ralue 22 le 50,0,error 23 second HAddress of rirst iput prompt Syoteen call for string displar #display string #Read integer system call #Get integer in vo #5tore first integer in so ne: 1 Column: 1 Show Line Numbers Mars Messages Run O rime nrbera are: 11 13 17 19 Clear program is finished running -

--------------------------------------------------------------

Program

#Data declaration section
.data
lowerBound: .asciiz "Enter A: "
upperBound: .asciiz "Enter B: "
result: .asciiz "Prime numbers are:\n"
Error: .asciiz "Integer should be positive!!!\nPlease enter again.\n"
BoundCheckError: .asciiz "B should be greater or equal than A\nPlease enter again.\n"
noPrime: .asciiz "No prime number between numbers A and B\n"
OneDisplay: .asciiz "1 is neither a prime nor a composite number.\n"
space: .asciiz " "
#main program
.text
main :
#Prompt for first integer
la $a0,lowerBound                  #Address of first input prompt
li $v0,4                           #Systeem call for string display
syscall                            #display string
li $v0,5                           #Read integer system call
syscall                            #Get integer in v0
move $s0,$v0                       #Store first integer in s0
#Check negative value
ble $s0,0,error1
second:
#Prompt for second integer
la $a0,upperBound                  #Address of second input prompt
li $v0,4                           #Systeem call for string display
syscall                            #display string
li $v0,5                           #Read integer system call
syscall                            #Get integer in v0
move $s1,$v0                       #Store second integer in s1
#Check negative value
ble $s1,0,error2
#Check upperbound >lowerbound
blt $s1,$s0,boundError
#Result prompt
la $a0,result                       #Address of Error message
li $v0,4                           #Systeem call for string display
syscall                            #display string
li $a1,0                           #counter for prime numbere
#Check prime
check:
bgt $s0,$s1,Exit
move $a0,$s0
jal checkPrime
addi $s0,$s0,1
j check
#End of the program
Exit:
bne $a1,0,end
la $a0,noPrime                      #Address of no prime msg
li $v0,4                           #Systeem call for string display
syscall                            #display string
end:
li $v0,10                         #Program end system call
syscall                           #end
#Negative error message
error1:
la $a0,Error                       #Address of Error message
li $v0,4                           #Systeem call for string display
syscall                            #display string
j main                             #go to main
#Negative error message
error2:
la $a0,Error                       #Address of Error message
li $v0,4                           #Systeem call for string display
syscall                            #display string
j second                            #go to second prompt
#Display bound error
boundError:
la $a0,BoundCheckError             #Address of bounde error message
li $v0,4                           #Systeem call for string display
syscall                            #display string
j second                            #go to second prompt
#Display primes prompt
la $a0,result                       #Address of Error message
li $v0,4                           #Systeem call for string display
syscall                            #display string
#prime check function
checkPrime:
li $t0,0                            #Flag for prime check
li $t1,2                           #for loop
beq $a0,1,print1                    #compare integer=1
div $t2,$a0,2                      #n/2
#loop for prime check
loop:
bgt $t1,$t2,print                 #for(i = 2; i <= n/2; ++i)
div $a0,$t1                      #Mode finding
mfhi $t4                         #Mode value
beq $t4,0,return                #if mode =0 no prime
addi $t1,$t1,1                  #Increment counter           
j loop                          #Repeat
print:
li $v0,1                       #If prime print value syatem call
syscall                       #Print integeer
la $a0,space                  #Address of space
li $v0,4                     #System call to print string
syscall                      #Print string
addi $a1,$a1,1               #counter for prime numbers
jr $ra                      #return to main method
#If not prime return to main without display
return:
jr $ra
#If integer=1
print1:
la $a0,OneDisplay                 #Address of 1 value display
li $v0,4                           #Systeem call for string display
syscall                            #display string
jr $ra                            #return to main

---------------------------------------------------

Output

Enter A: 20
Enter B: 22
Prime numbers are:
No prime number between numbers A and B

-- program is finished running --

Enter A: 10
Enter B: 20
Prime numbers are:
11 13 17 19
-- program is finished running --

Add a comment
Know the answer?
Add Answer to:
Write a MIPS Assembly program to accept two numbers A and B from the user. Print...
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
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
ADVERTISEMENT