scanf.asm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. %xdefine chr (rbp-8)
  2. %xdefine tmp (rbp-16)
  3. %include "macroses.inc"
  4. global Fscanf
  5. global ReadString
  6. global ReadDecimal
  7. ;| input
  8. ;rax == buf
  9. ;rdi == fd
  10. ;| output
  11. ;rdx == last read char
  12. ReadDecimal:
  13. push rbp
  14. mov rbp, rsp
  15. sub rsp, 8
  16. mov qword [chr], 0
  17. push rcx
  18. push rbx
  19. xor rcx, rcx
  20. xor rbx, rbx
  21. .next_iter:
  22. READ_CHAR chr, rdi
  23. CHECK_SPECIAL [chr]
  24. je .is_first_char
  25. cmp byte [chr], '-'
  26. je .is_neg
  27. CHECK_NUMBER [chr]
  28. je .is_number
  29. jmp .ret
  30. .is_first_char:
  31. cmp rcx, 0
  32. je .next_iter
  33. jmp .ret
  34. .is_neg:
  35. cmp rcx, 0
  36. ja .ret
  37. neg qword [rax]
  38. jmp .next_iter
  39. .is_number:
  40. sub byte [chr], 48
  41. mov rbx, [rax]
  42. imul rbx, 10
  43. add rbx, [chr]
  44. mov [rax], rbx
  45. inc rcx
  46. jmp .next_iter
  47. .ret:
  48. mov rdx, [chr]
  49. pop rbx
  50. pop rcx
  51. add rsp, 8
  52. pop rbp
  53. ret
  54. ;| input
  55. ;rax == buf
  56. ;rdi == fd
  57. ;| output
  58. ;rdx == last read char
  59. ReadString:
  60. push rbp
  61. mov rbp, rsp
  62. sub rsp, 16
  63. mov qword [chr], 0
  64. push rbx
  65. mov [tmp], rax
  66. .next_iter:
  67. READ_CHAR chr, rdi
  68. CHECK_SPECIAL [chr]
  69. je .is_first_char
  70. mov bl, [chr]
  71. mov [rax], bl
  72. inc rax
  73. jmp .next_iter
  74. .is_first_char:
  75. cmp [tmp], rax
  76. je .next_iter
  77. jmp .ret
  78. .ret:
  79. mov rdx, [chr]
  80. pop rbx
  81. add rsp, 16
  82. pop rbp
  83. ret
  84. ;| input
  85. ; rdi == fd
  86. ; rax == format
  87. ; arg == stack
  88. ;| output
  89. ; rax == number proccesing chars
  90. Fscanf:
  91. push rbp
  92. mov rbp, rsp
  93. sub rsp, 8
  94. mov qword [chr], 0
  95. push rcx
  96. push rdx
  97. push rbx
  98. push rdi
  99. xor rbx, rbx
  100. xor rcx, rcx
  101. xor rdx, rdx
  102. .next_iter:
  103. cmp [rax], byte 0x0
  104. je .ret
  105. cmp [rax], byte '%'
  106. je .format_char
  107. mov cl, [rax]
  108. mov [chr], cl
  109. CHECK_SPECIAL [chr]
  110. je .special_char
  111. jmp .default_char
  112. .format_char:
  113. inc rax
  114. cmp [rax], byte 's'
  115. je .read_string
  116. cmp [rax], byte 'd'
  117. je .read_decimal
  118. cmp [rax], byte 'c'
  119. je .read_char
  120. jmp .ret
  121. .read_string:
  122. push rax
  123. mov rax, [next_arg]
  124. cmp dl, 0x0
  125. je .skip_push_char
  126. mov [chr], dl
  127. CHECK_SPECIAL [chr]
  128. je .skip_push_char
  129. mov [rax], dl
  130. inc rax
  131. .skip_push_char:
  132. call ReadString
  133. pop rax
  134. inc rbx
  135. xor rdx, rdx
  136. jmp .next_step
  137. .read_decimal:
  138. push rax
  139. mov rax, [next_arg]
  140. call ReadDecimal
  141. pop rax
  142. inc rbx
  143. jmp .next_step
  144. .read_char:
  145. push rax
  146. cmp dl, 0x0
  147. je .skip_push_char2
  148. mov [chr], dl
  149. CHECK_SPECIAL [chr]
  150. je .skip_push_char2
  151. mov rcx, [next_arg]
  152. mov [rcx], dl
  153. xor rdx, rdx
  154. jmp .read_char_end
  155. .skip_push_char2:
  156. READ_CHAR chr, rdi
  157. CHECK_SPECIAL [chr]
  158. je .skip_push_char2
  159. mov al, [chr]
  160. mov rcx, [next_arg]
  161. mov [rcx], al
  162. .read_char_end:
  163. pop rax
  164. inc rbx
  165. jmp .next_step
  166. .special_char:
  167. inc rax
  168. mov cl, [rax]
  169. mov [chr], cl
  170. CHECK_SPECIAL [chr]
  171. je .special_char
  172. jmp .next_iter
  173. .default_char:
  174. cmp dl, 0x0
  175. je .skip_push_char3
  176. cmp dl, [rax]
  177. je .del_dl
  178. .del_dl:
  179. xor rdx, rdx
  180. jmp .next_step
  181. jmp .ret
  182. .skip_push_char3:
  183. READ_CHAR chr, rdi
  184. mov cl, [chr]
  185. cmp cl, [rax]
  186. je .next_step
  187. jmp .ret
  188. .next_step:
  189. inc rax
  190. jmp .next_iter
  191. .ret:
  192. mov rax, rbx
  193. pop rdi
  194. pop rbx
  195. pop rdx
  196. pop rcx
  197. add rsp, 8
  198. pop rbp
  199. ret