Guest User

Untitled

a guest
Apr 26th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. .section .data
  2. prime_number_str:
  3. .asciz "%d "
  4.  
  5. .section .text
  6.  
  7. .global _start
  8. _start:
  9. pushl $20
  10. call .first_prime_numbers
  11. addl $4, %esp
  12. pushl $0
  13. call exit
  14.  
  15.  
  16. .first_prime_numbers: #argument first n numbers
  17. movl 4(%esp), %ecx #get the first argument
  18. do_test:
  19. pushl %ecx #push function arguments
  20. call .prime
  21. addl $4, %esp #restore the stack
  22.  
  23. #if not prime jump to the next number
  24. cmpl $0, %eax
  25. je no_not_prime
  26.  
  27. #print the number
  28. pushl %eax #save eax
  29. pushl %ecx #first argument
  30. pushl $prime_number_str #text to print
  31. call printf
  32. addl $4, %esp
  33. popl %eax #restore eax
  34.  
  35. no_not_prime:
  36. loop do_test
  37. ret
  38.  
  39.  
  40. .prime: #argument: number to check
  41. movl 4(%esp), %eax #get the first argument
  42.  
  43. #divide the argument by 2
  44. xorl %edx, %edx
  45. movl $2, %ecx
  46. pushl %eax #save the value of eax
  47. divl %ecx
  48. movl %eax, %ecx #init the counter register
  49. popl %eax #restore the value of eax
  50.  
  51. movl $1, %ebx #assume the argument is prime
  52. test_prime:
  53. # if ecx == 1 then return exit the function
  54. cmpl $1, %ecx
  55. jle return_value
  56.  
  57. pushl %eax #save the old value of eax
  58.  
  59. #divide the value by the value of counter
  60. xorl %edx, %edx
  61. divl %ecx
  62.  
  63. #if the reminder is 0 then the number is not prime
  64. cmpl $0, %edx
  65. popl %eax #restore the value of eax
  66. je not_prime
  67.  
  68.  
  69. subl $1, %ecx #decrease counter
  70. jmp test_prime #try next division
  71.  
  72. not_prime:
  73. movl $0, %ebx
  74. return_value:
  75. movl %ebx, %eax
  76. ret
  77.  
  78. gdb youprogram
Add Comment
Please, Sign In to add comment