Difference between revisions of "PLC Laboratory 10"

From Marek Běhálek Wiki
Jump to navigation Jump to search
(Created page with "== Virtual Machine Interpreting Arithmetic Expressions == In our previous laboratoy ( Laboratory 9), we have generated a stack-based code defining the c...")
 
Line 1: Line 1:
 
== Virtual Machine Interpreting Arithmetic Expressions ==
 
== Virtual Machine Interpreting Arithmetic Expressions ==
  
In our previous laboratoy ([[PLC_Laboratory_9 | Laboratory 9]]), we have generated a stack-based code defining the computation for arithmetic expressions.  
+
In our previous laboratory  ([[PLC_Laboratory_9 | Laboratory 9]]), we have generated a stack-based code defining the computation for arithmetic expressions.  
  
 
The target stack-based code consist of following instructions:
 
The target stack-based code consist of following instructions:
Line 16: Line 16:
 
== Output specification ==
 
== Output specification ==
  
Interprete the instructions from the input and write the result on the standard output.
+
Interpret  the instructions from the input and write the result on the standard output.
  
 
== Example ==  
 
== Example ==  

Revision as of 06:22, 17 April 2024

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 value n on stack. It will be either int or float.
  • LOAD a - it loads the value of variable a to the stack.
  • SAVE a - it takes the value from the stack and stores in into variable a.
  • 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

Solution