## Copyright (C) 2016 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/>.

## ELF Header
7F 45 4C 46               ## e_ident[EI_MAG0-3] ELF's magic number
02                        ## e_ident[EI_CLASS] Indicating 64 bit
01                        ## e_ident[EI_DATA] Indicating little endianness
01                        ## e_ident[EI_VERSION] Indicating original elf
00                        ## e_ident[EI_OSABI] Set at 0 because none cares
00                        ## e_ident[EI_ABIVERSION] See above
00 00 00 00 00 00 00      ## e_ident[EI_PAD]
02 00                     ## e_type Indicating Executable
3E 00                     ## e_machine Indicating AMD64
01 00 00 00               ## e_version Indicating original elf
78 00 60 00 00 00 00 00
40 00 00 00 00 00 00 00   ## e_phoff Address of program header table
30 01 00 00 00 00 00 00
00 00 00 00               ## e_flags
40 00                     ## e_ehsize Indicating our 64 Byte header
38 00                     ## e_phentsize size of a program header table
01 00                     ## e_phnum number of entries in program table
00 00                     ## e_shentsize size of a section header table
00 00                     ## e_shnum number of entries in section table
00 00                     ## e_shstrndx index of the section names

## Program Header table
01 00 00 00               ## p_type
06 00 00 00               ## Flags
00 00 00 00 00 00 00 00   ## p_offset
00 00 60 00 00 00 00 00   ## p_vaddr
00 00 00 00 00 00 00 00   ## Undefined
18 01 00 00 00 00 00 00   ## p_filesz
18 01 00 00 00 00 00 00   ## p_memsz
00 00 20 00 00 00 00 00   ## Required alignment


## _start
## loop
	# Attempt to read a single byte from STDIN
	48 c7 c2 01 00 00 00  # mov $1, %rdx        ; set the size of chars we want
	48 c7 c6 17 01 60 00  # mov $input, %rsi    ; Where to put it
	48 c7 c7 00 00 00 00  # mov $0, %rdi        ; Where are we reading from
	48 c7 c0 00 00 00 00  # mov $0, %rax        ; the syscall number for read
	0f 05                 # syscall             ; call the Kernel

	# If we didn't read any bytes jump to Done
	48 85 c0               # test %rax, %rax     ; check what we got
	74 5b                  # jz Done             ; Got EOF call it done

	# Move our byte into registers for processing
	8a 04 25 17 01 60 00   # movb input, %al     ; load char
	4c 0f b6 e0            # movzx %al, %r12     ; We have to zero extend it to use it
	4c 0f b6 e8            # movzx %al, %r13     ; We have to zero extend it to use it

	# Break out the nibbles
	49 c1 ec 04            # shr $4, %r12        ; Purge the bottom 4 bits
	49 83 e5 0f            # and $0xF, %r13      ; Chop off all but the bottom 4 bits

	# add our base pointer
	49 81 c4 06 01 60 00   # add $output, %r12   ; Use that as our index into our array
	49 81 c5 06 01 60 00   # add $output, %r13   ; Use that as our index into our array

	# Print our first Hex
	48 c7 c2 01 00 00 00   # mov $1, %rdx        ; set the size of chars we want
	4c 89 e6               # mov %r12, %rsi      ; What we are writing
	48 c7 c7 01 00 00 00   # mov $1, %rdi        ; Stdout File Descriptor
	48 c7 c0 01 00 00 00   # mov $1, %rax        ; the syscall number for write
	0f 05                  # syscall             ; call the Kernel

	# Print our second Hex
	48 c7 c2 01 00 00 00   # mov $1, %rdx        ; set the size of chars we want
	4c 89 ee               # mov %r13, %rsi      ; What we are writing
	48 c7 c7 01 00 00 00   # mov $1, %rdi        ; Stdout File Descriptor
	48 c7 c0 01 00 00 00   # mov $1, %rax        ; the syscall number for write
	0f 05                  # syscall             ; call the Kernel
	eb 82                  # jmp loop


## Done
	# program completed Successfully
	48 c7 c7 00 00 00 00   # mov $0, %rdi        ; All is well
	48 c7 c0 3c 00 00 00   # mov $60, %rax       ; put the exit syscall number in eax
	0f 05                  # syscall             ; Call it a good day

## Hex output chars
# "0123456789ABCDEF\n"
30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 0A

## Where the byte we are reading is shoved
00
