9 if [ $index -lt 0 ]; then
10 echo "Index negative, give up!"
14 if ! [ ${da[$index]+a} ]; then
18 declare -g __comp_value="${da[$index]}"
25 local relative_base=$4
27 get_param_loc dap $pos $mode $relative_base
37 local relative_base=$4
42 declare -g __comp_param_loc=$__comp_value
45 declare -g __comp_param_loc=$pos
50 declare -g __comp_param_loc=$((relative_base+$move_by))
53 echo "Unknown parameter mode: $mode"
65 while [ $pos -le ${#data[@]} ]; do
66 # first, printf the value to 5 digits
67 instruction=$(printf "%05d" ${data[$pos]})
68 for (( a=0; a<3; a++ )); do
69 immediate[$((3-a))]=${instruction:$a:1}
71 instruction=${instruction:3}
73 echo -n $'\r'" "$'\r'$instruction >&2
76 # 01 - add, 02 multiply
78 if [ $instruction == "02" ]; then
81 get_param data $((pos+1)) ${immediate[1]} $relative_base
83 get_param data $((pos+2)) ${immediate[2]} $relative_base
85 get_param_loc data $((pos+3)) ${immediate[3]} $relative_base
86 res_loc=$__comp_param_loc
87 data[$res_loc]=$(($val1 $symbol $val2))
91 echo -n $'\r'" "$'\r' >&2
97 echo "$instruction: '$input'" >&2
99 get_param_loc data $((pos+1)) ${immediate[1]} $relative_base
100 res_loc=$__comp_param_loc
101 data[$res_loc]=$input
106 get_param data $((pos+1)) ${immediate[1]} $relative_base
108 echo -n $'\r'" "$'\r' >&2
113 # 05 - jump-if-true, 06 - jump-if-false
114 get_param data $((pos+1)) ${immediate[1]} $relative_base
116 get_param data $((pos+2)) ${immediate[2]} $relative_base
119 if ( [ $val -ne 0 ] && [ "$instruction" == "05" ] ) ||
120 ( [ $val -eq 0 ] && [ "$instruction" == "06" ] ); then
126 # 07 - is less than, 08 - is equal
127 get_param data $((pos+1)) ${immediate[1]} $relative_base
129 get_param data $((pos+2)) ${immediate[2]} $relative_base
130 val2=$__comp_value >&90
132 echo "$instruction: '$val1' '$val2'" >&2
134 get_param_loc data $((pos+3)) ${immediate[3]} $relative_base
135 res_pos=$__comp_param_loc
137 if [ "$instruction" == "07" ]; then
138 if [ $val1 -lt $val2 ]; then
142 if [ $val1 -eq $val2 ]; then
148 # adjust relative base
150 get_param data $((pos+1)) ${immediate[1]} $relative_base
152 relative_base=$((relative_base+$val))
158 echo "Invalid opcode: $instruction at position $pos"
162 pos=$(($pos+$param_count+1))
164 echo -n $'\r'" "$'\r' >&2