Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- .align 2 # 2^2 = 4 bytes per item
- numerePrime: .space 100 # init space for 100 elements => 100/4 = 25 elements
- .text
- li $t4, 0 # counter
- li $t5, 25 # total number of elements
- li $a1, 2 # N a0 is avoided because it is used for return value of functions
- la $t6, numerePrime # address of array
- cauta:
- jal EstePrim # call function (jump and link)
- beqz $s0, nu_e_prim # if s0 == 0, then n is not prime
- e_prim:
- puti $a1
- putc ' '
- sw $a1, ($t6) # store a1 in array
- addi $t6, $t6, 4 # t6 += 4
- addi $t4, $t4, 1 # t4++ (counter++)
- nu_e_prim:
- addi $a1, $a1, 1 # a1++ (n++)
- sge $t7, $t4, $t5 # t7 = 1 if t4 >= t5
- beqz $t7, cauta # if t7 == 0, then continue
- sfarsit:
- done
- # input: $a0 - $a3. The other registers are not preserved.
- # $a1 = n
- # outuput: $s0 - $s8.
- # $s0 = 1 if n is prime, 0 otherwise
- EstePrim:
- li $t0, 2 # t0 = 2 (new counter)
- repeta:
- sge $t3, $t0, $a1 # t3 = 1 if t0 >= a1(n)
- bnez $t3, gata_e_prim # if t3 == 1, then n is prime
- div $a1, $t0 # n / t0
- mfhi $t1 # t1 = n % t0
- addi $t0, $t0, 1 # t0++
- beqz $t1, gata_nu_e_prim # if t1 == 0, then n is not prime
- j repeta # jump to repeta (continue)
- gata_nu_e_prim:
- li $s0, 0 # n is not prime
- j iesire # jump to iesire
- gata_e_prim:
- li $s0, 1 # n is prime
- j iesire # jump to iesire
- iesire:
- jr $ra # jump register to return address
Advertisement
Add Comment
Please, Sign In to add comment