CS 202 - Homework 3 - 9/24/07

Due: Monday, 10/1/07, in class (or by 2pm the latest)

In this assignment, you will write and assemble Sniac II programs, and also complete a C implementation of a Sniac II simulator that will allow you to test your assembly programs. Please hand in printouts of all your programs, and also submit them electronically as explained below. On the first page of your written submission, please write your own name as well as the names of the students with whom you collaborated.
  1. Sniac II programming. For each of the four questions below, write out the Sniac II program in assembler (mnemonics), then assemble it by hand into hexadecimal machine code. Use the extension ".z" for your Sniac assembly (source) code files, and ".hex" for your machine code files. Use symbolic names for variables, constants, and branch targets. Please type your programs using a text editor, and use a format similar to the second example in the Sniac II handout. Use C-style comments (i.e., /* ... */) to include comments with your name and a short description of what the program does. There is some flexibility as to what your assembly code looks like, but be sure to be consistent. Your notation should include (hex) addresses, (hex) opcodes, and (text) mnemonics and labels. Here is an example:
    /*  Zniac II program "example.z"
     *  Daniel Scharstein
     *
     *  computes Z = X+Y
     *  and stores Z in RAM at location 0x1f
     */
    00: 04      ld  A, X
    01: 45      add A, Y
    02: 7f      st  A, Z
    03: ff      halt
    04: 02  X:  2
    05: 03  Y:  3
    
    1f:     Z:
    
    1. Write a program "max.z" to compute the maximum of two non-negative numbers in ROM (each in the range of 0..127) and store the result in RAM at location 0x1f.

    2. Write a program "odd.z" that tests whether a non-negative number in ROM is even or odd and loads the accumulator with 1 if it is odd, and 0 if it is even.

    3. Write a program "mult.z" that computes the product of two non-negative numbers in ROM and stores the result in the accumulator. (You may assume that the multiplication does not result in overflow, i.e., that the result is less or equal to 127.) Does your program work if one or both number are negative? In a comment, explain in which cases it works / doesn't work, and why. (Be sure to verify your answer with the simulator!)

    4. Challenge: Write a program "runlong.z" that runs for as many steps as possible (but not forever) without ever executing the "bneg" instruction, and eventually stops properly by executing the "halt" instruction. Obviously, it's not too hard to write a program that runs for 23 steps. Can you write one that runs for more than 24? More that 32? More than 100??
      Notes:
      • Make sure to explain in a comment what your program is trying to do so I can award partial credit even if your program doesn't work properly.
      • Your program may not exploit any (potential) bugs in the simulator, and the PC should at all times contain a value between 0 and 31 (inclusive).
      • You should not assume that the RAM or the accumulator contains zeros when the computer starts running.
      • Extra credit will be awarded for the program that runs for the largest number of steps.

  2. Complete the program zniac.c, a simulator for the Sniac II. A skeleton and sample input files are provided in my directory ~schar/cs202/hw3/.

    Run your programs from part 1 by creating files "max.hex", "odd.hex", "mult.hex", and "runlong.hex" that contain your machine code in hex, and make sure they work correctly.

  3. How much time did it take you to complete this assignment?

Electronic submission

Make sure that all your files for this assignment (i.e., "zniac.c", "max.z", "odd.z", "mult.z", "runlong.z", "max.hex", "odd.hex", "mult.hex", and "runlong.hex") are in one directory. Please use these names, otherwise my testing scripts won't find your files. Then, go to this directory and type
  ~schar/bin/submit202
This will copy all files in your current directory and store them in my account. You can resubmit your files as many times as you want - only the newest submission will be kept. The final deadline for both paper and electronic submission is Monday, 2pm.

Have fun!