Back to Pause for Breath. On to Steps in Compilation, Continued.
Held Wednesday, April 25, 2001
Summary
Today we revist the steps involved in compilation by looking at a more complex program in some more detail.
Notes
Overview
Here is a sample Pascal program to compute the factorial of a number entered by the user.
program factorial(input,output); var val: integer; function factorial(n: integer): integer; function helper(n: integer; acc: integer): integer; begin { factorial/helper } if (n = 0) then helper := acc else helper := helper(n-1, acc*n); end; { factorial/helper } begin { factorial } factorial := helper(n, 1); end; { factorial} begin { program } write('Please enter a number: '); readln(val); write('The factorial of '); write(val:1); write(' is '); write(factorial(val):1); writeln('.') end. { program }
val
)? We
use a pseudo-assembler declare-integer
.deci Val
.local Val .comm Val,4,4
.deci STRING1 "Please enter a number: "
STRING1: .string "Please enter a number: "
write
and writeln
calls because they're so similar. For each of those, we need
to
_p_write
.
writeln("Please enter a number: ")
becomes
push Constant(23) # Yes, it has 23 characters push Address(STRING1) # "Please enter a number: " push Constant(16) # And it's a string push Constant(1) # That's the only parameter push Address(_p_stdout) # Also need the place to print to call _p_write # Write the value popmany Constant(5 ) # Pop five values off the stack JumpIfEqual Constant(0),Constant(_p_inoutres),NEWLABEL # Is the result okay? Cal l_p_inoutres NEWLABEL
Monday, 22 January 2001
Wednesday, 25 April 2001
Back to Pause for Breath. On to Steps in Compilation, Continued.
[Current]
[Discussions]
[Glance]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Primary
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Project]
[Quizzes]
[Readings]
[Reference]
Sets
[Blackboard]
[98F]
Links
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:13 2001.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2001S/outline.35.html
.
You may validate
this page's HTML.
The source was last modified Wed Apr 25 10:52:14 2001.