5

I have been reading Donald Knuth's The Art of Programming, Volume 1, in which MIX is used as the assembly language. In the section where Knuth talks about arithmetic operations in MIX, I didn't understand how the subtraction, multiplication and division operations are carried out.

For example, the text book has this:

register A has the following word code: -| 1234 | 0 | 0 | 9 and a memory cell, say M, has the following word code: -| 2000 | 150 | 0.

The book says on performing A-M the result is: +| 766 | 149|? .

In MIX, the memory is split into words. Each word has the following: The first field represents sign(+ or -)
The next two bytes hold the address.
The next byte represents Indexing while the fifth byte is for field specification.
The last byte is for opcode.
The book says on performing A-M the result is: +| 766 | 149|? .

Can anyone help me with this?

Mooncrater
  • 4,146
  • 4
  • 33
  • 62
coderasp
  • 133
  • 1
  • 8
  • 1
    Can you explain the notation? That is, how is one supposed to interpret e.g. `-| 1234 | 0 | 0 | 9` ? – Michael Sep 16 '14 at 17:30
  • 2
    Whatever are your future intentions, instead of trying to decipher now certainly ancient MIX language your effort would pay better if you'd try to decipher Doland Knuth's **MMIX** - _"A RISC computer for the new millennium"_ - assembly language - http://mmix.cs.hm.edu/index.html ([Wikipedia: MMIX](http://en.wikipedia.org/wiki/MMIX)) – xmojmr Sep 17 '14 at 06:45
  • i have just started reading Donald Knuth's The Art of Computer Programming volume 1. I read it in the book that all the volumes will be re-edited with the new MMIX assembly language. Are they already available in MMIX? @xmojmr – coderasp Sep 17 '14 at 12:08
  • @user3464843 you can find out by yourself if you click the link above, then in the chapter "The MMIX Supplement" read _This supplement contains all the programs contained in volume 1 to 3 rewritten for MMIX. These programs are available here for download_ then click the **_here_** (show some effort. BTW: Welcome to Stack Overflow. Showing some research effort, some code etc. is always a good thing at this site :) – xmojmr Sep 17 '14 at 16:33

4 Answers4

6

We must remember what a byte means in MIX. A byte must be capable of holding:

  1. at leat 64 distinct values, and
  2. at most 100 distinct values

On a binary computer, the byte must be 6-bit large. Because it allows us to store 2⁶=64 distinct values, satisfying condition 1. And 64≤100, satisfying condition 2.

On a decimal computer, the byte must be 2-digit large. Because that would allow us to store 10²=100 distinct values, satisfying condition 1. And 100≤100, satisfying condition 2.

Let's see how the calculation is done on each of these computers.

On a 6-bit binary computer:

┌─┬─┬─┬─┐                           ┌─┬──────┬──────┬──────┐
│-│0│0│9│   would be represented as │-│000000│000000│001001│
└─┴─┴─┴─┘                           └─┴──────┴──────┴──────┘

and

┌─┬─┬─┬─┐                           ┌─┬──────┬──────┬──────┐
│-│150│0│   would be represented as │-│000010 010110│000000│
└─┴─┴─┴─┘                           └─┴──────┴──────┴──────┘

Subtracting the two gives us:

┌─┬──────┬──────┬──────┐
│+│000010 010101│110111│
└─┴──────┴──────┴──────┘

Which in decimal equals:

┌─┬─┬─┬──┐
│+│149│55│  (we'll call this result A)
└─┴─┴─┴──┘

On a 2-digit decimal computer:

┌─┬─┬─┬─┐                           ┌─┬──┬──┬──┐
│-│0│0│9│   would be represented as │-│00│00│09│
└─┴─┴─┴─┘                           └─┴──┴──┴──┘

and

┌─┬─┬─┬─┐                           ┌─┬──┬──┬──┐
│-│150│0│   would be represented as │-│01 50│00│
└─┴─┴─┴─┘                           └─┴──┴──┴──┘

Subtracting the two gives us:

┌─┬──┬──┬──┐
│+│01 49│91│
└─┴──┴──┴──┘

Which in decimal equals:

┌─┬─┬─┬──┐
│+│149│91│  (we'll call this result B)  
└─┴─┴─┴──┘

Conclusion

We notice that A≠B, but 149 is always there. It's the last byte that differs. So depending on the numeral system the MIX computer uses, the least significant byte will differ, whereas the following two bytes will always be the same. Hence the "?" in the book.

bel3atar
  • 913
  • 4
  • 6
4

I realize this question is a little old but I was working this out recently. The heart of issue is ambiguity; the printed representation of a MIX words is ambiguous.

According to Knuth a byte must hold at least 64 values (0..63) and no more than 100 (0..99) values. Some other answers would be invalid by a close reading of the spec. (pg 125 TAOCP vol 1)

Lets work this out in the binary and decimal interpretations. First the MIX word is explicitly converted then the arithmetic is performed in a familiar decimal mode. Finally the answer is converted back to a MIX representation.

BINARY MODE REALITY
1234 0 0 9 = [(1234 * 64^3) + (0 * 64*2) + (0 * 64) + 9] = 323485705

2000 150 0 = [(2000 * 64*3) + (150 * 64) + 0] = 524297600

-323485705 - -524297600 = 200811895

The MIX word binary representation of the answer is:

200811895 = [(766 * 64^3) + (149 * 64) + 55] = 766 149 55

Now for the decimal interpretation:

DECIMAL MODE REALITY
1234 0 0 9 = [(1234 * 10^3) + (0 * 10^2) + (0*10) + 9] = 1234009

2000 150 0 = [(2000 * 10^3) + (150 * 10) + 0] = 2001500

-1234009 - -2001500 = 767 491

The MIX word decimal representation is:

767491 = [(766 * 10^3) + (149 * 10) + 1] = 766 149 1

Note, the printed representation is ambiguous. eg 1234 0 0 9 can represent 323485705 or 1234009 at the same time. Depending on your reading of the words (binary or decimal mode) you are working two different problems with two unique answers.

The following table will summarize and hopefully make things clear.

       MIX            Binary           Decimal
rA  -1234 0 0 9    -323485705         -1234009
SUB -2000 150 0  - -524297600       - -2001500
    -----------    ----------         --------
      766 149 ?     200811895           767491 NOTE: 2 different answers!

Given the printed answer is 766 149 ? we can work out the ? value.

  766 149 0     200811840           767490
          ?            55                1

The MIX word representations are ambiguous enough without throwing in the field packing; it's easy to get this wrong. The field packing is not relevant as the operations work on entire words as a unit. Representing the results of the operations as field packed bytes is another layer of abstraction.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
smoore
  • 51
  • 2
  • Welcome to Stack Overflow! Thanks for sharing. I have no idea why someone downvoted this, but I think you're right. – ShreevatsaR Mar 01 '19 at 13:13
  • 1
    @ShreevatsaR: I'd assume the downvote was due to lack of code formatting in the original, making the tables an unreadable mess without looking at the markdown. Geister helpfully edited it, but not right away. Someone probably downvoted it from review, which happened before the edit according to the timeline https://stackoverflow.com/posts/54936440/timeline. (SO post timelines don't show vote timestamps, only the daily summaries, probably to avoid people trying to correlate votes to users) – Peter Cordes Mar 02 '19 at 05:56
  • Thanks @smoore, could you please explain why we must multiply the fields by powers of 64 and 10? Also, which of the other answers may not be valid by a close reading of the book? – user51462 Apr 19 '22 at 01:27
3

The subtraction operation is being performed, so one would intuitively think that the answer should be something like:

rA  - | 1234 | 0 | 0 | 9  | (before)
SUB - | 2000 |  150  | 0  |
---------------------------
      | +766 |  +150 | -9 | (after)

This is wrong, because even though you know that (3:4) is 150, the individual values of (3:3) and (4:4) cannot be determined. Also, the signs of the bytes do not agree. If you consider the case of 5 bits per byte, the machine sees the least significant bytes in this result as:

[ 321 x (150) ] - 9 = [ 321 x (149) ] + [ 320 x (23) ]

A machine that has 6 bits per byte will interpret this as:

[ 641 x (150) ] - 9 = [ 641 x (149) ] + [ 640 x (55) ]

And going one further, a 7-bits-per-byte machine will interpret this as:

[ 1281 x (150) ] - 9 = [ 1281 x (149) ] + [ 1280 x (119) ]

So you can see from these examples that 149 is present for any byte size, but the least significant byte varies depending on the machine. Thus, the correct answer is

rA  - | 1234 | 0 | 0 | 9 | (before)
SUB - | 2000 |  150  | 0 |
--------------------------
rA  + |  766 |  149  | ? | (after)
tn3rt
  • 260
  • 3
  • 13
3

Assume that the size of each byte is b. Therefore +|1234|0|0|9 can be written as :
-(1234*B³+9)
and -|2000|150|0| can be written as:
-(2000*B³+150*B+0).
Now subtracting the second number from the first one yields:
-(1234*B³+9)-(-(2000*B³+150*B))
=766*B³+150*B-6
But this can't be represented directly (as a term is negative), thus :
=766*B³+149*B+(B-6) Where B=2^b.

Therefore we don't know what the last block of the register will hold, because that is dependent upon the definition of the size of one byte, that is b.

Mooncrater
  • 4,146
  • 4
  • 33
  • 62
  • 1
    Although I like the thorough presentation of the above answer slightly more, your representation gives me a method that I'm going to hold on to for working through remaining examples in the book. Thank you. – Thomas Hodges Jul 18 '18 at 20:37