Difference between revisions of "PLC Laboratory 10"

From Marek Běhálek Wiki
Jump to navigation Jump to search
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 ==

Revision as of 07:49, 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

Solution