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}
26 # 01 - add, 02 multiply
28 if [ $instruction == "02" ]; then
31 val1=${data[$((pos+1))]}
32 val2=${data[$((pos+2))]}
33 if [ ${immediate[1]} -eq 0 ]; then
36 if [ ${immediate[2]} -eq 0 ]; then
39 data[$res_loc]=$(($val1 $symbol $val2))
43 read -p "input: " input
44 res_loc=${data[$((pos+1))]}
50 if [ ${immediate[1]} -eq 1 ]; then
51 echo ${data[$((pos+1))]}
53 res_loc=${data[$((pos+1))]}
54 echo ${data[$res_loc]}
59 # 05 - jump-if-true, 06 - jump-if-false
60 val=${data[$((pos+1))]}
61 jumpto=${data[$((pos+2))]}
63 if [ ${immediate[1]} -eq 0 ]; then
66 if [ ${immediate[2]} -eq 0 ]; then
67 jumpto=${data[$jumpto]}
69 if ( [ $val -ne 0 ] && [ "$instruction" == "05" ] ) ||
70 ( [ $val -eq 0 ] && [ "$instruction" == "06" ] ); then
76 # 07 - is less than, 08 - is equal
77 val1=${data[$((pos+1))]}
78 val2=${data[$((pos+2))]}
79 res_pos=${data[$((pos+3))]}
80 if [ ${immediate[1]} -eq 0 ]; then
83 if [ ${immediate[2]} -eq 0 ]; then
87 if [ "$instruction" == "07" ]; then
88 if [ $val1 -lt $val2 ]; then
92 if [ $val1 -eq $val2 ]; then
101 echo "Invalid opcode: $instruction at position $pos"
104 pos=$(($pos+$param_count+1))