hex.M1 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ## Copyright (C) 2017 Jeremiah Orians
  2. ## This file is part of stage0.
  3. ##
  4. ## stage0 is free software: you can 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. DEFINE ADD_ebp_TO_eax 01E8
  17. DEFINE CALLI32 E8
  18. DEFINE CMP_eax_Immediate8 83F8
  19. DEFINE CMP_edi_Immediate8 83FF
  20. DEFINE INT_80 CD80
  21. DEFINE JE32 0F84
  22. DEFINE JE8 74
  23. DEFINE JGE8 7D
  24. DEFINE JL8 7C
  25. DEFINE JMP32 E9
  26. DEFINE JMP8 EB
  27. DEFINE LOAD32I_eax B8
  28. DEFINE LOAD32I_ebp BD
  29. DEFINE LOAD32I_ebx BB
  30. DEFINE LOAD32I_ecx B9
  31. DEFINE LOAD32I_edi BF
  32. DEFINE LOAD32I_edx BA
  33. DEFINE LOAD8_al_Absolute32 A0
  34. DEFINE MOVE_eax_TO_ebp 89C5
  35. DEFINE MOVZBL_eax_al 0FB6C0
  36. DEFINE NULL 00000000
  37. DEFINE RET C3
  38. DEFINE SHL_ebp_Immediate8 C1E5
  39. DEFINE STORE8_al_Absolute32 A2
  40. DEFINE SUB_eax_Immediate8 83E8
  41. DEFINE TEST_eax_eax 85C0
  42. # Where the ELF Header is going to hit :_start first
  43. :read_byte
  44. # Attempt to read 1 byte from STDIN
  45. LOAD32I_edx %1 # set the size of chars we want
  46. LOAD32I_ecx &input # Where to put it
  47. LOAD32I_ebx %0 # Where are we reading from
  48. LOAD32I_eax %3 # the syscall number for read
  49. INT_80 # call the Kernel
  50. TEST_eax_eax # check what we got
  51. JE32 %Done # Got EOF call it done
  52. # load byte
  53. LOAD8_al_Absolute32 &input # load char
  54. MOVZBL_eax_al # We have to zero extend it to use it
  55. RET
  56. :print_byte
  57. # Print our first Hex
  58. LOAD32I_edx %1 # set the size of chars we want
  59. LOAD32I_ecx &output # What we are writing
  60. LOAD32I_ebx %1 # Stdout File Descriptor
  61. LOAD32I_eax %4 # the syscall number for write
  62. INT_80 # call the Kernel
  63. :hex
  64. # Deal with #
  65. CMP_eax_Immediate8 !35
  66. JE8 !ascii_comment
  67. # deal with ;
  68. CMP_eax_Immediate8 !59
  69. JE8 !ascii_comment
  70. # deal all ascii less than 0
  71. CMP_eax_Immediate8 !48
  72. JL8 !ascii_other
  73. # deal with 0-9
  74. CMP_eax_Immediate8 !58
  75. JL8 !ascii_num
  76. # deal with all ascii less than A
  77. CMP_eax_Immediate8 !65
  78. JL8 !ascii_other
  79. # deal with A-F
  80. CMP_eax_Immediate8 !71
  81. JL8 !ascii_high
  82. #deal with all ascii less than a
  83. CMP_eax_Immediate8 !97
  84. JL8 !ascii_other
  85. #deal with a-f
  86. CMP_eax_Immediate8 !103
  87. JL8 !ascii_low
  88. # The rest that remains needs to be ignored
  89. JMP8 !ascii_other
  90. :ascii_num
  91. SUB_eax_Immediate8 !48
  92. RET
  93. :ascii_low
  94. SUB_eax_Immediate8 !87
  95. RET
  96. :ascii_high
  97. SUB_eax_Immediate8 !55
  98. RET
  99. :ascii_other
  100. LOAD32I_eax %-1
  101. RET
  102. :ascii_comment
  103. CALLI32 %read_byte
  104. CMP_eax_Immediate8 !10
  105. JE8 !ascii_other
  106. JMP8 !ascii_comment
  107. :_start
  108. # Our flag for byte processing
  109. LOAD32I_edi %-1
  110. # temp storage for the sum
  111. LOAD32I_ebp %0
  112. :loop
  113. # Read in a byte
  114. CALLI32 %read_byte
  115. # process byte
  116. CALLI32 %hex
  117. # deal with -1 values
  118. CMP_eax_Immediate8 !0
  119. JL8 !loop
  120. # deal with toggle
  121. CMP_edi_Immediate8 !0
  122. JGE8 !print
  123. # process first byte of pair
  124. MOVE_eax_TO_ebp
  125. LOAD32I_edi %0
  126. JMP8 !loop
  127. # process second byte of pair
  128. :print
  129. # update the sum and store in output
  130. SHL_ebp_Immediate8 !4
  131. ADD_ebp_TO_eax
  132. STORE8_al_Absolute32 &output
  133. # flip the toggle
  134. LOAD32I_edi %-1
  135. CALLI32 %print_byte
  136. JMP8 !loop
  137. :Done
  138. # program completed Successfully
  139. LOAD32I_ebx %0 # All is well
  140. LOAD32I_eax %1 # put the exit syscall number in eax
  141. INT_80 # Call it a good day
  142. # Our writable space
  143. :input
  144. NULL
  145. :output
  146. NULL
  147. :ELF_end