Lab 2 -- An Arithmetic and Logic Unit

Related Reading

1.1 Machine Words

The memory of a computer is divided into fixed-sized units called cells. Each cell has a unique address and can hold one machine word. A word is the unit of information in the machine. At the hardware level, a machine word is a collection of bits with no inherent meaning: the same word can mean different things in different contexts. For example, the 8-bit word 11101111 could refer to:

Dealing with bit-encodings is tedious. We will therefore abstract from the level of bits in our representation of machine words and use a Java object to represent machine words. Design and implement a class Word to represent machine words.

1.2 The Arithmetic and Logic Unit (ALU)

The Arithmetic and Logic Unit (which is usually referred to as the ALU) is the subsystem that performs operations such as addition (add), subtraction (sub), incrementing (inc), decrementing (dec), and comparison (compare). The arithmetic operations have the usual meaning. The comparison operation takes two machine words and sets indicators based on their relative values. The ALU contains three condition codes that serve as indicators: GT, EQ, and LT, which stand for greater than, equal to, and less than, respectively. The operation compare(x,y) returns no value but sets the condition codes as follows:

	x  > y 		GT=true	      EQ=false      LT=false
	x  = y 		GT=false      EQ=true       LT=false
	x  < y 		GT=false      EQ=false      LT=true

Abstractly speaking, the ALU is just an object with methods add, sub, inc, dec, and compare that implement the required operations, as well as three methods to read the contents of the condition codes. This analysis is formalized in the following interface for the ALU:

public interface ALUI {
	boolean fetchGT ();
	boolean fetchEQ ();
	boolean fetchLT ();

	Word add (Word w1, Word w2);
	Word sub (Word w1, Word w2);
	Word inc (Word w);
	Word dec (Word w);
	void compare (Word w1, Word w2);
}

Implement a class ALU that implements the ALUI interface.

1.3 Exercise

  1. Design and implement the class Word.
  2. Implement the class ALU.
  3. Write a little program that tests every ALU operation.
Turn in your code during class on Monday, April 10th. Write the lab (day and time) that you are attending on top of your assignment in addition to your name and id number.


Visited times since March 23, 1999 (or the last crash).