CS 202 - Homework 3
Due: Monday 3/5, at the beginning of class
In this assignment you will write and assemble Σniac II
programs, and also complete a C implementation of a Σniac II
simulator that will allow you to test your assembly programs.
- Σniac II programming. For each of
the four questions below, write out the Σniac II program
in assembler (mnemonics), then
assemble it by hand into hexadecimal machine code.
Use the extension ".zn" for your Σniac
assembly (source) code files, and ".hex" for your machine code files.
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 Σniac II handout.
Use C-style comments (i.e.,
/* ... */ or // ...) 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.zn"
* 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
- Write a program "max.zn" 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.
- Write a program "odd.zn" 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.
- Write a program "mult.zn" 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!)
- Challenge: Write a program "runlong.zn" 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 24 steps. Can you write one that runs for more than
24? More that 32? More than 100?? (Note that this homework
problem is not optional; for full credit you need to submit a program
that runs for at least 25 steps.)
- 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
- Your program may not exploit any (potential) bugs in the
simulator, and the IC 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 top 5
programs that run for the largest number of steps.
- Complete the program zniac.c,
a simulator for the Σniac 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.
Upload your files using the HW 3
submission page by the beginning of your class section on
the due date. You should have 9 files to submit: max.zn,
odd.zn, mult.zn, runlong.zn,
max.hex, odd.hex, mult.hex,
runlong.hex, and zniac.c. All files should be in
plain text format, and all .zn and .c files should include a comment
with your name and the program name. Be careful to upload the correct
files, and pay close attention to any warning messages the upload