1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
## Copyright (C) 2017 Jeremiah Orians
## This file is part of stage0.
##
## stage0 is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## stage0 is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with stage0. If not, see <http://www.gnu.org/licenses/>.
DEFINE ADD_RAX_R14 4C01F0
DEFINE CMP_RAX_Immediate8 4883F8
DEFINE CMP_R15_Immediate8 4983FF
DEFINE JE8 74
DEFINE JNE8 75
DEFINE JGE8 7D
DEFINE JE32 0F84
DEFINE JL8 7C
DEFINE JMP8 EB
DEFINE JMP32 E9
DEFINE LOAD32I_RDX 48C7C2
DEFINE LOAD32I_RSI 48C7C6
DEFINE LOAD32I_RDI 48C7C7
DEFINE LOAD32I_RAX 48C7C0
DEFINE LOAD32I_R14 49C7C6
DEFINE LOAD32I_R15 49C7C7
DEFINE LOAD8_al_Absolute32 8A0425
DEFINE STORE8_al_Absolute32 880425
DEFINE SYSCALL 0F05
DEFINE SHL_R14_Immediate8 49C1E6
DEFINE TEST_RAX_RAX 4885C0
DEFINE MOVE_R14_RAX 4989C6
DEFINE MOVZBQ_RAX_AL 480FB6C0
DEFINE RETQ C3
DEFINE SUB_RAX_Immediate8 4883E8
DEFINE CALLI32 E8
DEFINE NULL 00000000
# Where the ELF Header is going to hit is :_start
:hex
# Purge Comment Lines (#)
CMP_RAX_Immediate8 !35
JE8 !purge_comment
# Purge Comment Lines (;)
CMP_RAX_Immediate8 !59
JE8 !purge_comment
# deal all ascii less than '0'
CMP_RAX_Immediate8 !48
JL8 !ascii_other
# deal with 0-9
CMP_RAX_Immediate8 !58
JL8 !ascii_num
# deal with all ascii less than 'A'
CMP_RAX_Immediate8 !65
JL8 !ascii_other
# deal with 'A'-'F'
CMP_RAX_Immediate8 !71
JL8 !ascii_high
# deal with all ascii less than 'a'
CMP_RAX_Immediate8 !97
JL8 !ascii_other
#deal with 'a'-'f'
CMP_RAX_Immediate8 !103
JL8 !ascii_low
# The rest that remains needs to be ignored
JMP8 !ascii_other
:purge_comment
# Attempt to read 1 byte from STDIN
LOAD32I_RDX %1 # set the size of chars we want
LOAD32I_RSI &input # Where to put it
LOAD32I_RDI %0 # Where are we reading from
LOAD32I_RAX %0 # the syscall number for read
SYSCALL # call the Kernel
TEST_RAX_RAX # check what we got
JE32 %Done # Got EOF call it done
# load byte
LOAD8_al_Absolute32 &input # load char
MOVZBQ_RAX_AL # We have to zero extend it to use it
# Loop if not LF
CMP_RAX_Immediate8 !10
JNE8 !purge_comment
# Otherwise return -1
LOAD32I_RAX %-1
RETQ
:ascii_num
SUB_RAX_Immediate8 !48
RETQ
:ascii_low
SUB_RAX_Immediate8 !87
RETQ
:ascii_high
SUB_RAX_Immediate8 !55
RETQ
:ascii_other
LOAD32I_RAX %-1
RETQ
# Our main function
:_start
# Our flag for byte processing
LOAD32I_R15 %-1
# temp storage for the sum
LOAD32I_R14 %0
:loop
# Attempt to read 1 byte from STDIN
LOAD32I_RDX %1 # set the size of chars we want
LOAD32I_RSI &input # Where to put it
LOAD32I_RDI %0 # Where are we reading from
LOAD32I_RAX %0 # the syscall number for read
SYSCALL # call the Kernel
TEST_RAX_RAX # check what we got
JE8 !Done # Got EOF call it done
# load byte
LOAD8_al_Absolute32 &input # load char
MOVZBQ_RAX_AL # We have to zero extend it to use it
# process byte
CALLI32 %hex
# deal with -1 values
CMP_RAX_Immediate8 !0
JL8 !loop
# deal with toggle
CMP_R15_Immediate8 !0
JGE8 !print
# process first byte of pair
MOVE_R14_RAX
LOAD32I_R15 %0
JMP8 !loop
# process second byte of pair
:print
# update the sum and store in output
SHL_R14_Immediate8 !4
ADD_RAX_R14
STORE8_al_Absolute32 &output
# flip the toggle
LOAD32I_R15 %-1
# Print our Hex
LOAD32I_RDX %1 # set the size of chars we want
LOAD32I_RSI &output # What we are writing
LOAD32I_RDI %1 # Stdout File Descriptor
LOAD32I_RAX %1 # the syscall number for write
SYSCALL # call the Kernel
JMP8 !loop
:Done
# program completed Successfully
LOAD32I_RDI %0 # All is well
LOAD32I_RAX %60 # put the exit syscall number in rax
SYSCALL # Call it a good day
# Where we are putting our output
:output
# Reserve 4bytes of Zeros
NULL
# Where we get our input
:input
# Reserve 4bytes of Zeros
NULL
:ELF_end
|