Use read_opcode function

This commit is contained in:
2024-12-17 17:09:19 +08:00
parent e02ee993c6
commit de2fd03c3c
3 changed files with 55 additions and 166 deletions

View File

@ -46,8 +46,8 @@ cdv () { # OPCODE 7
REG_C=$(( REG_A / (2 ** OPERAND) ))
}
# Get combo operand
get_combo () {
# Interpret operand value
if [[ $OPERAND -eq 0 ]]
then
printf "$1"
@ -76,6 +76,7 @@ get_combo () {
fi
}
# Check if the program is a quine
check_quine () {
for (( i=0; i<INPUT_LEN; i++ ))
do
@ -85,3 +86,42 @@ check_quine () {
fi
done
}
# Interpret opcode
read_opcode () {
local OPCODE=$1
local OPERAND=$2
if [[ $OPCODE -eq 0 ]]
then
adv "$OPERAND"
(( POINTER+=2 ))
elif [[ $OPCODE -eq 1 ]]
then
bxl "$OPERAND"
(( POINTER+=2 ))
elif [[ $OPCODE -eq 2 ]]
then
bst "$OPERAND"
(( POINTER+=2 ))
elif [[ $OPCODE -eq 3 ]]
then
# jnz itself moves the pointer
jnz "$OPERAND"
elif [[ $OPCODE -eq 4 ]]
then
bxc "$OPERAND"
(( POINTER+=2 ))
elif [[ $OPCODE -eq 5 ]]
then
out "$OPERAND"
(( POINTER+=2 ))
elif [[ $OPCODE -eq 6 ]]
then
bdv "$OPERAND"
(( POINTER+=2 ))
elif [[ $OPCODE -eq 7 ]]
then
cdv "$OPERAND"
(( POINTER+=2 ))
fi
}