-
Notifications
You must be signed in to change notification settings - Fork 5
/
lc3run
executable file
·56 lines (48 loc) · 1.32 KB
/
lc3run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env bash
# Takes assembly on stdin, assembles it, and runs it
debug=false
run=true
custom_tmp=false
if [ "$1" == "--norun" ]; then
run=false
fi
if [ "$1" == "--usetmp" ]; then
temp_dir="$2"
custom_tmp=true
else
temp_dir="$(mktemp -d)"
fi
set -euo pipefail
# if debug set, print commands before executing them
if [ $debug = 'true' ]; then set -x; fi
if [ $debug = 'true' ]; then echo temp_dir: $temp_dir; fi
asm_path="$temp_dir/a.asm"
obj_path="$temp_dir/a.obj"
script_path="$temp_dir/a.script"
# Pipe input from STDIN to asm file
cat > $temp_dir/a.asm
if [ $debug = 'true' ]; then cat $asm_path; fi
# Assemble input
# `chronic` ignores output unless there is an error, in which
# case it outputs normally.
# Requires debian package 'moreutils'
chronic lc3as $asm_path || exit
if [ $debug = 'true' ]; then ls $temp_dir; fi
# Create script file
echo "file $obj_path
continue" > $script_path
# Run it.
# Remove line that says "Loaded such-and-such"
# Remove part after LC3 halts
if [ $run = 'true' ]; then
lc3sim -norun -s $script_path | sed '1 d; /--- halting the LC-3 ---/,$ d' | head --bytes -2
else
echo lc3sim -norun $obj_path # | sed '1 d'
exit
fi
# If not in debug mode, clean up after ourselves.
if [ $custom_tmp == 'false' ]; then
if [ $debug != 'true' ];
then rm -r $temp_dir;
fi
fi