44-525 Theory and Implementation of Programming Languages Intro Exercise 05

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

44-525 Theory and Implementation of Programming Languages

Intro Exercise 05
Consider the following code fragment:
for( i = 1; i <= 10; i++ )
{
x = x + 2 * y;
}
A compiler might generate the intermediate code shown below. Note that
each instruction consists of a single arithmetic operation, a conditional
branch, or a jump.
test:

i = 1
if i > 10 jump to endfor
temp = 2 * y
x = x + temp
i = i + 1
jump test

endfor:
(a)
What is the total number of instructions that is executed when this
code is run?
(b)

Show how this code can be optimized.

You might also like