9. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 9
Memory
program
A Simple Virtual Machine
CPU
ALU
Register
Code
sto 0, s
sto 0, d
sto 0, c
sto 0, d
lod s, 0
lit “s”, 0
opr 14, 0
jmc #a1, false
lod b, 0
Symbol
Table
10. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 10
A Simple Virtual Machine
CPU
ALU
Register
PC
Memory
program
Code
sto 0, s
sto 0, d
sto 0, c
sto 0, d
lod s, 0
lit “s”, 0
opr 14, 0
jmc #a1, false
lod b, 0
Symbol
Table
11. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 11
Instructions
instruction first second
parameter parameter
12. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 12
Instructions
• LIT <value>, <register_id>
Put a constant value in a CPU register
Examples:
LIT 5, 0
LIT 5.5, 0
LIT 'a', 0
LIT ”hello”, 0
LIT true, 0
13. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 13
Instructions
• LOD <variable>, <register_id>
Search for <variable> in the symbol table
Read its value
Put the value of <variable> in the CPU register
Examples:
LOD a, 0
LOD hello, 0
LOD cse340, 0
14. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 14
Instructions
• STO <variable>, <register_id>
Read a value from the CPU register
Search for <variable> in the symbol table
Store the value into the variable
Examples:
STO a, 0
STO hello, 0
STO cse340, 0
15. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 15
Example
Source Code:
{
int a; int b;
a = 10;
b = a;
Symbol Table:
Type Name Scope Value
int a global 0
int b global 0
Intermediate (Object) Code:
LIT 10,0
STO a,0
LOD a,0
STO b,0
16. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 16
Instructions
• OPR <operation>, <register_id>
Read one or two values from the CPU register
Do the operation <operation>
Put the result into the CPU register
Examples:
OPR 1, 0 ; return
OPR 2, 0 ; add
OPR 3, 0 ; subtract
17. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 17
Operations
Number Action
0 Exit program
1 Return
2 ADD: POP A, POP B, PUSH B+A
3 SUBTRACT: POP A, POP B, PUSH B-A
4 MULTIPLY: POP A, POP B, PUSH B*A
5 DIVIDE: POP A, POP B, PUSH B/A
6 MOD: POP A, POP B, PUSH (B mod A)
7 POW: POP A, POP B, PUSH (A to the power B)
8 OR: POP A, POP B, PUSH (B or A)
9 AND: POP A, POP B, PUSH (B and A)
10 NOT: POP A, PUSH (not A)
11 TEST GREATER THAN: POP A, POP B, PUSH (B>A)
12 TEST LESS THAN: POP A, POP B, PUSH (B<A)
13 TEST GREATER THAN OR EQUAL: POP A, POP B, PUSH (B>=A)
18. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 18
Operations
Number Action
14 TEST LESS THAN OR EQUAL: POP A, POP B, PUSH (B<=A)
15 TEST EQUAL: POP A, POP B, PUSH (B=A)
16 TEST NOT EQUAL: POP A, POP B, PUSH (B<>A)
17
18 clear screen
19 read a value from the standard input
20 print a value to the standard output
21 print a value to the standard output and a newline character
19. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 19
Example
{
int a;
int b;
a = 10;
b = a;
a = a * 10;
b = 2 + 3 + 4;
}
Type Name Scope Value
int a global 0
int b global 0
LIT 10, 0
STO a, 0
LOD a, 0
STO b, 0
LOD a, 0
LIT 10, 0
OPR 4, 0 ; multiply
STO a, 0
LIT 2, 0
LIT 3, 0
OPR 2, 0 ; add
LIT 4, 0
OPR 2, 0 ; add
STO b, 0
OPR 1,0
OPR 0,0
21. CSE340 - Principles of Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Summer 2015
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.