User:Janetitsme
Hi I'm trying to print prime numbers from number1 and number2 Program gives output always from 2 till number2 istead of number1 If the first number is greater than the second it still shows 2 to number 2 instead of number 1 to number 2
.data
Displays the menu options menu: .ascii "\n" .ascii "1, Enter Number 1\n" .ascii "2, Enter Number 2\n" .ascii "3, Display prime numbers between num1 and num2\n" .ascii "4, Quit\n" .ascii "\n" .ascii "Please enter menu option : " .asciiz ""
Strings to display before after chosing appropriate options make the input and output look meaningful str.first: .asciiz "\nEnter first number :\t" str.second: .asciiz "\n Enter Second number :\t" str.combine: .asciiz " and " str.space: .asciiz " \t " str.newline: .asciiz " \n " str.prime: .asciiz " \n The prime numbers of : "
initialization of registers these used for prime numbers i: .word 2
p: .word 0
k: .word 2
c: .word 1
d: .word 2
.globl main
.text
setting default values to stroring numbers if user wants to choose direct option 3 or higher addi $s1,$zero, 2 # default Value for num1 addi $s2,$zero, 8 # default Value for num2
Print Menu using service num 4 printMenu: # Label so we can jump/branch back here
la $a0, menu #set $a0 to label menu 's address addi $v0, $zero, 4 syscall
Read user option using service 5 [chosen menu num goes into $v0] addi $v0, $zero, 5 syscall
move user's choice into $s3 add $s3,$zero, $v0
process menu beq $s3,1,opt1 beq $s3,2 opt2 beq $s3,3,opt3 beq $s3,4,quit
user selected an invalid option so print menu again j printMenu
opt1:
do this if user selected 1 User input for the first number addi $v0, $zero, 4 la $a0, str.first #set labels address into $a0 syscall #Displays Enter first number from $a0
addi $v0, $zero, 5 #Reads input from keyboard and stores in $v0 syscall addi $s1, $v0,0 # set FIRST NUMBER(s1) to user enter input(v0)
j printMenu # jump back and print menu again
opt2:
do this if user selected 2 User input for the second number addi $v0, $zero, 4 la $a0, str.second #set labels address into $a0 syscall #Displays Enter Second number from $a0
addi $v0, $zero, 5 #Reads input from keyboard and stores in $v0 syscall addi $s2, $v0,0 # set SECOND NUMBER(s2) to user enter input(v0)
j printMenu
DO THIS IF OPTION 3 IS SELECTED Prime numbers will be displayed between num1 and num2 opt3: main:
li $v0, 4 # display message to user la $a0, str.prime #load address of the label in $a0 syscall #Display the string 'The prime numbers of : '
li $v0,1 move $a0,$s1 #Entered first number moved to $a0 syscall #Display the first number
addi $v0, $zero, 4 la $a0, str.combine #load address of the label in $a0 syscall #'and' will be displayed in between first and the second number
li $v0,1 move $a0,$s2 #Entered second number moved to $a0 from $s2 syscall #Display the second number
addi $v0, $zero, 4 la $a0, str.newline #load address of the label in $a0 syscall
swapped values move $t0,$s1 #move contents of $s1 to $t0 move $t7,$s2 #move contents of $s2 to $t7
li $v0, 4 la $a0, str.space #load address of the label in $a0 syscall # call operating sys to leave gap after printing number
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 $t7,$t1 L2 #Branch to statement at Label L2 if $t7 is less than $t1
li $v0, 1 # print integer function call 1 move $a0, $t1 #move contents of $t1 is moved to $a0 syscall # integer to print call operating sys
li $v0, 4 la $a0, str.space #load address of the label in $a0 syscall # call operating sys to print space or gap
Outer loop L2: move $t3, $zero
# Inner loop
L1: remu $t4, $t1, $t2 # remainder set to $t4(remainder of $t1 divided by $t3) 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 # move contents of $t1 to $a0 syscall #prints the integer
li $v0, 4 # print string function call 4 la $a0, str.space #load address of the label in $a0 syscall # call operating sys to print gap or space
P: add $t1, $t1, $t5 # increment i
move $t2, $t6 #move contents $t6 to $t2
bgt $t1, $t7, E #branch to statements at label if $t1 is greater than $t7 j L2
E: j printMenu # jump back and print menu again
DO THIS IF OPTION 12 IS SELECTED opt4: quit: