Exercise exam question 5

0 views
Skip to first unread message

daniel.arenhage

unread,
Mar 5, 2010, 7:59:19 AM3/5/10
to proglang-course-2010
Exercise exam question 5.
compile (G,L) (while (exp) stm)
write the compilation scheme..

Hello, i was wondering if someone could help me understand this code
by adding comments explaining each step what happens ?

//answer from lecture

Test := get Label L
End := get Label (L+1)
compile (G,L+2)

emit Test
env1 := compile (G, L+2) exp
emit ifeq END
env2 := compile env1 stm
emit goto Test
emit END
return env2

Per Strömdahl

unread,
Mar 6, 2010, 9:14:32 AM3/6/10
to proglang-course-2010
A while loop

statements A
while(exp)
{
statements B
}
statements C


is equivalent to pseudo-assembler:

execute statements A
test: execute exp
if exp is false, goto end
execute statements B
goto test
end: execute statements C

perhaps that makes it clearer?

Reply all
Reply to author
Forward
0 new messages