9 if [ $index -lt 0 ]; then
10 echo "Index negative, give up!"
14 if ! [ ${da[$index]+a} ]; then
25 local relative_base=$4
27 loc=$(get_param_loc dap $pos $mode $relative_base)
36 local relative_base=$4
46 move_by=$(get_value dapl $pos)
47 echo $((relative_base+$move_by))
50 echo "Unknown parameter mode: $mode"
62 while [ $pos -le ${#data[@]} ]; do
63 # first, printf the value to 5 digits
64 instruction=$(printf "%05d" ${data[$pos]})
65 for (( a=0; a<3; a++ )); do
66 immediate[$((3-a))]=${instruction:$a:1}
68 instruction=${instruction:3}
70 echo -n $'\r'" "$'\r'$instruction >&2
73 # 01 - add, 02 multiply
75 if [ $instruction == "02" ]; then
78 val1=$(get_param data $((pos+1)) ${immediate[1]} $relative_base)
79 val2=$(get_param data $((pos+2)) ${immediate[2]} $relative_base)
80 res_loc=$(get_param_loc data $((pos+3)) ${immediate[3]} $relative_base)
81 data[$res_loc]=$(($val1 $symbol $val2))
85 echo -n $'\r'" "$'\r' >&2
91 echo "$instruction: '$input'" >&2
93 res_loc=$(get_param_loc data $((pos+1)) ${immediate[1]} $relative_base)
99 val=$(get_param data $((pos+1)) ${immediate[1]} $relative_base)
100 echo -n $'\r'" "$'\r' >&2
105 # 05 - jump-if-true, 06 - jump-if-false
106 val=$(get_param data $((pos+1)) ${immediate[1]} $relative_base)
107 jumpto=$(get_param data $((pos+2)) ${immediate[2]} $relative_base)
109 if ( [ $val -ne 0 ] && [ "$instruction" == "05" ] ) ||
110 ( [ $val -eq 0 ] && [ "$instruction" == "06" ] ); then
116 # 07 - is less than, 08 - is equal
117 val1=$(get_param data $((pos+1)) ${immediate[1]} $relative_base)
118 val2=$(get_param data $((pos+2)) ${immediate[2]} $relative_base)
120 echo "$instruction: '$val1' '$val2'" >&2
122 res_pos=$(get_param_loc data $((pos+3)) ${immediate[3]} $relative_base)
124 if [ "$instruction" == "07" ]; then
125 if [ $val1 -lt $val2 ]; then
129 if [ $val1 -eq $val2 ]; then
135 # adjust relative base
137 val=$(get_param data $((pos+1)) ${immediate[1]} $relative_base)
138 relative_base=$((relative_base+$val))
144 echo "Invalid opcode: $instruction at position $pos"
148 pos=$(($pos+$param_count+1))
150 echo -n $'\r'" "$'\r' >&2