6 IFS="," read -u 3 -a orig_data
10 data=("${orig_data[@]}")
13 while [ $pos -le ${#data[@]} ]; do
14 loc1=${data[$((pos+1))]}
15 loc2=${data[$((pos+2))]}
16 res_loc=${data[$((pos+3))]}
17 # first, printf the value to 5 digits
18 instruction=$(printf "%05d" ${data[$pos]})
19 for (( a=0; a<3; a++ )); do
20 immediate[$((3-a))]=${instruction:$a:1}
22 instruction=${instruction:3}
27 if [ $instruction == "02" ]; then
30 val1=${data[$((pos+1))]}
31 val2=${data[$((pos+2))]}
32 if [ ${immediate[1]} -eq 0 ]; then
35 if [ ${immediate[2]} -eq 0 ]; then
38 data[$res_loc]=$(($val1 $symbol $val2))
41 read -p "input: " input
42 res_loc=${data[$((pos+1))]}
47 if [ ${immediate[1]} -eq 1 ]; then
48 echo ${data[$((pos+1))]}
50 res_loc=${data[$((pos+1))]}
51 echo ${data[$res_loc]}
56 val=${data[$((pos+1))]}
57 jumpto=${data[$((pos+2))]}
59 if [ ${immediate[1]} -eq 0 ]; then
62 if [ ${immediate[2]} -eq 0 ]; then
63 jumpto=${data[$jumpto]}
65 if ( [ $val -ne 0 ] && [ "$instruction" == "05" ] ) ||
66 ( [ $val -eq 0 ] && [ "$instruction" == "06" ] ); then
72 val1=${data[$((pos+1))]}
73 val2=${data[$((pos+2))]}
74 res_pos=${data[$((pos+3))]}
75 if [ ${immediate[1]} -eq 0 ]; then
78 if [ ${immediate[2]} -eq 0 ]; then
81 if [ $val1 -lt $val2 ]; then
88 val1=${data[$((pos+1))]}
89 val2=${data[$((pos+2))]}
90 res_pos=${data[$((pos+3))]}
91 if [ ${immediate[1]} -eq 0 ]; then
94 if [ ${immediate[2]} -eq 0 ]; then
97 if [ $val1 -eq $val2 ]; then
107 echo "Invalid opcode: $instruction at position $pos"
110 pos=$(($pos+$param_count+1))