better build script!

This commit is contained in:
nora 2022-01-02 15:10:35 +01:00
parent 9cf4469be6
commit ad531e2811
3 changed files with 95 additions and 25 deletions

View file

@ -1,8 +1,14 @@
QUIET="false"
CLEAN="false"
assemble () { assemble () {
FILE="$1" FILE="$1"
PROGRAM_NAME="$2" PROGRAM_NAME="$2"
echo "Assembling $PROGRAM_NAME" if [ "$QUIET" = "false" ]; then
echo "Assembling $PROGRAM_NAME"
fi
nasm -g -F dwarf -f elf64 "$FILE" -o "./target/$PROGRAM_NAME.o" || return nasm -g -F dwarf -f elf64 "$FILE" -o "./target/$PROGRAM_NAME.o" || return
} }
@ -23,27 +29,58 @@ build () {
assemble "$FILE" "$PROGRAM_NAME" assemble "$FILE" "$PROGRAM_NAME"
echo "Linking $PROGRAM_NAME" if [ "$QUIET" = "false" ]; then
echo "Linking $PROGRAM_NAME"
fi
ld.lld "./target/$PROGRAM_NAME.o" "./target/common.o" -o "./target/$PROGRAM_NAME" ld.lld "./target/$PROGRAM_NAME.o" "./target/common.o" -o "./target/$PROGRAM_NAME"
} }
clean () {
if [ "$1" = "--clean" ]; then if [ -d ./target ]; then
rm -r target rm -r target
exit fi
}
for ARG in "$@" ; do
case $ARG in
"--clean")
CLEAN="true"
;;
"--quiet" | "-q")
QUIET="true"
;;
"--help")
cat << EOF
self-made shitty build script
Run to build all programs into the ./target directory
Options:
--quiet, -q | Don't output information
--clean | Clean all build artifacts
EOF
exit
;;
esac
done
if [ "$CLEAN" = "true" ]; then
clean
exit
fi fi
if [ ! -d ./target ]; then if [ ! -d ./target ]; then
mkdir ./target mkdir ./target
fi fi
assemble "src/common.asm" "common" assemble "src/common.asm" "common"
if [ "$#" -eq 0 ]; then for FILE in ./src/*.asm ; do
for FILE in ./src/*.asm ; do build "$FILE"
build "$FILE" done
done
else
build "src/$1.asm"
fi

View file

@ -1,4 +1,10 @@
global itoa global itoa
global println_num
section .data
print_num_buf: times 64 db 0
section .text
; itoa - converts an unsigned integer into its ascii representation ; itoa - converts an unsigned integer into its ascii representation
; inputs: ; inputs:
@ -7,13 +13,9 @@
; outputs: ; outputs:
; rax - 0: success, 1: error ; rax - 0: success, 1: error
; rbx - the length ; rbx - the length
MAX_SIZE EQU 1000000000 MAX_SIZE EQU 1000000000
START_DIVISOR EQU MAX_SIZE / 10 START_DIVISOR EQU MAX_SIZE / 10
ASCII_NUM EQU 48 ASCII_NUM EQU 48
section .text
itoa: itoa:
; r12: whether we are in the leading zeroes (bool) ; r12: whether we are in the leading zeroes (bool)
; r11: buffer start ; r11: buffer start
@ -86,4 +88,25 @@ return_success:
number_too_big: number_too_big:
mov rax, 1 ; return error code 1 mov rax, 1 ; return error code 1
mov rbx, 0 ; we've written nothing mov rbx, 0 ; we've written nothing
ret
ASCII_NEWLINE EQU 10
; println_num
; prints a number to stdout
; inputs:
; rax - the number
println_num:
mov rbx, print_num_buf
call itoa
; add a trailing newline
mov byte [print_num_buf + rbx], ASCII_NEWLINE
inc rbx
mov rdx, rbx ; len
mov rax, 1 ; write
mov rdi, 1 ; stdout
mov rsi, print_num_buf
syscall
ret ret

View file

@ -1,21 +1,31 @@
extern itoa extern itoa
extern println_num
global _start global _start
IO_BUF_SIZE EQU 1024
section .data section .data
itoa_buf: times 100 db 0 io_buf: times IO_BUF_SIZE db 0
section .text section .text
_start: _start:
mov rax, 45354 ; r13 is the character counter
mov rbx, itoa_buf xor r13, r13
call itoa
mov rdx, rbx process:
mov rax, 1 mov rax, 0
mov rdi, 1 mov rdi, 0 ; stdin
mov rsi, itoa_buf mov rsi, io_buf
mov rdx, IO_BUF_SIZE
syscall syscall
add r13, rax
count_and_print:
mov rax, r13
call println_num
mov rax, 60 mov rax, 60
xor rdi, rdi xor rdi, rdi
syscall syscall