PLC Laboratory 10
Jump to navigation
Jump to search
Contents
Virtual Machine Interpreting Arithmetic Expressions
In our previous laboratory ( Laboratory 9), we have generated a stack-based code defining the computation for arithmetic expressions.
The target stack-based code consist of following instructions:
ADD, SUB, MUL, DIV, MOD
- it takes two values from the stack, computes the appropriate values, and stores the result on stack. If necessary, integers are automatically casted into floating-point numbers.MOD
works only for integers.PUSH (I|F) n
- it stores the valuen
on stack. It will be eitherint
orfloat
.LOAD a
- it loads the value of variablea
to the stack.SAVE (I|F) a
- it takes the value from the stack and stores in into variablea
of given type.PRINT (I|F)
- it takes a value from stack and based on its type, prints it on output.
Input specification
Input is a correct program composed from instructions described above.
Output specification
Interpret the instructions from the input and write the result on the standard output.
Example
- Input
PUSH I 15
SAVE b
LOAD b
SAVE a
LOAD a
PRINT I
LOAD a
LOAD b
ADD
PRINT I
LOAD a
LOAD b
MOD
PRINT I
LOAD a
LOAD b
ADD
SAVE c
LOAD c
PRINT F
LOAD c
LOAD a
ADD
PRINT F
LOAD a
SAVE c
LOAD c
PRINT F
LOAD c
PUSH F 1.1
ADD
PRINT F
PUSH I 10
LOAD a
MOD
PRINT I
- Output
15
30
0
30
45
15
16.1
10
Solution
- You can download the solution: PLC_Lab10_solution.zip