Initial starting point for the creation of our hex bootstrap toolchain
This commit is contained in:
commit
02d5888f16
|
@ -0,0 +1,2 @@
|
|||
bin/
|
||||
tmp/
|
|
@ -0,0 +1,50 @@
|
|||
.text # section declaration
|
||||
msg:
|
||||
.string "needs a proper file name\n"
|
||||
len = . - msg # length of our dear string
|
||||
|
||||
# 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
|
||||
|
||||
# Load our preferred mode
|
||||
mov $0755, %rsi
|
||||
|
||||
# Load the syscall number for chmod
|
||||
mov $90, %rax
|
||||
|
||||
# Call the kernel
|
||||
syscall
|
||||
|
||||
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:
|
||||
# first let the user know what was wrong
|
||||
mov $len,%rdx # third argument: message length
|
||||
mov $msg,%rsi # second argument: pointer to message to write
|
||||
mov $1,%rdi # first argument: file handle (stdout)
|
||||
mov $1,%rax # system call number (sys_write)
|
||||
syscall # call kernel
|
||||
|
||||
# Second terminate with an error
|
||||
mov $1, %rdi # there was an error
|
||||
mov $60, %rax # put the exit syscall number in eax
|
||||
syscall # bail out
|
||||
|
|
@ -0,0 +1,137 @@
|
|||
.text # section declaration
|
||||
|
||||
# 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
|
||||
hex:
|
||||
# Purge Comment Lines
|
||||
cmp $35, %rax
|
||||
je purge_comment
|
||||
# deal all ascii less than 0
|
||||
cmp $48, %rax
|
||||
jl ascii_other
|
||||
# deal with 0-9
|
||||
cmp $58, %rax
|
||||
jl ascii_num
|
||||
# deal with all ascii less than A
|
||||
cmp $65, %rax
|
||||
jl ascii_other
|
||||
# deal with A-F
|
||||
cmp $71, %rax
|
||||
jl ascii_high
|
||||
#deal with all ascii less than a
|
||||
cmp $97, %rax
|
||||
jl ascii_other
|
||||
#deal with a-f
|
||||
cmp $103, %rax
|
||||
jl ascii_low
|
||||
# The rest that remains needs to be ignored
|
||||
jmp ascii_other
|
||||
|
||||
purge_comment:
|
||||
# Attempt to read 1 byte from STDIN
|
||||
mov $1, %rdx # set the size of chars we want
|
||||
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
|
||||
|
||||
test %rax, %rax # check what we got
|
||||
jz Done # Got EOF call it done
|
||||
|
||||
# load byte
|
||||
movb input, %al # load char
|
||||
movzx %al, %rax # We have to zero extend it to use it
|
||||
|
||||
# Loop if not LF
|
||||
cmp $10, %rax
|
||||
jne purge_comment
|
||||
|
||||
# Otherwise return -1
|
||||
mov $-1, %rax
|
||||
ret
|
||||
|
||||
ascii_num:
|
||||
sub $48, %rax
|
||||
ret
|
||||
ascii_low:
|
||||
sub $87, %rax
|
||||
ret
|
||||
ascii_high:
|
||||
sub $55, %rax
|
||||
ret
|
||||
ascii_other:
|
||||
mov $-1, %rax
|
||||
ret
|
||||
|
||||
_start:
|
||||
# Our flag for byte processing
|
||||
mov $-1, %r15
|
||||
|
||||
# temp storage for the sum
|
||||
mov $0, %r14
|
||||
loop:
|
||||
|
||||
# Attempt to read 1 byte from STDIN
|
||||
mov $1, %rdx # set the size of chars we want
|
||||
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
|
||||
|
||||
test %rax, %rax # check what we got
|
||||
jz Done # Got EOF call it done
|
||||
|
||||
# load byte
|
||||
movb input, %al # load char
|
||||
movzx %al, %rax # We have to zero extend it to use it
|
||||
|
||||
# process byte
|
||||
call hex
|
||||
|
||||
# deal with -1 values
|
||||
cmp $0, %rax
|
||||
jl loop
|
||||
|
||||
# deal with toggle
|
||||
cmp $0, %r15
|
||||
jge print
|
||||
|
||||
# process first byte of pair
|
||||
mov %rax, %r14
|
||||
mov $0, %r15
|
||||
jmp loop
|
||||
|
||||
# process second byte of pair
|
||||
print:
|
||||
# update the sum and store in output
|
||||
shl $4, %r14
|
||||
add %r14, %rax
|
||||
mov %al, output
|
||||
|
||||
# flip the toggle
|
||||
mov $-1, %r15
|
||||
|
||||
# Print our first Hex
|
||||
mov $1, %rdx # set the size of chars we want
|
||||
mov $output, %rsi # What we are writing
|
||||
mov $1, %rdi # Stdout File Descriptor
|
||||
mov $1, %rax # the syscall number for write
|
||||
syscall # call the Kernel
|
||||
|
||||
jmp loop
|
||||
|
||||
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
|
||||
|
||||
# Our writable space
|
||||
.data
|
||||
read_size = 2
|
||||
input:
|
||||
.byte read_size
|
||||
output:
|
||||
.byte 0x00
|
|
@ -0,0 +1,12 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Build new
|
||||
./hex2 < hex2_1.hex | sponge trial && ./exec_enable trial
|
||||
|
||||
# Test compile
|
||||
./trial < foo > example2
|
||||
readelf -a trial > summary2
|
||||
|
||||
# Check results
|
||||
sha256sum example* summary*
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
.text # section declaration
|
||||
output: .byte 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x0A
|
||||
|
||||
|
||||
# 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:
|
||||
loop:
|
||||
# Attempt to read a single byte from STDIN
|
||||
mov $1, %rdx # set the size of chars we want
|
||||
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
|
||||
|
||||
# If we didn't read any bytes jump to Done
|
||||
test %rax, %rax # check what we got
|
||||
jz Done # Got EOF call it done
|
||||
|
||||
# Move our byte into registers for processing
|
||||
movb input, %al # load char
|
||||
movzx %al, %r12 # We have to zero extend it to use it
|
||||
movzx %al, %r13 # We have to zero extend it to use it
|
||||
|
||||
# Break out the nibbles
|
||||
shr $4, %r12 # Purge the bottom 4 bits
|
||||
and $0xF, %r13 # Chop off all but the bottom 4 bits
|
||||
|
||||
# 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
|
||||
|
||||
# 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
|
||||
jmp loop
|
||||
|
||||
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
|
||||
|
||||
write_size = 2
|
||||
input:
|
||||
.byte write_size
|
||||
|
||||
|
Loading…
Reference in New Issue