mark12
mark12

Reputation: 23

MIPS program: check if number is prime

.data 
    userInput: .asciiz "give an integer to check if prime: "
    prime: .asciiz "Prime"
    notPrime: .asciiz "No prime"
.text
    main:
        li $v0, 4
        la $a0, userInput
        syscall
    
    li $v0, 5
    syscall
    move $t0, $v0
    
    addi $t1, $t1, 1
    addi $t2, $t2, 2
    ble $t0, $t1, no
    beq $t0, $t2, yes
    div $t0, $t2
    mflo $s0
    mfhi $s1
    bgt $s1, $zero, yes
yes:
    li $v0, 4
    la $a0, prime
    syscall
    j exit
no:
    li $v0, 4
    la $a0, notPrime
    syscall
    j exit
exit:
    li $v0, 10
    syscall

For some reason, the program always jumps to the "yes" even when there's a remainder (mfhi) and I can't seem to find the problem on why it does it. When I give input 4 it should jump to the "no" label since the remainder is 0 so it isn't greater than the constant 0.

Upvotes: 1

Views: 2713

Answers (1)

Erik Eidt
Erik Eidt

Reputation: 26656

What are you trying to accomplish with this part of your code:

    bgt $s1, $zero, yes
yes:

Play the computer and decide where to go if the gt is true vs. if the gt is false.

Upvotes: 1

Related Questions