hex2.asm 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. cmp rax,':'
  19. je read_label
  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. call putchar
  28. jmp _start
  29. .done:
  30. mov rax,60
  31. mov rdi,0
  32. syscall
  33. skip_comment:
  34. call getchar
  35. cmp rax,-1
  36. je die
  37. cmp rax,0x0A
  38. je _start
  39. jmp skip_comment
  40. read_label:
  41. jmp skip_comment
  42. hex_digit:
  43. cmp rax,'0'
  44. jl .next1
  45. cmp rax,'9'
  46. jg .next1
  47. sub rax,'0'
  48. ret
  49. .next1:
  50. cmp rax,'a'
  51. jl .next2
  52. cmp rax,'f'
  53. jg .next2
  54. sub rax,('a'-10)
  55. ret
  56. .next2:
  57. cmp rax,'A'
  58. jl die
  59. cmp rax,'F'
  60. jg die
  61. sub rax,('A'-10)
  62. ret
  63. die
  64. mov rax,60
  65. mov rdi,1
  66. syscall
  67. getchar:
  68. mov rax,0x00
  69. mov rdi,0 ;stdin fd
  70. lea rsi,[buf]
  71. mov rdx,1 ;count
  72. syscall
  73. cmp rax,1
  74. jne .getchar_fail
  75. mov rax,0
  76. mov al,[buf]
  77. ret
  78. .getchar_fail:
  79. mov rax,-1
  80. ret
  81. putchar:
  82. mov [buf],al
  83. mov rax,0x01
  84. mov rdi,1 ;stdout fd
  85. lea rsi,[buf]
  86. mov rdx,1
  87. syscall
  88. ret