This document contains lecture slides for CSE340 on intermediate code. It discusses instructions like LIT, LOD, STO, OPR, JMP, JMC and how they are used to represent code structures like while loops, if statements, and switch cases. It provides examples of translating source code to intermediate code and discusses using symbol tables and labels. It also assigns homework to translate a switch statement to intermediate code.
18. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 19
Instructions
• JMP <line>, 0
Put the value <line> in the program counter;
thus, the next line to be executed will be <line>
Examples:
JMP 1, 0
JMP 14, 0
JMP 75, 0
19. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 20
Instructions
• JMC <line>, <condition>
Read one value from the CPU register
If the value is equal to <condition>, put the value
<line> in the program counter; thus, the next line to
be executed will be <line>
Examples:
JMC 1, true
JMC 14, false
JMC 75, true
20. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 21
Example
{
int a; int b;
a = 10;
while (a>1){
print (a);
}
return;
}
int a global 0
int b global 0
@
LIT 10, 0
STO a, 0
LOD a, 0
LIT 1, 0
OPR 11, 0 ; >
JMC #e1, false
LOD a, 0
OPR 20, 0 ; print
JMP #e2, 0
OPR 1, 0
OPR 0, 0
21. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 22
Labels
• Compiler creates variables and adds them to the
symbol table to remember positions in the code.
• This is useful for loops and conditions.
22. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 23
Labels | how while works
while (a < b) {
//code
}
label#1
if (a < b) {
//code
} else
goto label#2
}
goto label#1
label #2
23. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 24
Labels | how if works
if (a < b) {
//code1
} else {
// code2
}
if (a < b) {
if (a>b) goto label #1
//code1
} else
goto label#2
label #1
// code2
}
label #2
24. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 25
Example | while
{
int a; int b;
a = 10;
while (a>1){
print (a);
}
return;
}
int a global 0
int b global 0
int #e1 global 10
int #e2 global 3
LIT 10, 0
STO a, 0
LOD a, 0
LIT 1, 0
OPR 11, 0 ; >
JMC #e1, false
LOD a, 0
OPR 20, 0 ;print
JMP #e2, 0
OPR 1, 0
OPR 0, 0
25. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 26
Example | if
{
int a; int b;
a = 10;
if (a>1) {
print (a);
} else {
print (b);
}
return;
}
int a global 0
int b global 0
int #e1 global 10
int #e2 global 12
LIT 10, 0
STO a, 0
LOD a, 0
LIT 1, 0
OPR 11, 0 ; >
JMC #e1, false
LOD a, 0
OPR 20, 0 ;print
JMP #e2, 0
LOD b, 0
OPR 20, 0 ;print
OPR 1, 0
OPR 0, 0
26. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 27
Exercise
{
int a; int b;
a = 10;
while (a>1) {
if (a != 0) {
print (a);
} else {
print (b);
}
a = a -1;
}
}
32. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 33
Homework
Translate this source code to intermediate code:
int a, b;
switch (a) {
case 1: { b = 11; break;}
case 2: { b = 22; break;}
case 3: { b = 33; break;}
default:{ b = 99; break;}
}
34. 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.