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
86 read -p "input: " input
87 res_loc=$(get_param_loc data $((pos+1)) ${immediate[1]} $relative_base)
93 val=$(get_param data $((pos+1)) ${immediate[1]} $relative_base)
94 echo -n $'\r'" "$'\r' >&2
99 # 05 - jump-if-true, 06 - jump-if-false
100 val=$(get_param data $((pos+1)) ${immediate[1]} $relative_base)
101 jumpto=$(get_param data $((pos+2)) ${immediate[2]} $relative_base)
103 if ( [ $val -ne 0 ] && [ "$instruction" == "05" ] ) ||
104 ( [ $val -eq 0 ] && [ "$instruction" == "06" ] ); then
110 # 07 - is less than, 08 - is equal
111 val1=$(get_param data $((pos+1)) ${immediate[1]} $relative_base)
112 val2=$(get_param data $((pos+2)) ${immediate[2]} $relative_base)
113 res_pos=$(get_param_loc data $((pos+3)) ${immediate[3]} $relative_base)
115 if [ "$instruction" == "07" ]; then
116 if [ $val1 -lt $val2 ]; then
120 if [ $val1 -eq $val2 ]; then
126 # adjust relative base
128 val=$(get_param data $((pos+1)) ${immediate[1]} $relative_base)
129 relative_base=$((relative_base+$val))
135 echo "Invalid opcode: $instruction at position $pos"
139 pos=$(($pos+$param_count+1))
141 echo -n $'\r'" "$'\r'