Skip to content

Commit

Permalink
fixes #859 (#1034)
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-henz authored Jul 9, 2024
1 parent 6ef2d6e commit c8d4929
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
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

0 comments on commit c8d4929

Please sign in to comment.