Skip to content

fixes #859 #1034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added static/img_javascript/ex5-1-solution-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img_javascript/ex5-1-solution-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions xml/chapter5/section1/section1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,48 @@ function factorial(n) {
}
</JAVASCRIPT>
</SNIPPET>
<SOLUTION>
(Solution by GitHub user escolmebartlebooth)
In <JAVASCRIPTINLINE>factorial(n)</JAVASCRIPTINLINE>, the function
<JAVASCRIPTINLINE>iter(product, counter)</JAVASCRIPTINLINE> is seeded by the
arguments <JAVASCRIPTINLINE>(1, 1)</JAVASCRIPTINLINE> and runs until
<JAVASCRIPTINLINE>counter > n</JAVASCRIPTINLINE>, at which point the product is
returned. Unlike <JVASCRIPTINLINE>gcd(a, b)</JVASCRIPTINLINE>, the register
machine has no need of a temporary register assuming that the correct order of
register assignment is followed.

For the data path, we see three registers:
<JAVASCRIPTINLINE>product</JAVASCRIPTINLINE>,
<JAVASCRIPTINLINE>counter</JAVASCRIPTINLINE>, and
<JAVASCRIPTINLINE>n</JAVASCRIPTINLINE>. There are two operations:
<JAVASCRIPTINLINE>multiply</JAVASCRIPTINLINE> and
<JAVASCRIPTINLINE>plus</JAVASCRIPTINLINE> with two assignments.
There is one test: <JAVASCRIPTINLINE>greater than</JAVASCRIPTINLINE>.

<FIGURE src="img_javascript/ex5-1-solution-1.png">
<LABEL NAME= "ex:5-1"/>
</FIGURE>

For the controller, we start by filling the three registers with the desired
factorial <JAVASCRIPTINLINE>n</JAVASCRIPTINLINE> and the value
<JAVASCRIPTINLINE>1</JAVASCRIPTINLINE> (seeding
<JAVASCRIPTINLINE>product</JAVASCRIPTINLINE> and
<JAVASCRIPTINLINE>counter</JAVASCRIPTINLINE>). The function
<JAVASCRIPTINLINE>iter</JAVASCRIPTINLINE> runs and tests whether
<JAVASCRIPTINLINE>counter > n</JAVASCRIPTINLINE>. If
<JAVASCRIPTINLINE>counter &lt;= n</JAVASCRIPTINLINE>, then
<JAVASCRIPTINLINE>product</JAVASCRIPTINLINE> is updated to
<JAVASCRIPTINLINE>product * counter</JAVASCRIPTINLINE>, followed by counter being
updated to
<JAVASCRIPTINLINE>counter + 1</JAVASCRIPTINLINE> and the process is repeated until
<JAVASCRIPTINLINE>counter > n</JAVASCRIPTINLINE>. Once counter is > n, the product
is
<JAVASCRIPTINLINE>factorial(n)</JAVASCRIPTINLINE> and can be returned.

<FIGURE src="img_javascript/ex5-1-solution-2.png">
<LABEL NAME= "ex:5-1-2"/>
</FIGURE>
</SOLUTION>
</EXERCISE>

<INDEX>data paths for register machine<CLOSE/></INDEX>
Expand Down
Loading