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

Hai there, I am needing to input an positive integer and then test if it is a me

ID: 3551351 • Letter: H

Question

Hai there, I am needing to input an positive integer and then test if it is a mersenne prime or not.

Currently I have this much: However I cannot figure out how to follow through with the rest.


############################

# Input a positive integer p.

# Compute 2^p -1.

# Using a function.

############################

.data

prompt: .asciiz "Enter a positive integer: "

.text

main:


# Prompt user for input

li $v0,4

la $a0,prompt

syscall

# Get integer p and put in $t1

li $v0,5

syscall

move $t1,$v0

# Prepare to call the function compute

move $a0,$t1

jal compute

# Output 2^p - 1

# 2^p - 1 is in $a0

move $a0,$v0

li $v0,1

syscall

li $v0,10

syscall

# Function to compute 2^p - 1

# $t3 contains intermediate calculation

# $t2 counts down from p to 0

compute: move $t2,$a0

li $t3,1

there: beqz $t2,here

mul $t3,$t3,2

addi $t2,$t2,-1

j there

here: addi $t3,$t3,-1

move $v0,$t3

jr $ra

Explanation / Answer

The question is to find the if the entered number is mersenne prime or not. The mersenne prime is 2^n - 1-> the mersenne prime in binary form contains all ones to right side and all zeros on the left, but the number of ones may vary depending on n.

Lets say the user entered 31 -> 0000011111 in binary is mersenne prime and on the other hand 5->101 binary is not.

The working code is as follows.


.data

prompt: .asciiz "Enter a positive integer: "

false: .asciiz "Entered number is not a mersenne prime "

true: .asciiz "Entered number is a mersenne prime "

.text

main:

# Prompt user for input

li $v0,4

la $a0,prompt

syscall

# Get integer p and put in $t1

li $v0,5

syscall

move $t1,$v0

#store the integer in t3 for calculations

move $t3,$t1

#clear t4(for count) and t5 (to store a constanst 32 to check the end of t4)

and $t4,$t4,$0

and $t5,$t5,$0

addi $t5,$t5,32

#search for first zero

zeronotfound:

andi $t2,$t3,0x01

beq $t2,$0,zerofound

addi $t4,$t4,0x01

ror $t3,$t3,0x01

beq $t4,$t5,end

b zeronotfound

#end of all the bits

end :

li $v0,4

la $a0,true

syscall

b exit

#first zero found, check if there are any ones to the left of first zero

zerofound:

addi $t4,$t4,0x01

ror $t3,$t3,0x01

beq $t4,$t5,end

andi $t2,$t3,0x01

beq $t2,$0,zerofound

#a one is found

li $v0,4

la $a0,false

syscall

exit:li $v0,10

syscall