MIPS program: check if number is prime - mips

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

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.

Related

Why is my array comparison not printing in MIPS?

Here is my code that calculates the sum of two arrays and then prints them in a message to the the console. I would now like to compare my two array sums and return the correct message prompt depending on which array is larger. I feel like I'm very close because I tried to follow the logic from the first printed statements but how can I compare register $s7 & $s8 and then declare one the larger array through my message prompt I have set up? I'm using this emulator: https://cpulator.01xz.net/?sys=mipsr5-spim
.data
x: .word 1,2,3,4,5,6,7,8,9,10
iterator: .word 0
size: .word 9
prompt: .asciiz "The total sum of the array is equal to: "
.text
main:
la $s0, prompt
la $t0, x
lw $t1, iterator
lw $t2, size
begin_loop:
bgt $t1, $t2, exit_loop
sll $t3, $t1, 2
addu $t3, $t3, $t0
lw $t6, 0($t3)
addu $s7, $s7, $t6
addi $t1, $t1, 1
j begin_loop
exit_loop:
li $v0, 4
la $a0, prompt
syscall
li $v0, 1
la $a0, ($s7)
syscall
.data
x2: .word 5,6,7,8,9,10,11,12,13,14
iterator2: .word 0
size2: .word 9
prompt2: .asciiz "The total sum of array 2 is equal to: "
prompt3: .asciiz "The larger array is array 1"
prompt4: .asciiz "The larger array is array 2"
.text
main2:
la $s0, prompt2
la $t0, x2
lw $t1, iterator2
lw $t2, size2
begin_loop2:
bgt $t1, $t2, exit_loop2
sll $t3, $t1, 2
addu $t3, $t3, $t0
lw $t6, 0($t3)
addu $s8, $s8, $t6
addi $t1, $t1, 1
j begin_loop2
exit_loop2:
li $v0, 4
la $a0, prompt2
syscall
li $v0, 1
la $a0, ($s8)
syscall
slt $t1,$s7,$s8 # checks if $s0 > $s1
beq $t1,1,prompt3 # if $s7 > $s8, goes to prompt3
beq $t1,$zero,prompt4 # if $s8 < $s7, goes to prompt4
li $v0, 4
la $a0, prompt3
syscall
li $v0, 4
la $a0, prompt4
syscall

MIPS Print multiples of 2 and 3 in the range 0 to n

I have just started MIPS and I am trying to print all numbers that are multiples of 2 and 3 between a range of 0 to n. I have coded a program that can print the numbers from 0 to n already.
I know that I should try use the "and $t0, $v0, 2" and the "and $t0, $v0, 3" to check if it is a multiple but I get mixed up in the MIPS code as it is very confusing for a beginner. Help would be greatly appreciated!
main:
la $a0, prompt
li $v0, 4
syscall
li $v0, 5
syscall
li $t0, 1
move $t2,$v0;
loop:
bgt $t0, $t2 end
move $a0, $t0
li $v0, 1
syscall
li $a0, '\n'
li $v0, 11
syscall
add $t0, $t0 1
b loop
end:
jr $ra
.data
prompt:
.asciiz "Enter a number: "
This is one approach for this problem
#for (int i =0;i<=n; i++)
#if (i%2 ==0) && (i%3==0)
#print i;
.data
prompt: .asciiz "enter value for n: "
newline: .asciiz "\n"
.text
.globl main
main:
li $t5 0 #initialising i = 0
#for printing prompt
li $v0 4
la $a0 prompt
syscall
#user input reading
li $v0 5
syscall
move $t1 $v0
#displaying on console
move $a0 $t0
li $v0 1
syscall
move $v0 $t0
loop:
beq $t5 $t1 end #checking if i=n
#checking multiples of 2
div $t2 $t5 2
mfhi $t2 #remainder
beq $t2 0 equal #if remainder 0, jump to equal
addi $t5 $t5 1 #increment i by 1
j loop
equal:
#beq $t2 $t3 answer
#checking multiples of 3
div $t3 $t5 3
mfhi $t3 #remainder
beq $t3 0 equal2 #if remainder 0, jump to equal2
addi $t5 $t5 1 #if remainder != 0, incrementing i and jumping to loop
j loop
equal2:
beq $t2 $t3 answer #if both remainder equal to zero, jump to answer
answer:
move $a0 $t5 #moving the value of i for which i is a multiple of 2 and 3
li $v0 1 #printing the answer
syscall
#giving space of a line
li $v0,4
la $a0, newline
syscall
addi $t5 $t5 1 #incrementing i
j loop
#terminating
end:
li $v0, 10
syscall

MIPS: how can I apply the integer which users input into arithmetic function?

This is my first time coding PCSPIM. I find that there is a little trouble with my code.
.data
user_input: .asciiz "\n\nEnter an Integer for the value of n: "
result_display: .asciiz "\nThe sum from 0 to n is "
Greeting: .asciiz "\n\nThank you!"
.text
main:
#user input
li $v0, 4
la $a0, user_input
syscall
#allow user input
li $v0, 5
syscall
#store the input value into t8
move $t8, $v0
#calculation
addi $s0, $zero, $t8
I wish to use the integer value ($t8) that users input into the #calculation section, but it ends up with error.
addi $t0, $zero, 0
loop1:
add $t0, $t0, $s0
addi $s0, $s0, -1
bne $s0, $zero, loop1
nop
nop
# Display the result
li $v0, 4
la $a0, result_display
syscall
# Print out the result
li $v0, 1
move $a0, $t0
syscall
# Greets the user
li $v0, 4
la $a0, Greeting
syscall
# Exit the program
li $v0, 10
syscall
Sorry for my broken English.
The error is in the way you are using the "addi" instruction. The instruction requires an immediate (number) value to be passed as the third operand and not an architectural register. If you update the "addi" instruction to "addu" the code should work.

MIPS multiplication using addition

sorry, I try to multiply two integers but it doesn't work. I can't find where the problem is.Maybe because of register' names but I don't know how to correct it. I correct many times but it is not successful. Could anyone give me some points?
.data
prompt1: .asciiz "Please enter the first signed (decimal) integer: "
prompt2: .asciiz "Please enter the second signed (decimal) integer: "
result_msg: .asciiz "The result of these two 16-bit integers\' multiplication is: "
.text
.globl main
main:
li $v0, 4 #print prompt
la $a0, prompt1
syscall
li $v0, 5 #read multiplicand
syscall
move $s0, $v0
li $v0, 4 #print prompt
la $a0, prompt2
syscall
li $v0, 5 #read multiplier
syscall
move $s1, $v0
Mult: ori $t0,$zero,1 #mask
move $s3, $0 #initialize the result register
move $t1, $0
loop: beq $s1, $zero, end #if the multiplier is 0 then finished
and $t1, $t0, $s1 #mask
beq $t1, 1, mult_add
beq $t1, 0, shift
mult_add: addu $s3, $s3, $s0 #add to get product
shift:
sll $s0, $s0, 1 #shift multiplicand left
srl $s1, $s1, 1 #shift multiplier right
j loop
end:
jr $ra
result: #input the print_string
li $v0, 4
la $a0, result_msg
syscall
exit:
li $v0, 1 #input result
move $a0, $s3
syscall
li $v0, 10 #exit
syscall
Inspecting your code I see that you jump to label end when you are done multiplying.
The instruction at that label issues a jr $ra which "returns from a function", but I guess you just want to print the result and exit.
Therefore I'd suggest you remove that instruction so as to print the result and exit and maybe remove label result as it is not used anywhere in your code.

MIPS: Printing Out a Histogram

I'm writing a MIPS program (assembly language) that takes in 10 integers and prints out a histogram represented by asterisks.
E.g.:
User input of 1, 2, 3, 4
Output:
*
**
***
****
I have most of this code written already in MIPS. The problem I am running into is printing out the correct length of asterisks. As of now it is simply printing out the a histogram all of the same length; the FIRST user inputed integer.
# program functionality:
.data
menu: .asciiz "\n1. New Histogram\n2. Print Histogram\n3. Quit\n"
prompt: .asciiz "\nEnter 10 numbers between 0 and 50 (inclusive):\n"
prompt1: .asciiz "\nEnter a valid number:\n"
asterisk: .asciiz "*"
space: .asciiz "\n"
array: .word 0:10
.text
main:
do:
jal print_menu
li $v0, 5
syscall
beq $v0, 1, new
beq $v0, 2, print
beq $v0, 3, quit
j do # end do
new:
jal new_user
j do
print:
jal print_user
j do
j quit
print_menu:
la $a0, menu
li $v0, 4
syscall
jr $ra
new_user:
la $a0, prompt
li $v0, 4
syscall
enter_loop:
la $t0, array
li $t1, 10
enter_loop_2:
la $a0, prompt1
li $v0, 4
syscall
li $v0, 5
syscall
sw $v0, ($t0)
addi $t1, $t1, -1
beqz $t1, end_loop_2
addi $t0, $t0, 4
j enter_loop_2
end_loop_2:
jr $ra
print_user:
la $t0, array
li $t1, 10
pLoop:
la $a0, space
li $v0, 4
syscall
asterisk_fun:
li $v0, 1
lw $a0, ($t0)
syscall
counter:
la $a0, asterisk
li $v0, 4
syscall
addi $a0, $a0, -1
beqz $a0, asterisk_end
j counter
asterisk_end:
jr $ra
addi $t1, $t1, -1
beqz $t1, endpLoop
addi $t0, $t0, 4
j pLoop
endpLoop:
jr $ra
quit:
li $v0, 10
syscall
The problems is that you are overwriting register $a0 in counter with the address of the asterisk, and you also used $a0 to count the number of items in that bucket.
Easy solution is to use other register (e.g. $a1) to count the number of items:
That would be:
#... your code
asterisk_fun:
li $v0, 1
lw $a1, ($t0) # Load number in $a1
move $a0, $a1 # move to $a0 just to print it
syscall
la $a0, asterisk
counter:
li $v0, 4
syscall
addi $a1, $a1, -1 # we use $a1 to keep the counter
beqz $a1, asterisk_end
j counter
asterisk_end:
# ... more of your code