mirror of
https://github.com/Noratrieb/asm-coreutils.git
synced 2026-01-14 17:55:03 +01:00
better build script!
This commit is contained in:
parent
9cf4469be6
commit
ad531e2811
3 changed files with 95 additions and 25 deletions
63
build.sh
63
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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
26
src/wc.asm
26
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue