hex0glo.asm 1.0 KB

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