hex0.asm 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. global _start
  2. section .data
  3. _start:
  4. call getchar
  5. cmp rax,-1
  6. je .done
  7. cmp rax,' '
  8. je _start
  9. cmp rax,0x09
  10. je _start
  11. cmp rax,0x0A
  12. je _start
  13. cmp rax,'#'
  14. je skip_comment
  15. call hex_digit
  16. shl rax,4
  17. mov [rel oct],rax
  18. call getchar
  19. call hex_digit
  20. or [rel oct],rax
  21. mov rax,[rel oct]
  22. call putchar
  23. jmp _start
  24. .done:
  25. mov rax,60
  26. mov rdi,0
  27. syscall
  28. skip_comment:
  29. call getchar
  30. cmp rax,-1
  31. je die
  32. cmp rax,0x0A
  33. je _start
  34. jmp skip_comment
  35. hex_digit:
  36. cmp rax,'0'
  37. jl .next1
  38. cmp rax,'9'
  39. jg .next1
  40. sub rax,'0'
  41. ret
  42. .next1:
  43. cmp rax,'a'
  44. jl .next2
  45. cmp rax,'f'
  46. jg .next2
  47. sub rax,('a'-10)
  48. ret
  49. .next2:
  50. cmp rax,'A'
  51. jl die
  52. cmp rax,'F'
  53. jg die
  54. sub rax,('A'-10)
  55. ret
  56. die
  57. mov rax,60
  58. mov rdi,1
  59. syscall
  60. getchar:
  61. mov rax,0x00
  62. mov rdi,0 ;stdin fd
  63. lea rsi,[rel buf]
  64. mov rdx,1 ;count
  65. syscall
  66. cmp rax,1
  67. jne .getchar_fail
  68. mov rax,0
  69. mov al,[rel buf]
  70. ret
  71. .getchar_fail:
  72. mov rax,-1
  73. ret
  74. putchar:
  75. mov [rel buf],al
  76. mov rax,0x01
  77. mov rdi,1 ;stdout fd
  78. lea rsi,[rel buf]
  79. mov rdx,1
  80. syscall
  81. ret
  82. buf: db 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  83. oct: db 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00