Compilers (CS362 2001S)

Class 36: Steps in Compilation, Continued

Back to Steps in Compilation, Revisited. On to General Techniques.

Held Friday, April 27, 2001

Summary

Today we continue our translation of a simple recursive Pascal program.

Notes

Overview

Where We've Been

Procedure Wrappers

Other Activities

Putting it All Together

  ivar Val 0
  svar STRING1 "Please enter a number: "
  svar STRING2 "The factorial of "
  svar STRING3 " is "
  svar STRING4 "."
Label Program_Factorial
  # write('Please enter a number: ')
  push Constant(23)
  push Address(STRING1)
  push Constant(WRITE_A_STRING)
  push Constant(1)
  push Address(_p_stdout)
  call _p_write
  popn Constant(5)
  # readln(Val)
  pushl Constant(READ_A_NEWLINE)
  pushl Address(Val)
  pushl Constant(READ_A_STRING)
  pushl Constant(2)
  call _p_read
  popn Constant(4)
  # write('The factorial of ');
  ...
  # write(val:1)
  push Constant(1)
  push Value(Val)
  push Constant(WRITE_A_NUMBER)
  push Constant(1)
  push Address(_p_stdout)
  call _p_write
  popn Constant(5)
  # write(' is ');
  ...
  # write(factorial(val):1) becomes
  #   T1 := factorial(val)
  #   write(T1:1)
  # T1 := factorial(val)
  push Value(Val)
  call Factorial
  move Register(eax) into T1
  popn Constant(1)
  # write(T1:1)
  ...
  # writeln('.')
  push Constant(WRITE_A_NEWLINE)
  push Constant(1)
  push Address(STRING4)
  push Constant(WRITE_A_STRING)
  push Constant(2)
  push Address(_p_stdout)
  call _p_write
  popn Constant(6)
  # end
  return
Label Factorial
# Initialization
  push Register(ebp)
  move Register(esp) Register(ebp)
  push Constant(0)
# factorial := helper(n,1)
  push Offset(Register(ebp),Constant(8))
  push Constant(1)
  call Factorial_Helper
  move Register(%eax) Offset(register(ebp),Constant(-4))
# Cleanup
  move Offset(register(ebp),Constant(-4)) Register(%eax)
  move Register(ebp) Register(esp)
  pop  Register(esp)
  ret
Label Factorial_Helper
# Initialization
  push Register(ebp)
  move Register(esp) Register(ebp)
  push Constant(0)
# if (n = 0) then
  cmp  Offset(register(ebp),Constant(-8)) Constant(0)
  je   LABEL_1
  jump LABEL_2
label LABEL_1
# helper := acc
  move Offset(register(ebp),Constant(-4)) Offset(register(ebp),Constant(4))
  jump LABEL_3
label LABEL_2
# helper := helper(n-1,n*acc)
# T2 := n-1
  sub  Offset(register(ebp),Constant(-8)) constant(1) T2
# T3 := n*
  mul  Offset(register(ebp),Constant(-8)) Offset(register(ebp),Constant(-4)) T3
# helper(T2,T3)
  push T2
  push T3
  call Factorial_Helper
# helper :=
  move Register(eax) Offset(register(ebp),Constant(-4))
  jump LABEL_3
label LABEL_3
# Cleanup
  move Offset(register(ebp),Constant(-4)) Register(%eax)
  move Register(ebp) Register(esp)
  pop  Register(esp)
  ret

 

History

Monday, 22 January 2001

Friday, 26 April 2001

 

Back to Steps in Compilation, Revisited. On to General Techniques.

Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This page was generated by Siteweaver on Mon Apr 30 10:52:14 2001.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2001S/outline.36.html.
You may validate this page's HTML.
The source was last modified Fri Apr 27 10:47:44 2001.