## ECE2030 Intro to Computer Engineering ## ## Prime identifier ## ## Author: Prof. Hsien-Hsin S. Lee ## ## School of Electrical & Computer Engineering ## Georgia Institute of Technology ## Atlanta, GA 30332 ## ## leehs@gatech.edu ## ## .rdata .globl string string: .ascii " is a prime number\n" .word 0x00000000 LB: .ascii "Lower Bound?" .word 0x00000000 UB: .ascii "Upper Bound?" .word 0x00000000 # Standard startup code. Invoke the routine main with no arguments. .text .globl __start __start: jal main addu $0, $0, $0 # Nop addiu $v0, $0, 10 syscall # syscall 10 (exit) .globl main main: addu $20, $0, $31 # Save return PC # set up initial values li $v0, 4 la $a0, LB syscall li $v0, 5 syscall addu $12, $0, $v0 # Enter Lower Bound in $12 li $v0, 4 la $a0, UB syscall li $v0, 5 syscall addu $20, $0, $v0 # Enter Upper Bound in $20 addiu $13, $0, 1 # Load immediate value into r3 add $16, $0, $0 # clear $16 before entering the loop # If $16=2, a prime number is detected Loop: divu $12, $13 # $12 / $13 mfhi $15 # Get remainder from HI bne $15, $0, Rem_notzero addiu $16, $16, 1 # # of occurences when remainder = 0 beq $13, $12, Next # The factor is itself if condition is true Rem_notzero: addiu $13, $13, 1 # $13 = $13 + 1 j Loop # Next: beq $16, 2, Print_prime # check prime number Cont: add $16, $0, $0 # clear $16 addiu $12, $12, 1 # Increment the number under test bgt $12, $20, Exit # exit if upper bound reached addiu $13, $0, 1 # clear $13 = 1 j Loop .globl Print_prime Print_prime: li $v0, 1 # syscall to print number add $a0, $12, $0 syscall li $v0, 4 # syscall to print string la $a0, string syscall j Cont Exit: jr $31