-
Notifications
You must be signed in to change notification settings - Fork 51
/
assembler.bash
76 lines (66 loc) · 1.88 KB
/
assembler.bash
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
# this is the basic structure for an S/370 assembler in bash
# (c) 2024 by moshix
# can be easily extended
# works as fast as a python program
# v 0.1 humble beginnings
# v 0.2 first few instructions
# v 0.3 CSECT and DSECT
# Instruction set (extendable)
declare -A INSTRUCTIONS=(
["L"]="58" # Load
["ST"]="50" # Store
["A"]="5A" # Add
["S"]="5B" # Subtract
["M"]="5C" # Multiply
["D"]="5D" # Divide
["B"]="47" # Branch
["BZ"]="47" # Branch on Zero
["BN"]="47" # Branch on Negative
["MVI"]="92" # Move Immediate
# Add more instructions here as needed
)
# Function to assemble a single line of S/370 assembly code
assemble_line() {
local line="$1"
local instruction=$(echo "$line" | awk '{print $1}')
local operands=$(echo "$line" | awk '{print $2, $3}')
# Handle special directives
case "$instruction" in
"USING")
echo "Processing USING directive: $operands"
;;
"CSECT")
echo "Processing CSECT directive: $operands"
;;
"DSECT")
echo "Processing DSECT directive: $operands"
;;
*)
# Check if the instruction exists
if [[ -n "${INSTRUCTIONS[$instruction]}" ]]; then
local opcode="${INSTRUCTIONS[$instruction]}"
echo "$opcode $operands"
else
echo "Error: Unknown instruction '$instruction'" >&2
fi
;;
esac
}
# Main function to assemble a file
assemble_file() {
local file="$1"
if [[ ! -f "$file" ]]; then
echo "Error: File '$file' not found." >&2
return 1
fi
while IFS= read -r line; do
assemble_line "$line"
done < "$file"
}
# Check for input file argument
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <assembly_file>" >&2
exit 1
fi
assemble_file "$1"