Hi There!

I'm Dan Schlegel, an Associate Professor in the Computer Science Department at SUNY Oswego

Assignment 1

Microproject

Write a small C program which reads an input file, line by line, into an array and allows the user to retrieve the elements by index. The input file contains, on the first line, an integer number of lines to read after that first line. Each line after that will contain some character string.

For example, consider the following input:

When loaded, your program will make an array of length 3, and populate the cells with the lines of the program. After loading the file, your program should enter a command loop where the user can enter an index number and the program should output the corresponding string.

Main Project

In this project you will implement a stack machine in C. A stack machine is a simple model of computation in which the primary memory construct is a stack. There is a set of instructions which allow adding/removing integers from the stack, as well as performing various mathematical operations. Stack machine programs are lists of these instructions. Instructions are executed one after another, except for branching instructions which allow jumping to a specific instruction number in the program.

You must implement a stack of integers as a linked data structure (think linked list). Be sure you implement this yourself and don’t use code from the internet — it’s not hard and is a useful task. Be careful to properly allocate memory when you need to push and to free memory when you pop. Stack machine programs will be read from a file — you may either provide an argument or have your program ask for a file name. The files will begin with the number of instructions, followed by the list of instructions. You should store the instructions in some data structure for easy access by index.

To execute your program, you will need some sort of counter to keep track of where you are in the list of instructions. Decode and execute instructions one after another until you’ve reached the end of the program.

Instruction Set

Example

Below is an example input file for your stack machine which computes Euclid’s algorithm.

What follows is a trace of the computation:

Instruction NumberInstructionInputStackOutput
1IN2020
2IN1515, 20
3DUP 115, 15, 20
4LIT 00, 15, 15, 20
5IFEQ 1215, 20
6SWAP20, 15
7DUP 220, 15, 20, 15
8DROP15, 20, 15
9SWAP20, 15, 15
10MOD5, 15
11JUMP 35, 15
3DUP 15, 5, 15
4LIT 00, 5, 5, 15
5IFEQ 125, 15
6SWAP15, 5
7DUP 215, 5, 15, 5
8DROP5, 15, 5
9SWAP15, 5, 5
10MOD0, 5
11JUMP 30, 5
3DUP 10, 0, 5
4LIT 00, 0, 0, 5
5IFEQ 120, 5
12DROP5
13OUT5

Extra Credit (+10% to your score)

Augment your machine with 256 int-sized addressable memory locations which may be accessed by two new instructions, LOAD and STOR, which take as an argument a memory address $00 through $FF.

Write a stack machine program which demonstrates the use of these in some interesting way.