LordMirai

lab 2 ScM

Mar 9th, 2023 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. .data
  2.  
  3. .align 2 # 2^2 = 4 bytes per item
  4. numerePrime: .space 100 # init space for 100 elements => 100/4 = 25 elements
  5.  
  6. .text
  7.  
  8. li $t4, 0 # counter
  9. li $t5, 25 # total number of elements
  10. li $a1, 2 # N a0 is avoided because it is used for return value of functions
  11. la $t6, numerePrime # address of array
  12.  
  13. cauta:
  14. jal EstePrim # call function (jump and link)
  15. beqz $s0, nu_e_prim # if s0 == 0, then n is not prime
  16.  
  17. e_prim:
  18. puti $a1
  19. putc ' '
  20. sw $a1, ($t6) # store a1 in array
  21. addi $t6, $t6, 4 # t6 += 4
  22. addi $t4, $t4, 1 # t4++ (counter++)
  23.  
  24. nu_e_prim:
  25. addi $a1, $a1, 1 # a1++ (n++)
  26. sge $t7, $t4, $t5 # t7 = 1 if t4 >= t5
  27. beqz $t7, cauta # if t7 == 0, then continue
  28.  
  29. sfarsit:
  30. done
  31.  
  32. # input: $a0 - $a3. The other registers are not preserved.
  33. # $a1 = n
  34. # outuput: $s0 - $s8.
  35. # $s0 = 1 if n is prime, 0 otherwise
  36. EstePrim:
  37. li $t0, 2 # t0 = 2 (new counter)
  38.  
  39. repeta:
  40. sge $t3, $t0, $a1 # t3 = 1 if t0 >= a1(n)
  41. bnez $t3, gata_e_prim # if t3 == 1, then n is prime
  42.  
  43. div $a1, $t0 # n / t0
  44. mfhi $t1 # t1 = n % t0
  45.  
  46. addi $t0, $t0, 1 # t0++
  47. beqz $t1, gata_nu_e_prim # if t1 == 0, then n is not prime
  48.  
  49. j repeta # jump to repeta (continue)
  50. gata_nu_e_prim:
  51. li $s0, 0 # n is not prime
  52. j iesire # jump to iesire
  53.  
  54. gata_e_prim:
  55. li $s0, 1 # n is prime
  56. j iesire # jump to iesire
  57.  
  58. iesire:
  59. jr $ra # jump register to return address
  60.  
Advertisement
Add Comment
Please, Sign In to add comment