From ad531e2811c50910ede0162a6802ce285541023e Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 2 Jan 2022 15:10:35 +0100 Subject: [PATCH] better build script! --- build.sh | 63 +++++++++++++++++++++++++++++++++++++++----------- src/common.asm | 31 +++++++++++++++++++++---- src/wc.asm | 26 ++++++++++++++------- 3 files changed, 95 insertions(+), 25 deletions(-) diff --git a/build.sh b/build.sh index 48af5cf..0f8b971 100644 --- a/build.sh +++ b/build.sh @@ -1,8 +1,14 @@ +QUIET="false" +CLEAN="false" + assemble () { FILE="$1" 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 } @@ -23,27 +29,58 @@ build () { 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" } - -if [ "$1" = "--clean" ]; then +clean () { + if [ -d ./target ]; then 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 if [ ! -d ./target ]; then mkdir ./target fi - assemble "src/common.asm" "common" -if [ "$#" -eq 0 ]; then - for FILE in ./src/*.asm ; do - build "$FILE" - done -else - build "src/$1.asm" -fi +for FILE in ./src/*.asm ; do + build "$FILE" +done diff --git a/src/common.asm b/src/common.asm index f913ff9..7b3804c 100644 --- a/src/common.asm +++ b/src/common.asm @@ -1,4 +1,10 @@ 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 ; inputs: @@ -7,13 +13,9 @@ ; outputs: ; rax - 0: success, 1: error ; rbx - the length - MAX_SIZE EQU 1000000000 START_DIVISOR EQU MAX_SIZE / 10 ASCII_NUM EQU 48 - - section .text - itoa: ; r12: whether we are in the leading zeroes (bool) ; r11: buffer start @@ -86,4 +88,25 @@ return_success: number_too_big: mov rax, 1 ; return error code 1 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 \ No newline at end of file diff --git a/src/wc.asm b/src/wc.asm index bc3e77e..2222274 100644 --- a/src/wc.asm +++ b/src/wc.asm @@ -1,21 +1,31 @@ extern itoa + extern println_num + global _start +IO_BUF_SIZE EQU 1024 + section .data -itoa_buf: times 100 db 0 +io_buf: times IO_BUF_SIZE db 0 section .text _start: - mov rax, 45354 - mov rbx, itoa_buf - call itoa + ; r13 is the character counter + xor r13, r13 - mov rdx, rbx - mov rax, 1 - mov rdi, 1 - mov rsi, itoa_buf +process: + mov rax, 0 + mov rdi, 0 ; stdin + mov rsi, io_buf + mov rdx, IO_BUF_SIZE syscall + add r13, rax + +count_and_print: + mov rax, r13 + call println_num + mov rax, 60 xor rdi, rdi syscall