10 while [ $pos -le ${#data[@]} ]; do
11 # first, printf the value to 5 digits
12 instruction=$(printf "%05d" ${data[$pos]})
13 for (( a=0; a<3; a++ )); do
14 immediate[$((3-a))]=${instruction:$a:1}
16 instruction=${instruction:3}
20 # 01 - add, 02 multiply
22 if [ $instruction == "02" ]; then
25 val1=${data[$((pos+1))]}
26 val2=${data[$((pos+2))]}
27 res_loc=${data[$((pos+3))]}
28 if [ ${immediate[1]} -eq 0 ]; then
31 if [ ${immediate[2]} -eq 0 ]; then
34 data[$res_loc]=$(($val1 $symbol $val2))
38 read -p "input: " input
39 res_loc=${data[$((pos+1))]}
45 if [ ${immediate[1]} -eq 1 ]; then
46 echo ${data[$((pos+1))]}
48 res_loc=${data[$((pos+1))]}
49 echo ${data[$res_loc]}
54 # 05 - jump-if-true, 06 - jump-if-false
55 val=${data[$((pos+1))]}
56 jumpto=${data[$((pos+2))]}
58 if [ ${immediate[1]} -eq 0 ]; then
61 if [ ${immediate[2]} -eq 0 ]; then
62 jumpto=${data[$jumpto]}
64 if ( [ $val -ne 0 ] && [ "$instruction" == "05" ] ) ||
65 ( [ $val -eq 0 ] && [ "$instruction" == "06" ] ); then
71 # 07 - is less than, 08 - is equal
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
82 if [ "$instruction" == "07" ]; then
83 if [ $val1 -lt $val2 ]; then
87 if [ $val1 -eq $val2 ]; then
96 echo "Invalid opcode: $instruction at position $pos"
99 pos=$(($pos+$param_count+1))