CSE142                    
29. Mar. 2001

Quiz #1 (Version 2)

March 29, 2001

Sample Solution

QOTD 1.

When Compilers Were People

Imagine you have a sister in grade school. She's just learning to count. She knows how to count numbers, say, how to add one. Just imagine her as a "human compiler", and explain how she could express the problem of adding 6 and 9 to a problem requiring adding only one at a time.

6 + 9  -> counting problem

 Solution:

A compiler translates a program in a high level language to a machine language program that can be executed. In some sense, a statement in machine language, which we generally call a machine language instruction, is a kind of atomic operation. Generally each statement of the high level language is mapped to a set of these atomic machine language instructions by the compiler.

 So let's make an analogy between the compiler and your sister.

 

Compiler

Sister

Input

High level language

Complex  computation like 6+9

Process

Express the high level language statement as a set of machine language instructions

Express the 6+9 as a set of "add one" operations

Output

A set of simpler machine language instructions

A set of simpler "add one" operations to get the result of 6+9

Express 6+9 as a set of ˇ°add oneˇ± operations:

  Perform ˇ°add oneˇ± 6 times and starting from that result, continue to perform 9 more ˇ°add oneˇ± operations, the final number you have counted is the result of 6+9.

Express x = y + 3 as a set of machine language instructions: (Don't worry about the details, this is just here to give you a sense of the process, you will learn more from later lectures)

1.      A ˇ°Loadˇ± instruction to load the value of y into a special memory place, which is called a ˇ°registerˇ±.

2.      An ˇ°Addˇ± instruction to add the value in the register with 3, and store the result to another register.

3.      A ˇ°Storeˇ± instruction to store the result in the register to x

Observation: The number of  expressed machine language instruction is generally larger than the number of corresponding high level language statements. Just like the number of ˇ°add oneˇ± operations (15) is larger than the one ˇ°6+9ˇ± operation (1).

What other observations can you make? Think of themˇ­ If you're not sure whether it is true for a compiler, discuss them on the mailing list or ask your TA or instructor.

ˇˇ

Self-Check Question:

Adapted from Self-Check p. 21

What do you think these five high-level language statements mean? Use simple plain language to explain them.

x = a * b * c;

Solution: x is equal to the product of a, b and c.

x = z / y;

Solution: x is equal to the quotient of z divided by y.

d = c + b - a;

Solution: d is equal to the value of c plus b minus a.

z = z + 8;

Solution: z's value is increased by 8.

Note: The value of z in the right side has the old value, but the value of z in the left side has the new value.

celsius  = kelvin  - 273.15;

Solution:  the Celsius value is attained by subtracting 273.15 from Kelvin value.