primitives.asm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ;@+leo-ver=5-thin
  2. ;@+node:caminhante.20211214151146.6: * @file primitives.asm
  3. ;@@nocolor
  4. ;@@language assembly_x86
  5. ;@@tabwidth -2
  6. ;@+others
  7. ;@-others
  8. ;; TODO
  9. ;; Return and Data Stack's pointer control
  10. PUSHRST: dd _enter_native
  11. lea ebp, [ebp-4]
  12. pop eax
  13. mov [ebp], eax
  14. jmp _next_routine
  15. POPRST: dd _enter_native
  16. push dword [ebp]
  17. lea ebp, [ebp+4]
  18. jmp _next_routine
  19. RSTFETCH: dd _enter_native
  20. push ebp
  21. jmp _next_routine
  22. RSTSTORE: dd _enter_native
  23. pop ebp
  24. jmp _next_routine
  25. RSTDROP: dd _enter_native
  26. lea ebp, [ebp+4]
  27. jmp _next_routine
  28. DSTFETCH: dd _enter_native
  29. mov eax, esp
  30. push eax
  31. jmp _next_routine
  32. DSTSTORE: dd _enter_native
  33. pop esp
  34. jmp _next_routine
  35. ;; Input/Output
  36. KEY: dd _enter_native
  37. call _KEY
  38. push eax
  39. jmp _next_routine
  40. _KEY:
  41. save_reg ebx,ecx,edx
  42. .start:
  43. movzx ebx, [read.cursor]
  44. movzx ecx, [read.top]
  45. cmp ebx, ecx
  46. jge .read_more
  47. movzx eax, byte [read.buffer+ebx]
  48. inc [read.cursor]
  49. restore_reg ebx,ecx,edx
  50. ret
  51. .read_more:
  52. mov eax, sys_read
  53. mov ebx, STDIN
  54. mov ecx, read.buffer
  55. mov edx, 4096
  56. int 80h
  57. test eax, eax
  58. jbe .read_error
  59. mov [read.top], ax
  60. mov [read.cursor], 0
  61. jmp _KEY.start
  62. .read_error:
  63. mov eax, -1
  64. restore_reg ebx,ecx,edx
  65. ret
  66. EMIT: dd _enter_native
  67. pop eax
  68. call _EMIT
  69. jmp _next_routine
  70. _EMIT:
  71. save_reg ebx,ecx,edx
  72. push eax
  73. mov ebx, STDOUT
  74. mov ecx, esp
  75. mov edx, 1
  76. mov eax, sys_write
  77. int 80h
  78. pop eax
  79. restore_reg ebx,ecx,edx
  80. ret
  81. WORD1: dd _enter_native
  82. call _WORD
  83. push word_buffer
  84. push ecx
  85. jmp _next_routine
  86. _WORD:
  87. save_reg edi,eax
  88. .start:
  89. mov edi, word_buffer
  90. mov ecx, 31
  91. .read_first:
  92. call _KEY
  93. cmp eax, -1
  94. je .error
  95. cmp al, '\'
  96. je .skip_comment
  97. cmp al, ' '
  98. je .read_first
  99. cmp al, 10
  100. je .read_first
  101. stosb
  102. .read_next:
  103. call _KEY
  104. cmp eax, -1
  105. je .error
  106. cmp al, '\'
  107. je .skip_comment
  108. cmp al, ' '
  109. je .end
  110. cmp al, 10
  111. je .end
  112. stosb
  113. loop .read_next
  114. .end:
  115. neg ecx
  116. add ecx, 32
  117. restore_reg edi,eax
  118. ret
  119. .error:
  120. mov ecx, -1
  121. restore_reg edi,eax
  122. ret
  123. .skip_comment:
  124. call _KEY
  125. cmp eax, -1
  126. je .error
  127. cmp al, 10
  128. jne .skip_comment
  129. jmp .end
  130. WRITE1: dd _enter_native
  131. save_reg eax,ebx,ecx,edx
  132. pop edx ecx
  133. test edx, edx
  134. js @f
  135. jz @f
  136. mov ebx, STDOUT
  137. mov eax, sys_write
  138. int 80h
  139. @@:
  140. restore_reg eax,ebx,ecx,edx
  141. jmp _next_routine
  142. ;@-leo