• No results found

arithmetic logic unit (ALU)

The arithmetic logic unit is the part of a computer system that actually performs calculations and logical comparisons on data. It is part of the central processing unit (CPU), and in practice there may be separate and multiple arithmetic and logic units (see cpu).

The ALU works by first retrieving a code that represents the operation to be performed (such as ADD). The code also specifies the location from which the data is to be retrieved and to which the results of the operation are to be stored. (For example, addition of the data from memory to a num- ber already stored in a special accumulator register within the CPU, with the result to be stored back into the accumu- lator.) The operation code can also include a specification of the format of the data to be used (such as fixed or float- ing-point numbers)—the operation and format are often combined into the same code.

In addition to arithmetic operations, the ALU can also carry out logical comparisons, such as bitwise operations that compare corresponding bits in two data words, corre-

sponding to Boolean operators such as AND, OR, and xOR (see bitWiseopeRations and booleanopeRatoRs).

The data or operand specified in the operation code is retrieved as words of memory that represent numeric data, or indirectly, character data (see memoRy, numeRicdata, and chaRacteRsandstRings). Once the operation is per- formed, the result is stored (typically in a register in the CPU). Special codes are also stored in registers to indicate characteristics of the result (such as whether it is positive, negative, or zero). Other special conditions called excep- tions indicate a problem with the processing. Common exceptions include overflow, where the result fills more bits than are available in the register, loss of precision (because there isn’t room to store the necessary number of decimal places), or an attempt to divide by zero. Exceptions are typically indicated by setting a flag in the machine status register (see flag).

t

he

b

ig

P

icture

Detailed knowledge of the structure and operation of the ALU is not needed by most programmers. Programmers who need to directly control the manipulation of data in the ALU and CPU write programs in assembly language (see assembleR) that specify the sequence of operations to be performed. generally only the lowest-level operations involving the physical interface to hardware devices require this level of detail (see devicedRiveR). modern compilers can produce optimized machine code that is almost as effi- cient as directly-coded assembler. However, understanding the architecture of the ALU and CPU for a particular chip can help predict its advantages or disadvantages for various kinds of operations.

Further Reading

Kleitz, William. Digital and Microprocessor Fundamentals: Theory and Applications. 4th ed. Upper Saddle River, N.J.: Prentice Hall, 2002.

Stokes, Jon. “Understanding the microprocessor.” Ars Technica.

Available online. URL: http://arstechnica.com/paedia/c/cpu/ part-1/cpu1-1.html. Accessed may 22, 2007.

array

An array stores a group of similar data items in consecutive order. Each item is an element of the array, and it can be retrieved using a subscript that specifies the item’s location relative to the first item. Thus in the C language, the state- ment

int Scores (10);

sets up an array called Scores, consisting of 10 integer val- ues. The statement

Scores [5] = 93;

stores the value 93 in array element number 5. One subtlety, however, is that in languages such as C, the first element of the array is [0], so [5] represents not the fifth but the sixth element in Scores. (many version of BASIC allow for setting either 0 or 1 as the first element of arrays.)

In languages such as C that have pointers, an equivalent way to access an array is to declare a pointer and store the address of the first element in it (see pointeRsand indi-

Rection):

int * ptr;

ptr = &Scores [0];

(See pointeRsandindiRection.)

Arrays are useful because they allow a program to work easily with a group of data items without having to use sep- arately named variables. Typically, a program uses a loop to traverse an array, performing the same operation on each element in order (see loop). For example, to print the cur- rent contents of the Scores array, a C program could do the following:

int index;

for (index = 0; i < 10; i++)

printf (“Scores [%d] = %d \n”, index, Scores [index]);

This program might print a table like this:

Scores [0] = 22 Scores [1] = 28 Scores [2] = 36

and so on. Using a pointer, a similar loop would increment the pointer to step to each element in turn.

An array with a single subscript is said to have one dimension. Such arrays are often used for simple data lists, strings of characters, or vectors. most languages also sup-

port multidimensional arrays. For example, a two-dimen- sional array can represent x and Y coordinates, as on a screen display. Thus the number 16 stored at Colors[10][40] might represent the color of the point at x=10, Y=40 on a 640 by 480 display. A matrix is also a two-dimensional array, and languages such as APL provide built-in support for mathematical operations on such arrays. A four-dimen- sional array might hold four test scores for each person.

Some languages such as FORTRAN 90 allow for defin- ing “slices” of an array. For example, in a 3 × 3 matrix, the expression mAT(2:3, 1:3) references two 1 × 3 “slices” of the matrix array. Pascal allows defining a subrange, or portion of the subscripts of an array.

a

ssociative

a

rrays

It can be useful to explicitly associate pairs of data items within an array. In an associative array each data element has an associated element called a key. Rather than using subscripts, data elements are retrieved by passing the key to a hashing routine (see hashing). In the Perl language, for example, an array of student names and scores might be set up like this:

%Scores = (“Henderson” => 86, “Johnson” => 87, “Jack- son” => 92);

The score for Johnson could later be retrieved using the reference:

$Scores (“Johnson”)

Associative arrays are handy in that they facilitate look-up tables or can serve as small databases. However, expanding the array beyond its initial allocation requires rehashing all the existing elements.

P

rogramming

i

ssues

To avoid error, any reference to an array must be within its declared bounds. For example, in the earlier example, Scores[9] is the last element, and a reference to Scores[10] would be out of bounds. Attempting to reference an out- of-bounds value gives an error message in some languages such as Pascal, but in others such as standard C and C++, it simply retrieves whatever happens to be in that location in memory.

Another issue involves the allocation of memory for the array. In a static array, such as that used in FORTRAN 77, the necessary storage is allocated before the program runs, and the amount of memory cannot be changed. Static arrays use memory efficiently and reduce overhead, but are inflex- ible, since the programmer has to declare an array based on the largest number of data items the program might be called upon to handle. A dynamic array, however, can use a flexible structure to allocate memory (see heap). The pro- gram can change the size of the array at any time while it is running. C and C++ programs can create dynamic arrays and allocate memory using special functions (malloc and free in C) or operators (new and delete in C++).

A two-dimensional array can be visualized as a grid, with the array subscripts indicating the row and column in which a par- ticular value is stored. Here the value 4 is stored at the location (1,2), while the value at (2,0), which is 8, is assigned to N. As shown, the actual computer memory is a one dimensional line of successive locations. In most computer languages the array is stored row by row.

In the early days of microcomputer programming, arrays tended to be used as an all-purpose data structure for stor- ing information read from files. Today, since there are more structured and flexible ways to store and retrieve such data, arrays are now mainly used for small sets of data (such as look-up tables).

Further Reading

Jensen, Ted. “A Tutorial on Pointers and Arrays in C.” Available online. URL: http://pw2.netcom.com/~tjensen/ptr/pointers. htm. Accessed April 12, 2007.

Sebesta, Robert W. Concepts of Programming Languages. 8th ed. Boston: Addison-Wesley, 2008.