Skip to content
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

improve exercise 5.12 #563

Merged
merged 1 commit into from
Sep 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions resources/lang/en/exercises/5_12.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
"(reg n) (reg val))).",
'6' => "Extend the message-passing interface to the machine to provide access to this new information. To " .
"test your analyzer, define the Fibonacci machine from figure 5.12 and examine the lists you constructed.",
'7' => "Figure 5.11: A recursive factorial machine.",
'8' => "Figure 5.12: Controller for a machine to compute Fibonacci numbers.",
],
];
2 changes: 2 additions & 0 deletions resources/lang/ru/exercises/5_12.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
"((op *) (reg n) (reg val))).",
'6' => "Расширьте интерфейс сообщений машины и включите в него доступ к новой информации. Чтобы проверить " .
"свой анализатор, примените его к машине Фибоначчи с рисунка 5.12 и рассмотрите получившиеся списки.",
'7' => "Рис. 5.11. Рекурсивная факториальная машина.",
'8' => "Рис. 5.12. Контроллер машины для вычисления чисел Фибоначчи.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Наверное, если тут не рисунок, а чисто текст, то можно "Рис. 5.12."?

],
];
57 changes: 57 additions & 0 deletions resources/views/exercise/listing/5_12.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,60 @@
<li>{{ __('exercises/5_12.description.5') }}</li>
</ul>
<p>{{ __('exercises/5_12.description.6') }}</p>
<pre><code>(controller
(assign continue (label fact-done)) ; set up final return address
fact-loop
(test (op =) (reg n) (const 1))
(branch (label base-case))
;; Set up for the recursive call by saving n and continue.
;; Set up continue so that the computation will continue
;; at after-fact when the subroutine returns.
(save continue)
(save n)
(assign n (op -) (reg n) (const 1))
(assign continue (label after-fact))
(goto (label fact-loop))
after-fact
(restore n)
(restore continue)
(assign val (op *) (reg n) (reg val)) ; val now contains n(n - 1)!
(goto (reg continue)) ; return to caller
base-case
(assign val (const 1)) ; base case: 1! = 1
(goto (reg continue)) ; return to caller
fact-done)
</code></pre>
<p>{{ __('exercises/5_12.description.7') }}</p>
<pre><code>(controller
(assign continue (label fib-done))
fib-loop
(test (op <) (reg n) (const 2))
(branch (label immediate-answer))
;; set up to compute Fib(n - 1)
(save continue)
(assign continue (label afterfib-n-1))
(save n) ; save old value of n
(assign n (op -) (reg n) (const 1)); clobber n to n - 1
(goto (label fib-loop)) ; perform recursive call
afterfib-n-1 ; upon return, val contains Fib(n - 1)
(restore n)
(restore continue)
;; set up to compute Fib(n - 2)
(assign n (op -) (reg n) (const 2))
(save continue)
(assign continue (label afterfib-n-2))
(save val) ; save Fib(n - 1)
(goto (label fib-loop))
afterfib-n-2 ; upon return, val contains Fib(n - 2)
(assign n (reg val)) ; n now contains Fib(n - 2)
(restore val) ; val now contains Fib(n - 1)
(restore continue)
(assign val ; Fib(n - 1) + Fib(n - 2)
(op +) (reg val) (reg n))
(goto (reg continue)) ; return to caller, answer is in val
immediate-answer
(assign val (reg n)) ; base case: Fib(n) = n
(goto (reg continue))
fib-done)
</code></pre>
<p>{{ __('exercises/5_12.description.8') }}</p>