Minimal programming language prototype, created with goal to have very small compiler, so that anyone can write his own compiler for it.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

136 wiersze
3.8KB

  1. linux_read_system_call = 0
  2. linux_write_system_call = 1
  3. linux_open_system_call = 2
  4. linux_close_system_call = 3
  5. linux_exit_system_call = 60
  6. standard_input = 0
  7. standard_output = 1
  8. format ELF64 executable 3
  9. segment readable executable
  10. macro begin {
  11. push rdi
  12. enter 0, 0
  13. xor rax, rax
  14. }
  15. macro end {
  16. leave
  17. pop rdi
  18. ret
  19. }
  20. main:
  21. nop
  22. nop
  23. nop
  24. mov rdi, text_data ; text);
  25. call echo ; echo (
  26. loop_1:
  27. cmp [main_x], 16
  28. je loop_1e
  29. xor rdx, rdx
  30. mov eax, [main_x]
  31. mov ebx, 15
  32. idiv rbx
  33. cmp edx, 0
  34. jne n3
  35. mov rdi, fb
  36. call echo
  37. jmp n0
  38. n3:
  39. xor rdx, rdx
  40. mov eax, [main_x]
  41. mov ebx, 3
  42. idiv rbx
  43. cmp edx, 0
  44. jne n1
  45. mov rdi, f
  46. call echo
  47. jmp n0
  48. n1:
  49. xor rdx, rdx
  50. mov eax, [main_x]
  51. mov ebx, 5
  52. idiv rbx
  53. cmp edx, 0
  54. jne n2
  55. mov rdi, b
  56. call echo
  57. jmp n0
  58. n2:
  59. mov rdi, n
  60. call echo
  61. n0:
  62. inc [main_x]
  63. jmp loop_1
  64. loop_1e:
  65. ;
  66. mov rax, linux_exit_system_call ; <main> return;;
  67. xor rdi, rdi
  68. syscall
  69. ;~string_length (character * text):
  70. ;~natural length = 0;
  71. ;~loop (* (text + length++));
  72. ;~return (length);;
  73. ;~48 C7 C7 BD 11 40 00 E8 CA 00 00 00 83 3D F3 10 00 00 10 0F 83 83 00 00 00 48 31 D2 8B 05 E4 10 00 00 BB 0F 00 00 00 48 F7 FB 83 FA 00 75 0E 48 C7 C7
  74. ;~CA 11 40 00 E8 9B 00 00 00 EB 54 48 31 D2 8B 05 C0 10 00 00 BB 03 00 00 00 48 F7 FB 83 FA 00 75 0E 48 C7 C7 D4 11 40 00 E8 77 00 00 00 EB 30 48 31 D2
  75. ;~8B 05 9C 10 00 00 BB 05 00 00 00 48 F7 FB 83 FA 00 75 0E 48 C7 C7 DA 11 40 00 E8 53 00 00 00 EB 0C 48 C7 C7 E0 11 40 00 E8 45 00 00 00 FF 05 6F 10 00
  76. ;~00 E9 70 FF FF FF 48 C7 C0 3C 00 00 00 48 31 FF 0F 05
  77. ;~57 C8 00 00 00 C7 05 48 10 00 00 00 00 00 00 48 31 C0 80 3F 00 74 0B FF 05 3A 10 00 00 48 FF C7 EB F0 C9 8B 05 2E 10 00 00 5F C3
  78. ;~57 C8 00 00 00 E8 C5 FF FF FF 48 89 C2 48 89 FE 48 C7 C7 01 00 00 00 48 C7 C0 01 00 00 00 0F 05 48 31 C0 C9 5F C3
  79. ;~00 00 00 00 01 00 00 00 48 65 79 6F 20 77 6F 72 6C 64 21 0A 00 66 69 7A 7A 62 75 7A 7A 0A 00 66 69 7A 7A 0A 00 62 75 7A 7A 0A 00 6E 75 6D 62 65 72 0A 00
  80. ;~string_length (character * string) > natural:
  81. ;~length: natural = 0;
  82. ;~loop (string [length]) length++;
  83. ;~return (length);;
  84. string_length: ; string_length
  85. begin
  86. mov [string_length_length], 0
  87. loop_0: ; loop_0
  88. cmp byte [rdi], 0 ; (* (text + length)) != '\0'
  89. je loop_0e
  90. inc [string_length_length] ; text + length
  91. inc rdi ; length++
  92. jmp loop_0 ; if not go loop_0
  93. loop_0e:
  94. mov eax, [string_length_length]
  95. end
  96. ;~echo (character * text):
  97. ;~system (linux_write_system_call, standard_output, text, string_length (text));
  98. ;~return;;
  99. echo:
  100. begin
  101. call string_length ; |
  102. mov rdx, rax ; | string_length (text)
  103. mov rsi, rdi ; text
  104. mov rdi, standard_output ; standard_output
  105. mov rax, linux_write_system_call ; linux_write_system_call
  106. syscall ; system (
  107. xor rax, rax ; return;;
  108. end
  109. segment readable writable
  110. ; variable
  111. string_length_length dd 0 ; natural length = 0;
  112. main_x dd 1 ; integer x = 0;
  113. ; data
  114. text_data db 'Heyo world!', 10, 0
  115. fb db 'fizzbuzz', 10, 0
  116. f db 'fizz', 10, 0
  117. b db 'buzz', 10, 0
  118. n db 'number', 10, 0