Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arithmetic operations in donald knuth's Mix assembly language

Tags:

assembly

knuth

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?

like image 589
coderasp Avatar asked Dec 06 '22 00:12

coderasp


2 Answers

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.

like image 171
bel3atar Avatar answered Dec 08 '22 14:12

bel3atar


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.

like image 39
smoore Avatar answered Dec 08 '22 14:12

smoore