Write a MIPS assembly program to read a number from the user
Write a MIPS assembly program to read a number from the user , and prints all the prime numbers between 0 and that number.
need assembely code
Solution
.text
.align 2
.globl main
# register initialization
i:
.word 2
p:
.word 0
k:
.word 2
c:
.word 1
d:
.word 2
main:
# list the first prime numbers up to given number n
li $v0, 4
la $a0, prompt
syscall
li $v0, 5 # read n from keyboard into $v0
syscall
move $t0,$v0 # save n in $t0
li $v0, 4 # display message to user
la $a0, message
syscall
li $v0, 4
la $a0, space
syscall
# load variables into the 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 $t0,$t1 Loop2
li $v0, 1 # print integer function call 1
move $a0, $t1 # print the integer
syscall
li $v0, 4 # print next line
la $a0, space
syscall
#Outer Loop
Loop2: move $t3, $zero
# Inner Loop
Loop1: remu $t4, $t1, $t2
bne $t4, $zero, I
move $t3, $t5
I: add $t2, $t2, $t5
blt $t2, $t1 Loop1
bne $t3, $zero, P
li $v0, 1
move $a0, $t1 # print the integer
syscall
li $v0, 4 # print next line
la $a0, space
syscall
P:
add $t1, $t1, $t5 # increment i
move $t2, $t6
bgt $t1, $t0, E
j Loop2
E: li $v0, 10
syscall # syscall to exit
end: jr $ra
.data
prompt:
.asciiz \"Please enter the value of number n: \"
message:
.asciiz \"The Prime Numbers are : \"
space: .asciiz \"\ \"

