You are NOT required to show the template for your programs (woo-hoo!). However, your functions should still follow the template, as appropriate. And of course, each function should still have a contract and purpose, and reasonable test cases.
Before you tackle the homework, remind yourself of our General Advice, Advice on Homeworks (in particular: staple and provide questions), and the Grading Guidelines.
You'll want to use the Jam2000 reference sheet.
(3pt)
Write a Jam program which computes
sum/n
(an integer, just as the div
instruction returns));
if n
is zero, return 0 as your answer instead.
Presume that sum
is held in memory location #100,
and n
in location #101.
The answer should be placed in location #102.
Before running your program, place the inputs into those memory locations, of course. Keep your answer in a machine-inspector script file, as mentioned in lab and lecture. (That is, the script file contains inspector commands like "p" and "(ldi R3 17)" and "x". Your script might start off with "r y" to reset the machine (the "y" to confirm).) You can use "l" to load your script from inside the machine inspector, to save you typing.
Pre-lab peek:
You can run the machine inspector by typing jam at
an owlnet unix prompt;
or from drscheme by loading the teachpack
/home/comp210/Projects/Jam/jam.ss
and evaluate (walk)
.
Either way, "?" from inside the inspector gives a list of legal commands.
In your hard copy, include showing the interesting part of memory/registers before and after running your program.
(1pt)
(blz R3 99)
?
(6pts)
For this problem we'll write several versions of
vector-max
, which returns
the maximum element of
(define data (vector 14 7 -3 17 1)) (define N (vector-length data))(This isn't good style to have the vector be global, but we'll do it pedagogically to stress the similarity between the different versions.) In general, you may presume that N >= 1 is always guaranteed, so that calling
vector-max
makes sense.
vector-max
in Scheme,
as a tail-recursive function (using an accumulator).
;; vector-max-imperative: --> (void) ;; Purpose: set! max-so-far to be the largest data value ;; with index in [i,N), ;; Side-effect: modify both max-so-far ...(You may use a version of fori= if you like, but you don't have to (only because of where the next problem is going -- in general it is good practice to separate the code controlling the loop from the body of the loop.))
Note what tail-recursion becomes, in assembly language
-- just a jmpi
!
Effectively, the function's arguments are
being set!
d over and over.