hex0.s 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ## Copyright (C) 2016 Jeremiah Orians
  2. ## This file is part of stage0.
  3. ##
  4. ## stage0 is free software: you an redistribute it and/or modify
  5. ## it under the terms of the GNU General Public License as published by
  6. ## the Free Software Foundation, either version 3 of the License, or
  7. ## (at your option) any later version.
  8. ##
  9. ## stage0 is distributed in the hope that it will be useful,
  10. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ## GNU General Public License for more details.
  13. ##
  14. ## You should have received a copy of the GNU General Public License
  15. ## along with stage0. If not, see <http://www.gnu.org/licenses/>.
  16. # Our writable space
  17. .data
  18. .global _start
  19. hex:
  20. # Purge Comment Lines
  21. cmp $35, %rax
  22. je purge_comment
  23. # deal all ascii less than 0
  24. cmp $48, %rax
  25. jl ascii_other
  26. # deal with 0-9
  27. cmp $58, %rax
  28. jl ascii_num
  29. # deal with all ascii less than A
  30. cmp $65, %rax
  31. jl ascii_other
  32. # deal with A-F
  33. cmp $71, %rax
  34. jl ascii_high
  35. #deal with all ascii less than a
  36. cmp $97, %rax
  37. jl ascii_other
  38. #deal with a-f
  39. cmp $103, %rax
  40. jl ascii_low
  41. # The rest that remains needs to be ignored
  42. jmp ascii_other
  43. purge_comment:
  44. # Attempt to read 1 byte from STDIN
  45. mov $1, %rdx # set the size of chars we want
  46. mov $input, %rsi # Where to put it
  47. mov $0, %rdi # Where are we reading from
  48. mov $0, %rax # the syscall number for read
  49. syscall # call the Kernel
  50. test %rax, %rax # check what we got
  51. jz Done # Got EOF call it done
  52. # load byte
  53. movb input, %al # load char
  54. movzx %al, %rax # We have to zero extend it to use it
  55. # Loop if not LF
  56. cmp $10, %rax
  57. jne purge_comment
  58. # Otherwise return -1
  59. mov $-1, %rax
  60. ret
  61. ascii_num:
  62. sub $48, %rax
  63. ret
  64. ascii_low:
  65. sub $87, %rax
  66. ret
  67. ascii_high:
  68. sub $55, %rax
  69. ret
  70. ascii_other:
  71. mov $-1, %rax
  72. ret
  73. _start:
  74. # Our flag for byte processing
  75. mov $-1, %r15
  76. # temp storage for the sum
  77. mov $0, %r14
  78. loop:
  79. # Attempt to read 1 byte from STDIN
  80. mov $1, %rdx # set the size of chars we want
  81. mov $input, %rsi # Where to put it
  82. mov $0, %rdi # Where are we reading from
  83. mov $0, %rax # the syscall number for read
  84. syscall # call the Kernel
  85. test %rax, %rax # check what we got
  86. jz Done # Got EOF call it done
  87. # load byte
  88. movb input, %al # load char
  89. movzx %al, %rax # We have to zero extend it to use it
  90. # process byte
  91. call hex
  92. # deal with -1 values
  93. cmp $0, %rax
  94. jl loop
  95. # deal with toggle
  96. cmp $0, %r15
  97. jge print
  98. # process first byte of pair
  99. mov %rax, %r14
  100. mov $0, %r15
  101. jmp loop
  102. # process second byte of pair
  103. print:
  104. # update the sum and store in output
  105. shl $4, %r14
  106. add %r14, %rax
  107. mov %al, output
  108. # flip the toggle
  109. mov $-1, %r15
  110. # Print our first Hex
  111. mov $1, %rdx # set the size of chars we want
  112. mov $output, %rsi # What we are writing
  113. mov $1, %rdi # Stdout File Descriptor
  114. mov $1, %rax # the syscall number for write
  115. syscall # call the Kernel
  116. jmp loop
  117. Done:
  118. # program completed Successfully
  119. mov $0, %rdi # All is well
  120. mov $60, %rax # put the exit syscall number in eax
  121. syscall # Call it a good day
  122. read_size = 2
  123. input:
  124. .byte read_size
  125. output:
  126. .byte 0x00