Advertisement
cfrantz

Prime sieve in DF.

Nov 27th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. ########################################################
  2. # Naively simple implementation of prime sieve
  3. ########################################################
  4. import stdio;
  5.  
  6. a: [32767]uint8;
  7.  
  8. sieve() -> void {
  9. n: uint16;
  10. i: uint16;
  11. for(n=2; n<32768; n+=1) {
  12. if (a[n]) {
  13. continue;
  14. }
  15. stdio::printf("%d\n", n);
  16. for(i=n; i<32768; i+=n) {
  17. a[i] = 1;
  18. }
  19. }
  20. }
  21.  
  22. main() -> uint8 {
  23. sieve();
  24. return 0;
  25. }
  26.  
  27. ########################################################
  28. # Compiler output
  29. ########################################################
  30. SECTION .bss
  31. ;
  32. ; tests/df/sieve.df:2: a: [32767]uint8;
  33. ;
  34. a:
  35. RESB 32767
  36. SECTION .rodata
  37. ;
  38. ; tests/df/sieve.df:11: stdio::printf("%d\n", n);
  39. ;
  40. str_6:
  41. DB 37,100,10,0
  42. SECTION .text
  43. ;
  44. ; tests/df/sieve.df:0: import stdio;
  45. ;
  46. ;
  47. ; builtin/stdio.df:2: printf: extern __magic__("alias:printf") (format: &const uint8, ...) -> int32;
  48. ;
  49. %define stdio$printf printf
  50. EXTERN printf
  51. ;
  52. ; tests/df/sieve.df:4: sieve() -> void {
  53. ;
  54. GLOBAL sieve
  55. sieve:
  56. PUSH ebp
  57. MOV ebp, esp
  58. AND esp, 4294967280
  59. SUB esp, 32800
  60. MOV [ebp-4], ebx
  61. MOV [ebp-8], esi
  62. MOV [ebp-12], edi
  63. ;
  64. ; tests/df/sieve.df:7: for(n=2; n<32768; n+=1) {
  65. ;
  66. MOV eax, 2
  67. LEA ebx, dword [ebp-20]
  68. MOV word [ebx], ax
  69. for_test_2:
  70. LEA eax, dword [ebp-20]
  71. MOVZX eax, word [eax]
  72. MOV ebx, 32768
  73. CMP eax, ebx
  74. JGE for_exit_4
  75. ;
  76. ; tests/df/sieve.df:8: if (a[n]) {
  77. ;
  78. MOV eax, a
  79. LEA ebx, dword [ebp-20]
  80. MOVZX ebx, word [ebx]
  81. MOV ecx, 1
  82. MOV dword [ebp-16], eax
  83. MOV eax, ebx
  84. MUL ecx
  85. MOV ebx, dword [ebp-16]
  86. ADD ebx, eax
  87. MOVZX eax, byte [ebx]
  88. OR eax, eax
  89. JE lbl_5
  90. ;
  91. ; tests/df/sieve.df:9: continue;
  92. ;
  93. JMP for_ctrl_3
  94. lbl_5:
  95. ;
  96. ; tests/df/sieve.df:11: stdio::printf("%d\n", n);
  97. ;
  98. MOV eax, str_6
  99. LEA ebx, dword [ebp-20]
  100. MOVZX ebx, word [ebx]
  101. MOV [esp+0], eax
  102. MOV [esp+4], ebx
  103. CALL stdio$printf
  104. ;
  105. ; tests/df/sieve.df:12: for(i=n; i<32768; i+=n) {
  106. ;
  107. LEA eax, dword [ebp-20]
  108. MOVZX eax, word [eax]
  109. LEA ebx, dword [ebp-24]
  110. MOV word [ebx], ax
  111. for_test_7:
  112. LEA eax, dword [ebp-24]
  113. MOVZX eax, word [eax]
  114. MOV ebx, 32768
  115. CMP eax, ebx
  116. JGE for_exit_9
  117. ;
  118. ; tests/df/sieve.df:13: a[i] = 1;
  119. ;
  120. MOV eax, 1
  121. MOV ebx, a
  122. LEA ecx, dword [ebp-24]
  123. MOVZX ecx, word [ecx]
  124. MOV edx, 1
  125. MOV dword [ebp-28], eax
  126. MOV eax, ecx
  127. MOV dword [ebp-32], edx
  128. MUL dword [ebp-32]
  129. ADD ebx, eax
  130. MOV eax, dword [ebp-28]
  131. MOV byte [ebx], al
  132. for_ctrl_8:
  133. LEA eax, dword [ebp-24]
  134. MOVZX eax, word [eax]
  135. LEA ebx, dword [ebp-20]
  136. MOVZX ebx, word [ebx]
  137. ADD eax, ebx
  138. LEA ebx, dword [ebp-24]
  139. MOV word [ebx], ax
  140. JMP for_test_7
  141. for_exit_9:
  142. for_ctrl_3:
  143. LEA eax, dword [ebp-20]
  144. MOVZX eax, word [eax]
  145. MOV ebx, 1
  146. ADD eax, ebx
  147. LEA ebx, dword [ebp-20]
  148. MOV word [ebx], ax
  149. JMP for_test_2
  150. for_exit_4:
  151. fnexit_sieve_1:
  152. MOV ebx, [ebp-4]
  153. MOV esi, [ebp-8]
  154. MOV edi, [ebp-12]
  155. MOV esp, ebp
  156. POP ebp
  157. RET
  158.  
  159.  
  160. ;
  161. ; tests/df/sieve.df:18: main() -> uint8 {
  162. ;
  163. GLOBAL main
  164. main:
  165. PUSH ebp
  166. MOV ebp, esp
  167. AND esp, 4294967280
  168. SUB esp, 32800
  169. MOV [ebp-4], ebx
  170. MOV [ebp-8], esi
  171. MOV [ebp-12], edi
  172. ;
  173. ; tests/df/sieve.df:19: sieve();
  174. ;
  175. CALL sieve
  176. ;
  177. ; tests/df/sieve.df:20: return 0;
  178. ;
  179. MOV eax, 0
  180. fnexit_main_10:
  181. MOV ebx, [ebp-4]
  182. MOV esi, [ebp-8]
  183. MOV edi, [ebp-12]
  184. MOV esp, ebp
  185. POP ebp
  186. RET
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement