Skip to content
This repository was archived by the owner on Feb 16, 2021. It is now read-only.

Commit 5286184

Browse files
committed
Add existing scripts
1 parent 04f3672 commit 5286184

File tree

19 files changed

+652
-2
lines changed

19 files changed

+652
-2
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
# uwaterloo-scripts
2-
A collection of handy scripts for various UWaterloo CS courses.
1+
UWaterloo Script Archive
2+
<br>
3+
![GitHub downloads](https://img.shields.io/github/downloads/george-lim/uwaterloo-script-archive/total.svg)
4+
[![GitHub release](https://img.shields.io/github/release/george-lim/uwaterloo-script-archive.svg)](https://github.com/george-lim/uwaterloo-script-archive/releases)
5+
[![GitHub issues](https://img.shields.io/github/issues/george-lim/uwaterloo-script-archive.svg)](https://github.com/george-lim/uwaterloo-script-archive/issues)
6+
[![GitHub pull requests](https://img.shields.io/github/issues-pr/george-lim/uwaterloo-script-archive.svg)](https://github.com/george-lim/uwaterloo-script-archive/pulls)
7+
[![license](https://img.shields.io/github/license/george-lim/uwaterloo-script-archive.svg)](https://github.com/george-lim/uwaterloo-script-archive/blob/master/LICENSE)
8+
===============
9+
10+
This project is an archive of powerful automation scripts that I have written over the course of my time at UWaterloo. It is meant to assist future UWaterloo students in various courses where automation scripts can save a lot of time. Only the `Linux Environment Mounting & SSH Script` should be set up on your system. The rest of the scripts are assumed to be executed from the Linux environment.
11+
12+
1. [Linux Environment Mounting & SSH Script](https://github.com/george-lim/uwaterloo-script-archive/tree/master/uw)
13+
2. [CS 246: Rapid Test Suite Creation](https://github.com/george-lim/uwaterloo-script-archive/tree/master/cs246)
14+
3. [CS 241: Hexadecimal (.word) Converter](https://github.com/george-lim/uwaterloo-script-archive/tree/master/cs241)
15+
3. [CS 350: OS/161 All-In-One CLI](https://github.com/george-lim/uwaterloo-script-archive/tree/master/cs350)

cs241/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
hexify - CS 241 Hexadecimal (.word) Converter
2+
===============
3+
4+
This project outputs the hexadecimal representation (using the .word notation) of a file. It is meant to be used in conjunction with `cs241.wordasm` to translate assembly into MIPS code. Use the `-p` flag if the input file should be interpreted as a MIPS program, or the `-s` flag if the input should be interpreted as a string literal.
5+
6+
1. [Usage](#usage)
7+
8+
# Usage
9+
Run `usage: ./hexify [-s | -p] <input file path> > <output file path>`. If you run into a permission error, run `chmod 755 hexify`.
10+
Example usage: `./hexify -p input > output`.

cs241/hexify

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/bin/bash
2+
if [ ${#} -ne 2 ] || ([ "$1" != '-s' ] && [ "$1" != '-p' ]); then
3+
echo "usage: ./hexify [-s | -p] <input file path> > <output file path>"
4+
echo "example: ./hexify -p input > output"
5+
exit
6+
fi
7+
8+
# If you choose to interpret file contents as a string literal.
9+
if [ "$1" == '-s' ]; then
10+
# Convert entire file into hexadecimal, split by every 8 characters to get word.
11+
words=$(echo $(xxd -pu <<< "$(cat $2)") | sed 's/.\{8\}/& /g')
12+
for word in $words; do
13+
# Add trailing zeroes to remaining word.
14+
while [ ${#word} -lt 8 ]; do
15+
word="${word}0"
16+
done
17+
# Write word to wordasm.
18+
echo ".word 0x$word"
19+
done
20+
exit
21+
fi
22+
23+
# Adds leading zeroes to $1 until it has reached a length of $2.
24+
addLeadingZeroes() {
25+
str=$1
26+
while [ ${#str} -lt "$2" ]; do
27+
str="0$str"
28+
done
29+
echo $str
30+
}
31+
32+
# Converts decimal $1 to a binary string of length $2.
33+
dec2bin() {
34+
use2scomplement=false
35+
decimal=$1
36+
# Set 'use2scomplement' flag to true and strip '-' from negative values.
37+
if [[ "$decimal" = '-'* ]]; then
38+
decimal=${decimal#'-'}
39+
use2scomplement=true
40+
fi
41+
binary=$(echo "obase=2;$decimal" | bc)
42+
binary=$(addLeadingZeroes "$binary" "$2")
43+
if [ "$use2scomplement" = true ]; then
44+
# Flip all bits and add one to binary string.
45+
binary=$(tr 01 10 <<< "$binary")
46+
binary=$(echo "obase=2;ibase=2;$binary+1" | bc -l)
47+
fi
48+
echo $binary
49+
}
50+
51+
# Converts binary string $1 to a hexadecimal word of length 8.
52+
bin2hex() {
53+
word=$(echo "obase=16;ibase=2;$1" | bc)
54+
word=$(addLeadingZeroes "$word" 8)
55+
echo ".word 0x$word"
56+
}
57+
58+
while IFS='' read -r line || [[ -n "$line" ]]; do
59+
components=($line)
60+
# Strip characters until every command argument is a decimal, then convert to binary.
61+
for i in {1..3}; do
62+
if [ "${components[i]}" == '' ]; then
63+
break
64+
fi
65+
# Remove ',' after certain arguments.
66+
components[$i]=${components[$i]%$','}
67+
# Strip ')' from lw and sw commands, and split the offset and register values into separate components.
68+
if [[ ${components[i]} = *')' ]]; then
69+
components[$i]=${components[$i]%$')'}
70+
subComponents=(${components[$i]//(/ })
71+
components[$i]=${subComponents[0]}
72+
components[$i+1]=${subComponents[1]}
73+
fi
74+
# Determine register or immediate value, and convert to binary.
75+
if [[ ${components[i]} = '$'* ]]; then
76+
components[$i]=${components[$i]#'$'}
77+
components[$i]=$(dec2bin "${components[$i]}" 5)
78+
else
79+
components[$i]=$(dec2bin "${components[$i]}" 16)
80+
fi
81+
done
82+
# Command interpreter.
83+
cmd=${components[0]}
84+
if [ "$cmd" == '.word' ]; then
85+
echo $(bin2hex "${components[1]}")
86+
elif [ "$cmd" == "add" ]; then
87+
echo $(bin2hex "000000${components[2]}${components[3]}${components[1]}00000100000")
88+
elif [ "$cmd" == "sub" ]; then
89+
echo $(bin2hex "000000${components[2]}${components[3]}${components[1]}00000100010")
90+
elif [ "$cmd" == "mult" ]; then
91+
echo $(bin2hex "000000${components[1]}${components[2]}0000000000011000")
92+
elif [ "$cmd" == "multu" ]; then
93+
echo $(bin2hex "000000${components[1]}${components[2]}0000000000011001")
94+
elif [ "$cmd" == "div" ]; then
95+
echo $(bin2hex "000000${components[1]}${components[2]}0000000000011010")
96+
elif [ "$cmd" == "divu" ]; then
97+
echo $(bin2hex "000000${components[1]}${components[2]}0000000000011011")
98+
elif [ "$cmd" == "mfhi" ]; then
99+
echo $(bin2hex "0000000000000000${components[1]}00000010000")
100+
elif [ "$cmd" == "mflo" ]; then
101+
echo $(bin2hex "0000000000000000${components[1]}00000010010")
102+
elif [ "$cmd" == "lis" ]; then
103+
echo $(bin2hex "0000000000000000${components[1]}00000010100")
104+
elif [ "$cmd" == "lw" ]; then
105+
echo $(bin2hex "100011${components[3]}${components[1]}${components[2]}")
106+
elif [ "$cmd" == "sw" ]; then
107+
echo $(bin2hex "101011${components[3]}${components[1]}${components[2]}")
108+
elif [ "$cmd" == "slt" ]; then
109+
echo $(bin2hex "000000${components[2]}${components[3]}${components[1]}00000101010")
110+
elif [ "$cmd" == "sltu" ]; then
111+
echo $(bin2hex "000000${components[2]}${components[3]}${components[1]}00000101011")
112+
elif [ "$cmd" == "beq" ]; then
113+
echo $(bin2hex "000100${components[1]}${components[2]}${components[3]}")
114+
elif [ "$cmd" == "bne" ]; then
115+
echo $(bin2hex "000101${components[1]}${components[2]}${components[3]}")
116+
elif [ "$cmd" == "jr" ]; then
117+
echo $(bin2hex "000000${components[1]}000000000000000001000")
118+
elif [ "$cmd" == "jalr" ]; then
119+
echo $(bin2hex "000000${components[1]}000000000000000001001")
120+
fi
121+
done < "$2"

cs241/input

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
beq $1, $2, 2018
2+
lw $1, -100($3)
3+
.word 4
4+
add $1, $2, $3
5+
jr $31

cs241/output

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.word 0x102207E2
2+
.word 0x8C61FF9C
3+
.word 0x00000004
4+
.word 0x00430820
5+
.word 0x03E00008

cs246/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 George Lim
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

cs246/README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
buildSuite - CS 246 Rapid Test Suite Creation
2+
===============
3+
4+
This project allows students to create entire test suites from a single file. The script takes a buildSuite input file and creates the necessary `suiteq#.txt`, `<test>.in`, and `<test>.args` files.
5+
6+
1. [Writing Test Suites](#writing-test-suites)
7+
1. [Usage](#usage)
8+
9+
# Writing Test Suites
10+
Test suites are written in a specific buildSuite input format. While the syntax is intuitive, there are rules that must be followed for `buildSuite` to execute correctly.
11+
12+
To understand the syntax, consider the following `helloworld.txt` test suite input:
13+
14+
```
15+
This is the input for the first test.
16+
This is the second line for the input to the first test.
17+
This is the third, etc.
18+
This input will be saved in a .in file.
19+
When you want to start a new test, you simply...
20+
21+
22+
do this. Any number of blank lines will do.
23+
We are now writing the input for the second test.
24+
This test is called "test2", and the previous test is called "test1".
25+
By default, test aliases are auto incremented with "test{#}"
26+
To add a custom test alias...
27+
28+
29+
"youDoThis"
30+
This MUST be declared on the first line of a new test.
31+
It is optional.
32+
Custom test aliases MUST not contain whitespace.
33+
Filename illegal characters are also not permitted.
34+
What about adding arguments to tests?
35+
36+
37+
/a -l -w
38+
Adds the arguments "-l -w" to your test .args file.
39+
It is also optional.
40+
Arguments must be declared before writing input for your test.
41+
42+
43+
Finally, to make comments, only single line
44+
comments are currently supported.
45+
// This comment will not be read.
46+
also, // this comment will not be read.
47+
Multi-line comments are not currently supported.
48+
49+
```
50+
51+
Please note that you **MUST** end your test file with a new line. Putting **EVERYTHING** all together, we can effectively produce test suites like this.
52+
53+
```
54+
// a9q1.input
55+
56+
"printZero"
57+
read a 0
58+
print a
59+
60+
"incrementZero"
61+
/a -l
62+
read a 0
63+
read b 1
64+
add c a b
65+
print c
66+
67+
// Only 1 argument, no test alias.
68+
/a -d
69+
read a 1
70+
read a 0
71+
print a
72+
73+
```
74+
75+
After running `buildSuite`, we would expect to produce a folder called `a9q1-tests` with the following files:
76+
77+
**suiteq1.txt**
78+
```
79+
printZero
80+
incrementZero
81+
test3
82+
83+
```
84+
85+
**printZero.in**
86+
```
87+
read a 0
88+
print a
89+
90+
```
91+
92+
**incrementZero.in**
93+
```
94+
read a 0
95+
read b 1
96+
add c a b
97+
print c
98+
99+
```
100+
101+
**incrementZero.args**
102+
```
103+
-l
104+
105+
```
106+
107+
**test3.in**
108+
```
109+
read a 1
110+
read a 0
111+
print a
112+
113+
```
114+
115+
**test3.args**
116+
```
117+
-d
118+
119+
```
120+
121+
# Usage
122+
After writing your test suite, place the input file inside the directory containing the script.
123+
124+
Run `./buildSuite <test file path> <assignment #> <question #>`. If you run into a permission error, run `chmod 755 buildSuite`.
125+
Example usage: `./buildSuite a3q1.txt 3 1`.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-l

cs246/a3q1-tests/incrementZero.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
read a 0
2+
read b 1
3+
add c a b
4+
print c

cs246/a3q1-tests/printZero.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
read a 0
2+
print a

0 commit comments

Comments
 (0)