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...")
 
 
(2 intermediate revisions by the same user not shown)
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 7: Line 7:
 
* <code>PUSH (I|F) n</code> - it stores the value <code>n</code> on stack. It will be either <code>int</code> or <code>float</code>.
 
* <code>PUSH (I|F) n</code> - it stores the value <code>n</code> on stack. It will be either <code>int</code> or <code>float</code>.
 
* <code>LOAD a</code> - it loads the value of variable <code>a</code> to the stack.
 
* <code>LOAD a</code> - it loads the value of variable <code>a</code> to the stack.
* <code>SAVE a</code> - it takes the value from the stack and stores in into variable <code>a</code>.
+
* <code>SAVE (I|F) a</code> - it takes the value from the stack and stores in into variable <code>a</code> of given type.
* <code>PRINT (I|F)</code> - it takes a value from stack and '''based on its type''', prints it on output.  
+
* <code>PRINT (I|F)</code> - it takes a value from stack and '''based on its type''', prints it on output.
  
 
== Input specification ==
 
== Input specification ==
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 ==  
Line 61: Line 61:
 
* Output
 
* Output
 
<syntaxhighlight lang="bash" >
 
<syntaxhighlight lang="bash" >
 
+
15
 +
30
 +
0
 +
30
 +
45
 +
15
 +
16.1
 +
10
 
</syntaxhighlight >
 
</syntaxhighlight >
  

Latest revision as of 07:52, 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 (I|F) a - it takes the value from the stack and stores in into variable a 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