hex0globuf.asm 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. section .bss
  2. buf resq 1
  3. oct resq 1
  4. len resq 2
  5. bin resb 4096
  6. section .data
  7. global _start
  8. _start:
  9. call getchar
  10. cmp rax,-1
  11. je .done
  12. cmp rax,' '
  13. je _start
  14. cmp rax,0x09
  15. je _start
  16. cmp rax,0x0A
  17. je _start
  18. cmp rax,'#'
  19. je skip_comment
  20. call hex_digit
  21. shl rax,4
  22. mov [oct],rax
  23. call getchar
  24. call hex_digit
  25. or [oct],rax
  26. mov rax,[oct]
  27. mov word bx,[len]
  28. mov byte [bin+rbx],al
  29. inc rbx
  30. mov [len],bx
  31. jmp _start
  32. .done:
  33. mov rax, 1 ; sys_write
  34. mov rdi, 2 ; stdout=2?
  35. mov rsi, bin
  36. mov rdx, 0
  37. mov word dx, [len]
  38. syscall
  39. ; mov rax,0
  40. ; mov rbx,[len]
  41. ; call putchar
  42. ; inc rax
  43. ; cmp rax,rbx
  44. ; jl .done
  45. mov rax,60
  46. mov rdi,0
  47. syscall
  48. skip_comment:
  49. call getchar
  50. cmp rax,-1
  51. je die
  52. cmp rax,0x0A
  53. je _start
  54. jmp skip_comment
  55. hex_digit:
  56. cmp rax,'0'
  57. jl .next1
  58. cmp rax,'9'
  59. jg .next1
  60. sub rax,'0'
  61. ret
  62. .next1:
  63. cmp rax,'a'
  64. jl .next2
  65. cmp rax,'f'
  66. jg .next2
  67. sub rax,('a'-10)
  68. ret
  69. .next2:
  70. cmp rax,'A'
  71. jl die
  72. cmp rax,'F'
  73. jg die
  74. sub rax,('A'-10)
  75. ret
  76. die
  77. mov rax,60
  78. mov rdi,1
  79. syscall
  80. getchar:
  81. mov rax,0x00
  82. mov rdi,0 ;stdin fd
  83. lea rsi,[buf]
  84. mov rdx,1 ;count
  85. syscall
  86. cmp rax,1
  87. jne .getchar_fail
  88. mov rax,0
  89. mov al,[buf]
  90. ret
  91. .getchar_fail:
  92. mov rax,-1
  93. ret
  94. putchar:
  95. mov [buf],al
  96. mov rax,0x01
  97. mov rdi,1 ;stdout fd
  98. lea rsi,[buf]
  99. mov rdx,1
  100. syscall
  101. ret