COMP 210, Spring 2001
Homework 12 : Assembly Language
Due Dec.03 (mon), at start of class.

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.

  1. (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.

  2. (1pt)

    1. What Jam instruction does 4231 encode? 198760?
    2. What is the encoding of (blz R3 99)?

  3. (6pts) For this problem we'll write several versions of vector-max, which returns the maximum element of { data[0], ..., data[N-1] }. Use a global vector of numbers data, and placeholder N for its size (which you can presume is positive). That is, in Scheme, your solution would start off with

    (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.

    1. Write vector-max in Scheme, as a tail-recursive function (using an accumulator).
    2. Re-write this tail-recursive version as:
      ;; 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.))
    3. Translate this imperative version into Jam2000 assembly language, including comments.
    You need only write the contract/purpose once, and refer back to that in later versions.

    Note what tail-recursion becomes, in assembly language -- just a jmpi! Effectively, the function's arguments are being set!d over and over.