# Doc/Articles/Play154

**At Play With J**
... Table of Contents ... Previous Chapter ... Next Chapter

# 20. New Model Computer

*By* Eugene McDonnell. *First published in* Vector, 15, 4, *(April 1999), 106-112*.

Donald Knuth began writing his series called The Art of Computer Programming in 1962, and devised a mythical computer called MIX with which to illustrate computer language programs, and as a machine to be used for exercises. Half a lifetime has gone by since then â half a human lifetime, three or four computer lifetimes. His MIX design is now obsolete. A new machine, called MMIX, will replace it in subsequent revisions of his volumes. A preliminary writeup of this new machine is given in Knuthâs web site.

His home page address is http://sunburn.stanford.edu/~knuth

Briefly, MMIX operates on 64-bit words. It has 256 general-purpose 64-bit registers that can hold either fixed point or floating point numbers, or characters, or arbitrary binary data. Most instructions have the form OP X Y Z, where OP, X, Y, and Z are each 8-bit bytes. If OP is the ADD instruction, for example, the meaning is âX=Y+Zâ, that is, âSet register X to the contents of register Y plus the contents of register Z.â There are 256 op codes that fall into a dozen or so categories. The virtual memory available to an application is 2^64 bytes. There are no input-output instructions; files are handled as memory-mapped data. There is no operating system at the moment for MMIX. Knuth writes, âWhenever I have been asked if I will be writing a book about operating systems, my reply has always been âNix.â Therefore the name of MMIXâs operating system, NNIX, should come as no surprise.â

The purpose of this column is to discuss a few J-related aspects of this machine. I am attempting just now to write a J model of it. Time will tell if and how well I do this.

On this machine numbers are 64 bits long, though the design tolerates shorter numbers. For floating point numbers it uses the IEEE/ANSI standard 64-bit form, and accommodates the 32-bit forms only to transform them to the 64-bit form in computations. More interestingly, integer numbers are also 64 bits long. Thus an unsigned integer can take on any value from zero through 18,446,744,073,709,551,615, or approximately 1.8e19. Signed integers range between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. One of the reasons I chose J as simulation language is its extended integers, which allow me to use these large numbers directly, with the least amount of fuss: an occasional terminal â`x`â on a number, or â`x:`â preceding a number.

Many years ago I heard Luther Woodrum, the one who gave APL a good grade (he designed and implemented the grade primitives in APL\360) ask, âWhen will computer designers put in a maximum instruction?â In almost all computers, finding the maximum of two numbers involves a branch instruction. Something like this:

condition is x is less than y

branch to Label if condition is true

maximum is x

branch to exit

Label: maximum is y

exit:

One of the great motivations of computer designers is to reduce the number of branches required by programs executing at the machine level. Branches really slow things down. I recently attended, along with Larry Breed (the key designer and implemetor of APL\360), a talk Knuth gave on MMIX. I had read the preliminary MMIX writeup and hadnât found a maximum instruction, so after the lecture, in the question period, I asked how one found a maximum on MMIX. Knuth fumbled a bit, and gave an explanation which I didnât hear very well. Larry, however seemed to understand and accept it, so I didnât pursue the issue then, but the next day I phoned Larry and asked him for more. He explained that there was an identity that could be used to obtain maximum at the machine level without a branch, namely:

a max b is b + 0 max a - b

I said to Larry that that looked a bit circular, didnât it, defining maximum in terms of itself? Larry assured me that the circularity is only apparent. He said also that, before retiring from IBM a few years ago, he had been instrumental in getting the architects of an IBM computer (I think the RS6000) to add a âdifference or zeroâ instruction, primarily to handle maximum, and that now Knuth had devised a similar feature for MMIX. Itâs called âzero or set if negativeâ, written `ZSNI rX, rY, Z`, which acts as follows:

If register Y is negative, register X is set to Z, otherwise nothing happens. To see how this works in finding maximum, look at the three cases below. The two values are in A and B and the result is to appear in B.

case 1 case 2 case 3 A=8 B=3 A=3 B=8 A=3 B=3 SUB A,A,B 5 3 -5 8 0 3 A=A-B ZSNI A,A,0 5 3 0 8 0 3 A=0 max A ADD B,A,B 5 8 0 8 0 3 B=A+B

Somewhere below the machine instruction level there is a branch, but the machine is happy nevertheless, because the three instructions above execute very rapidly, and in a pipelined machine the pipeline is undisturbed, since no instruction-level branch is needed.

The next thing Iâd like to mention is exemplified by a frustrating aspect of IEEE floating point as implemented by two different systems: the little-endian and the big-endian school. In the little-endian implementations the bytes are numbered from left to right, 7 through 0, and the bytes in the big-endian school are numbered 0 through 7. This leads to strange things like, in J, looking at the value of a floating point number on a Macintosh and on an Intel machine gives two different pictures, one reversed from the other. Another bit of explanation I need to give you is that Knuth uses the prefix # in front of a number expressed in hexadecimal. One of the âbit fiddlingâ instructions is âmultiple orâ, denoted by MOR. The explanation given for it is:

Suppose the 64 bits of registers Y and Z are indexed by pairs such that the bytes are numbered from 0 to 7 and the bits within the bytes also by 0 through 7; then the MOR operation replaces bit ij of register X by the bit

y,,0j,,z,,i0,, or y,,1j,,z,,i1,, or ... or y,,7j,,z,,7i,,

Thus, for example, if register Z contains #0102040810204080, MOR reverses the order of the bytes in register Y, converting between little-endian and big-endian addressing.

What Knuth has designed, if the above is opaque to you, is a special case of the good old-fashioned âor dot andâ inner product. When I came to describing this instruction in my simulation, in addition to standard housekeeping, all I had to write was

U =. B +./ . *. A

where B and A are 8-by-8 boolean matrices representing the 64 bits in the two operands.

If I get anywhere with this simulation, Iâll probably report on it in a future column.