hex0globuflbl.asm 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. section .bss
  2. buf resq 1
  3. oct resq 1
  4. len resq 1
  5. bin resb 4096
  6. num resq 1
  7. wrd resq 4096
  8. section .data
  9. global _start
  10. _start:
  11. call getchar
  12. cmp rax,-1
  13. je .done
  14. cmp rax,' '
  15. je _start
  16. cmp rax,0x09
  17. je _start
  18. cmp rax,0x0A
  19. je _start
  20. cmp rax,'#'
  21. jne .dont_skip_comment
  22. call skip_comment
  23. jmp _start
  24. .dont_skip_comment:
  25. cmp rax,':'
  26. jne .dont_read_label
  27. call read_word
  28. mov rax, '!'
  29. call putchar
  30. jmp _start
  31. .dont_read_label:
  32. call hex_digit
  33. shl rax,4
  34. mov [oct],rax
  35. call getchar
  36. call hex_digit
  37. or [oct],rax
  38. mov rax,[oct]
  39. mov rbx,[len]
  40. mov byte [bin+rbx],al
  41. inc rbx
  42. mov [len],rbx
  43. jmp _start
  44. .done:
  45. mov rax, 1 ; sys_write
  46. mov rdi, 2 ; stdout=2?
  47. mov rsi, bin
  48. mov rdx, 0
  49. mov word dx, [len]
  50. syscall
  51. ; mov rax,0
  52. ; mov rbx,[len]
  53. ; call putchar
  54. ; inc rax
  55. ; cmp rax,rbx
  56. ; jl .done
  57. mov rax,60
  58. mov rdi,0
  59. syscall
  60. die
  61. mov rax,60
  62. mov rdi,1
  63. syscall
  64. skip_comment:
  65. call getchar
  66. cmp rax,-1
  67. je die
  68. cmp rax,0x0A
  69. jne skip_comment
  70. ret
  71. hex_digit:
  72. cmp rax,'0'
  73. jl .next1
  74. cmp rax,'9'
  75. jg .next1
  76. sub rax,'0'
  77. ret
  78. .next1:
  79. cmp rax,'a'
  80. jl .next2
  81. cmp rax,'f'
  82. jg .next2
  83. sub rax,('a'-10)
  84. ret
  85. .next2:
  86. cmp rax,'A'
  87. jl die
  88. cmp rax,'F'
  89. jg die
  90. sub rax,('A'-10)
  91. ret
  92. read_word
  93. mov rbx,[num]
  94. mov rax,rbx
  95. add rax,1
  96. mov [num],rax
  97. shl rbx,5 ;; = *32
  98. lea rbx,[wrd + rbx]
  99. .loop:
  100. call getchar
  101. cmp rax,-1
  102. je .done
  103. cmp rax,' '
  104. je .done
  105. cmp rax,0x0A
  106. je .done
  107. mov byte [rbx],al
  108. inc rbx
  109. jmp .loop
  110. .done
  111. mov byte [rbx],0
  112. ret
  113. getchar:
  114. mov rax,0x00
  115. mov rdi,0 ;stdin fd
  116. lea rsi,[buf]
  117. mov rdx,1 ;count
  118. syscall
  119. cmp rax,1
  120. jne .getchar_fail
  121. mov rax,0
  122. mov al,[buf]
  123. ret
  124. .getchar_fail:
  125. mov rax,-1
  126. ret
  127. putchar:
  128. mov [buf],al
  129. mov rax,0x01
  130. mov rdi,1 ;stdout fd
  131. lea rsi,[buf]
  132. mov rdx,1
  133. syscall
  134. ret