Converted xeh to Hex

This commit is contained in:
Jeremiah Orians 2016-05-02 20:47:57 -04:00
parent 25c9e25e65
commit 8ebf08d91a
No known key found for this signature in database
GPG Key ID: 7457821534D2ACCD
4 changed files with 157 additions and 16 deletions

View File

@ -29,7 +29,7 @@ F0 01 00 00 00 00 00 00 ## e_shoff Address of section header table
05 00 00 00 ## Flags
00 00 00 00 00 00 00 00 ## p_offset
00 00 40 00 00 00 00 00 ## p_vaddr
00 00 40 00 00 00 00 00 ## Undefined
00 00 00 00 00 00 00 00 ## Undefined
B1 00 00 00 00 00 00 00 ## p_filesz
B1 00 00 00 00 00 00 00 ## p_memsz
00 00 20 00 00 00 00 00 ## Required alignment

76
read.s Normal file
View File

@ -0,0 +1,76 @@
.data
# we must export the entry point to the ELF linker or loader.
# They convientionally recognize _start as their entry point.
# Use ld -e main to override the default if you wish
.global _start
_start:
# first check that we got the correct number of inputs
pop %rax # Get the number of arguments
pop %rdi # Get the program name
pop %rdi # Get the actual argument
# Check if we have the correct number of inputs
cmp $2, %rax
# Jump to Bail if the number is not correct
jne Bail
# attempt to open the file for reading
mov $0, %rsi # prepare read_only
# we already have what we need in ebx
mov $2, %rax # the syscall number for open()
syscall # call the Kernel
# Check if we have a valid file
test %rax, %rax
# Jump to Bail_file if not actual file
js Bail
mov %rax, %rdi # move the pointer to the right location
Circle: #print contents of file
mov $read_size, %rdx # set the size of chars we want
mov $buffer, %rsi # Where to put it
# We already have what we need in ebx
mov $0, %rax # the syscall number for read
syscall # call the Kernel
test %rax, %rax # check what we got
jz Done # Got EOF call it done
# Make sure we don't write a bunch of NULLs
mov %rax, %rdx
# get file pointer out of the way
movq %rdi, %rsp
# edx was already setup
mov $1, %rdi # setup stdout write
mov $1, %rax # setup the write
syscall # call the Kernel
#now to prepare for next loop
movq %rsp, %rdi
jmp Circle
Done:
# program completed Successfully
mov $0, %rdi # All is well
mov $60, %rax # put the exit syscall number in eax
syscall # Call it a good day
Bail:
# terminate with an error
mov $1, %rdi # there was an error
mov $60, %rax # put the exit syscall number in eax
syscall # bail out
# Our writable space
# 2^ 30 Should be enough per read
read_size = 1073741824
buffer:
.space 1

71
xeh1.hex Normal file
View File

@ -0,0 +1,71 @@
## 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
48 c7 c2 01 00 00 00 # mov $0x1,%rdx
48 c7 c6 17 01 60 00 # mov $0x600117,%rsi
48 c7 c7 00 00 00 00 # mov $0x0,%rdi
48 c7 c0 00 00 00 00 # mov $0x0,%rax
0f 05 # syscall
48 85 c0 # test %rax,%rax
74 5b # je 6000f6 <Done>
8a 04 25 17 01 60 00 # mov 0x600117,%al
4c 0f b6 e0 # movzbq %al,%r12
4c 0f b6 e8 # movzbq %al,%r13
49 c1 ec 04 # shr $0x4,%r12
49 83 e5 0f # and $0xf,%r13
49 81 c4 06 01 60 00 # add $0x600106,%r12
49 81 c5 06 01 60 00 # add $0x600106,%r13
48 c7 c2 01 00 00 00 # mov $0x1,%rdx
4c 89 e6 # mov %r12,%rsi
48 c7 c7 01 00 00 00 # mov $0x1,%rdi
48 c7 c0 01 00 00 00 # mov $0x1,%rax
0f 05 # syscall
48 c7 c2 01 00 00 00 # mov $0x1,%rdx
4c 89 ee # mov %r13,%rsi
48 c7 c7 01 00 00 00 # mov $0x1,%rdi
48 c7 c0 01 00 00 00 # mov $0x1,%rax
0f 05 # syscall
eb 82 # jmp 600078 <_start>
## Done
48 c7 c7 00 00 00 00 # mov $0x0,%rdi
48 c7 c0 3c 00 00 00 # mov $0x3c,%rax
0f 05 # syscall
## Hex output chars
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

24
xeh1.s
View File

@ -1,7 +1,4 @@
.text # section declaration
output: .byte 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x0A
.data
# we must export the entry point to the ELF linker or loader.
# They convientionally recognize _start as their entry point.
# Use ld -e main to override the default if you wish
@ -14,7 +11,7 @@ loop:
mov $input, %rsi # Where to put it
mov $0, %rdi # Where are we reading from
mov $0, %rax # the syscall number for read
syscall # call the Kernel
syscall # call the Kernel
# If we didn't read any bytes jump to Done
test %rax, %rax # check what we got
@ -32,32 +29,29 @@ loop:
# add our base pointer
add $output, %r12 # Use that as our index into our array
add $output, %r13 # Use that as our index into our array
# Print our first Hex
mov $1, %rdx # set the size of chars we want
mov %r12, %rsi # What we are writing
mov $1, %rdi # Stdout File Descriptor
mov $1, %rax # the syscall number for write
syscall # call the Kernel
syscall # call the Kernel
# Print our second Hex
mov $1, %rdx # set the size of chars we want
mov %r13, %rsi # What we are writing
mov $1, %rdi # Stdout File Descriptor
mov $1, %rax # the syscall number for write
syscall # call the Kernel
syscall # call the Kernel
jmp loop
Done:
Done:
# program completed Successfully
mov $0, %rdi # All is well
mov $60, %rax # put the exit syscall number in eax
syscall # Call it a good day
.data
syscall # Call it a good day
write_size = 2
output: .byte 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x0A
input:
.byte write_size