hex0_nocomments.asm 847 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. global _start
  2. section .bss
  3. buf dd 1
  4. oct dd 1
  5. section .text
  6. _start:
  7. call getchar
  8. cmp eax,-1
  9. je .done
  10. call hex_digit
  11. shl eax,4
  12. mov [oct],eax
  13. call getchar
  14. call hex_digit
  15. or [oct],eax
  16. mov eax,[oct]
  17. call putchar
  18. jmp _start
  19. .done:
  20. mov eax,0x01
  21. mov ebx,0
  22. int 0x80
  23. getchar:
  24. mov eax,0x03
  25. mov ebx,0 ;stdin fd
  26. mov ecx,buf
  27. mov edx,1 ;count
  28. int 0x80
  29. cmp eax,1
  30. jne .getchar_fail
  31. mov eax,[buf]
  32. ret
  33. .getchar_fail:
  34. mov eax,-1
  35. ret
  36. putchar:
  37. mov [buf],eax
  38. mov eax,0x04
  39. mov ebx,1 ;stdout fd
  40. mov ecx,buf
  41. mov edx,1
  42. int 0x80
  43. ret
  44. hex_digit:
  45. cmp eax,'0'
  46. jl .next1
  47. cmp eax,'9'
  48. jg .next1
  49. sub eax,'0'
  50. ret
  51. .next1:
  52. cmp eax,'a'
  53. jl .next2
  54. cmp eax,'f'
  55. jg .next2
  56. sub eax,('a'-10)
  57. ret
  58. .next2:
  59. cmp eax,'A'
  60. jl .die
  61. cmp eax,'F'
  62. jg .die
  63. sub eax,('A'-10)
  64. ret
  65. .die
  66. mov eax,0x01
  67. mov ebx,2
  68. int 0x80