Advanced Object-Oriented Programming - Homework 1 | CS 591, Assignments of Programming Languages

Material Type: Assignment; Professor: Stefanovic; Class: ST: Prog Analy &Mechanization; Subject: Computer Science; University: University of New Mexico; Term: Fall 2004;

Typology: Assignments

Pre 2010

Uploaded on 07/23/2009

koofers-user-icm-1
koofers-user-icm-1 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 591/491 Advanced Object-Oriented Programming, Fall 2004
1
Homework 1 Warm-up exercise assigned Wednesday 25 August, due
Wednesday 1 September
Total points: 300.
Write an emulator for a hypothetical computer—a simple stack machine for integer list manipulation;
then write a corresponding translator.
1.1 Interpreter
1.1.1 General description
Consider a simple stack machine for integer list manipulation (an exercise devised by P. Sestoft, IT
Copenhagen). The stack machine has a program p, a program counter pc, a stack sthat may hold
integers and lists, and a stack top pointer sp. The instructions of the machine and their effect on the stack
are described by the following table:
Instruction Stack before Stack after Effect
CST i s i s Push integer constant i
ADD i2i1s(i1+i2)sAdd integers
SUB i2i1s(i1i2)sSubtract integers
DUP v s vvs Duplicate
SWAP v2v1sv1v2sSwap
POP v s sPop
GOTO a s sJump to a
IFNZRO a i s sJump to aif i6=0
CALL a v s v r s Call function at a, pushing return address r
RET v r s v s Return: jump to r
MKNIL sNil sPush Nil (the empty list)
MKCONS t i s Cons(i,t)sPush cons node
LISTCASE aNil ssIf Nil, do not jump
LISTCASE aCons(i,t)st i s If Cons, unpack components and jump to a
PRINT v s v s Print vand keep it
STOP sHalt the machine
In the table, istands for an integer, afor an address, within the program, rfor an address within the
program, tfor a list, vfor an arbitrary value, and sfor the remainder of the stack (0 or more values).
An instruction with an argument (CST,GOTO,IFNZRO,CALL,LISTCASE) takes up two positions in
the program.
Under any circumstances not covered by the table, the machine will crash. For instance, if the instruction
to be executed is an ADD but the top stack element is a list rather than an integer, the machine will crash.
pf3
pf4
pf5

Partial preview of the text

Download Advanced Object-Oriented Programming - Homework 1 | CS 591 and more Assignments Programming Languages in PDF only on Docsity!

Homework 1 — Warm-up exercise — assigned Wednesday 25 August, due

Wednesday 1 September

Total points: 300.

Write an emulator for a hypothetical computer—a simple stack machine for integer list manipulation; then write a corresponding translator.

1.1 Interpreter

1.1.1 General description

Consider a simple stack machine for integer list manipulation (an exercise devised by P. Sestoft, IT Copenhagen). The stack machine has a program p , a program counter pc , a stack s that may hold integers and lists, and a stack top pointer sp. The instructions of the machine and their effect on the stack are described by the following table:

Instruction Stack before Stack after Effect CST i si s Push integer constant i ADD i 2 i 1 s ⇒ ( i 1 + i 2 ) s Add integers SUB i 2 i 1 s ⇒ ( i 1 − i 2 ) s Subtract integers DUP v sv v s Duplicate SWAP v 2 v 1 sv 1 v 2 s Swap POP v ss Pop GOTO a ss Jump to a IFNZRO a i ss Jump to a if i 6 = 0 CALL a v sv r s Call function at a , pushing return address r RET v r sv s Return: jump to r MKNIL s ⇒ Nil s Push Nil (the empty list) MKCONS t i s ⇒ Cons( i , t ) s Push cons node LISTCASE a Nil ss If Nil, do not jump LISTCASE a Cons( i , t ) st i s If Cons, unpack components and jump to a PRINT v sv s Print v and keep it STOP s ⇒ Halt the machine

In the table, i stands for an integer, a for an address, within the program, r for an address within the program, t for a list, v for an arbitrary value, and s for the remainder of the stack (0 or more values).

An instruction with an argument (CST, GOTO, IFNZRO, CALL, LISTCASE) takes up two positions in the program.

Under any circumstances not covered by the table, the machine will crash. For instance, if the instruction to be executed is an ADD but the top stack element is a list rather than an integer, the machine will crash.

1.1.2 Code completion (40pts)

Given the skeleton of a list stack machine interpreter written in Java, provided below, complete the rest of the code.

Your code must be placed in a package named edu.unm.cs.cs591491aoop.fall2004. yourusername .liststackmachine.

According to the provided code, you are to complete the method execcode. You must also write a main method, which must accept command-line arguments as follows: the first argument on the com- mand line, a string, is the name of a file containing the “assembly” text of a program (i.e., machine instructions in human-readable form, as in the examples provided below); the second argument on the command line, an integer, is the initial value of the program counter; the remaining arguments (if any) on the command line, which are integers, are the initial contents of the stack, from top to bottom. Presum- ably you will write a separate load method, and, for debugging purposes, a disassemble method.

For example, if you put the assembly text:

0: CST 3

2: ADD

3: PRINT

4: POP

5: STOP

in a file add3.lsm, then you should be able to invoke the interpreter as follows:

yourfavoritejvm edu.unm.cs.cs591491aoop.fall2004. yourusername .liststack
machine.ListStackMachine add3.lsm 0 5

and expect to see 8 printed.

1.1.3 Code skeleton

(From P. Sestoft, IT Copenhagen.)

abstract class List { } class Nil extends List { public String toString () { return "Nil"; } } class ListStackMachine { private static int execcode (int [] p, Object [] s, int pc, int sp) { for (;;)

1.2.3 (5pts)

The instructions CALL and RET can be used to implement simple functions. What does the following stack machine program do, assuming that the integer 42 is on the stack top when execution is started at instruction address 0?

0: CALL 4

2: PRINT

3: STOP

4: DUP

5: DUP

6: MKNIL

7: MKCONS

8: MKCONS

9: MKCONS

10: RET

Show the contents of the stack after each instruction, in the order in which the instructions are executed.

1.2.4 (10pts)

Write a stack machine program that, given that integer n ≥ 0 is on the stack top, builds and prints an n -element list of the form Cons( n ,Cons( n − 1 ,...,Cons(1,Nil),...)). Provide both an iterative and a recursive solution.

1.2.5 (5pts)

The instruction LISTCASE can be used to test whether the list on the stack top is Nil or a Cons. If the stack top element is Nil, execution simply continues with the next instruction. If the stack top element is Cons( i , t ), then the integer i and the list tail t are unpacked on the stack and execution continues at address a.

Consider the following stack machine code fragment:

21: LISTCASE 27 23: CST 999 25: GOTO 28 27: POP 28: PRINT

Assume that the list Cons(111,Cons(222,Nil)) is on the stack top when the function at address 21 is called (by CALL 21). What is on the stack top, and is therefore printed, when control reaches instruction 28? What is printed if the empty list is on the stack top when CALL 21 is executed?

1.2.6 (10pts)

Write a stack machine function that, given that a list is on the stack top, computes the sum of the list elements. Provide both an iterative and a recursive solution.

1.2.7 (10pts)

For some programs and some initial states of the machine, the machine will crash. Write some programs that cause the machine to crash. What kinds of conditions cause a crash?

We would like to know for certain if a given stack machine program will crash when run. If we are given a program, an initial program counter, and an initial stack, can we prove that the program will crash, or, more usefully, that it will not crash? What do you think? If this cannot be done in general, discuss possible restrictions on admissible programs that may allow such proofs to go through.

1.2.8 (5pts)

Extend the machine with a new instruction GETVAR i that reads the contents of the i th stack element below the stack top and pushes that value onto the stack. Since the stack top is the 0th element below the stack top, GETVAR 0 should be equivalent to DUP.

1.2.9 (5pts)

Use the new GETVAR instruction to write a stack machine function that can create a list that contains n copies of v , like this:

Cons( v ,Cons( v ,...,Cons( v ,Nil)...))

The function should be called with a stack of the form n : v : s , that is, with n on the stack top and v below it, and should return with a stack of the form w : s where w is an n -element list all of whose elements are v. Provide both an iterative and a recursive solution.

1.2.10 (5pts)

Use the new GETVAR instruction to write a stack machine function that, given that integer n ≥ 0 is on the stack top, builds and prints an n -element list of the form

Cons( 2 n ,Cons( 2 n − 2 ,...,Cons(2,Nil),...)).

Provide both an iterative and a recursive solution.

1.2.11 (5pts)

Without using the GETVAR instruction, write a stack machine function that, given that integer n ≥ 0 is on the stack top, builds and prints an n -element list of the form

  • a doc directory containing various detailed project documentation as needed (including a detailed description of code structure (classes, interfaces, and how they are related) in a file structure.txt or, preferably, structure.ps, a description of the design process that led to this code structure in a file design.txt or, preferably, design.ps, and a subdirectory generated/javadoc with Javadoc-generated HTML files, exactly corresponding to the src files);
  • a tests directory with input files used for testing the correctness of the program, including both valid and invalid inputs;
  • a test-results directory with output files generated by the program on test inputs; and
  • a RESULTS file (a summary of the testing process, documenting the program’s compliance with the specification).

Note that for the translator you have to provide, as tests, a number of assembly programs covering, to the best of your ability, all programming situations, and for each its translation into a Java class file (suitably disassembled), together with tests that demonstrate that the translated code works correctly.

Place everything in a compressed tar file and email to the instructor.