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:
'OUT 15'
, whose execution prints the contents of memory location 15
, or
-17
, or
239
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.
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.
Word
.
ALU
.