Probably the reason you’ve come to this page is because someone asked you to develop something in MIXAL or learn to use MIX. So we are going to keep things as simple as possible and only focus on understanding MIX’s architecture and learning to write MIXAL source files.

MIX is a hypothetical computer used in the well-known Art of Computer Programming written by Knuth. Since the book has become very popular, MIX was used as an easier way to make students dive into computer architecture and basic assembly programming. In other words, MIX is a primitive hypothetical computer that will help you understand better the real world machines.

Unfortunately, MIX and its assembly language, MIXAL, are very poorly documented online for a beginner. Of course, if you have access to a copy of Art of Computer Programming, you can always read the book. But I didn’t have the book, so I had to go through some technical documentation and old text. On the other hand, MIX is not hard to comprehend if you have the right material. Thus, I hope this guide will help the beginners get their hands dirty.

Emulators

Since MIX is a hypothetical computer, the only way to see it working and write some code is through emulators. There is a great selection of emulators in the official MIX and MIXAL site found here , in the MIXware section.

If you are on Linux, your best bet is to go for GNU MDK. It’s command line based, works great and supports all of the I/O devices. Another great option for quick testing is this online emulator. Very easy to use and works great, props to that man!

As for Windows, there are already lots of them to select. JMixSim seems nice even though I haven’t used it a lot

Finally, your best friend while developing, no matter what emulator you will use is the GNU MDK documentation. It is very well written. In fact, it contains everything you will need but it’s not the best starting point if you are a beginner. You can find it here and we are going refer to it in the next chapters.

Architecture

For reference, please open the online emulator I mentioned earlier.

MIX is the simpliest kind of computer. It has a RAM of 4000 memory words or simply memory “cells” for those unfamiliar with basic computer architecture. This is the “MEMORY” part found in Dan’s emulator. Each word is a row with an address number(starting from 0000) and holds 6 bytes. Oddly enough, in MIXAL 1 byte = 6 bits.

What do we we store there? First of all you can store integers . Note that, the first byte (the left one) is used to store the sign (positive, negative). Thus, there are 5 bytes remaining to store our integer which can hold numbers from -1.073.741.824 to 1.073.741.823.

Moreover, you can store strings. Each byte is used to represent a character. Thus we have 64 different characters for each byte. Refer to the following table for byte values-character correlation.

00   01 A 02 B 03 C 04 D 05 E 06 F 07 G
08 H 09 I 10 ~ 11 J 12 K 13 L 14 M 15 N
16 O 17 P 18 Q 19 R 20 [ 21 # 22 S 23 T
24 U 25 V 26 W 27 X 28 Y 29 Z 30 0 31 1
32 2 33 3 34 4 35 5 36 6 37 7 38 8 39 9
40 . 41 , 42 ( 43 ) 44 + 45 - 46 * 47 /
48 = 49 $ 50 < 51 > 52 @ 53 ; 54 : 55

In memory cells we can also store instructions. But we will get into that later.

MIX has a CPU of course which has 9 registers with the most important being rA and rX. Registers are one-word “memories” used by the CPU for storing an important value or performing arithmetic operations.

Apart from these three registers, there is a program counter(“PC”) which indicates the position of the instruction in memory, that the CPU will process in the next step.

There is also the “OV” flag (a theoretical light indicator) that turns on when an overflow has happened. This happens, for example, when the result of adding two numbers is greater than 1.073.741.823 and thus it can not be stored in the memory.

Finally, there is one more register, “CI”, which takes three values: L(ess), E(qual) and G(reater) and is used when performing comparisons. Don’t sweat it too much, you will fully understand them when you start writing source code.

Finally, for I/O operations, MIXAL is connected to a variety of devices like printers, consoles, tape machines etc that can be accessed by writing to the memory addresses 0-20. For example 0-7 is used for I/O with the tape and 19 with the terminal.

MIXAL and writing source code

To write a program for MIX, you need to write it in a specific assembly language called MIXAL and “compile” it (everywhere is referred as “compiling” but the correct term is “assembling”). In Dan’s simulator you just have to paste the source code into the left text box and click compile. But if you are using an emulator like GNU MDK, you need to compile it first (refer to the documentation).

A source file includes lines of code, each line containing exactly one instruction. Then, every instruction is loaded into a memory word.

Each instruction has the structure: LABEL OPERATOR VALUE

  • LABEL : a symbolic name for the current instruction or memory address.
  • OPERATOR : one of the MIXAL operators (e.g. ADD, SUB, LDA)
  • VALUE : the value that the operator uses.

To make it easier to understand, whenever you change line in the source code file, you move to the next memory word. This will become clear in the following example:

Here is some example code.

START    EQU    2000         
         ORIG   START        
N        CON    4            
         LDA    N            
         ADD    =1=          
         STA    N            
         HLT                 
         END    START       

It is a very silly program but it will do for the purpose of this tutorial.

The first line makes the label START equal to the memory address 2000. The next line indicates that the first instruction of our program is the value of the label START (in this case 2000). The next instruction will be placed in 2001, the following in 2002 etc. Try pasting the code into Dan’s emulator and hit compile. You will see the memory cells 2000-2004 filling with the instruction code.

Line 3 is the first instruction of our program. It indicates that the current memory address(2000) can now be referred with the label N. Also, CON, sets the value of the current address to 4. And we just declared a variable and assigned a value to it.

Line 4 loads the contents of N(address 2000) to the register rA. Currently the contents of N are 4 so rA takes the value 4.

Line 5 adds to the rA the integer 1. Whenever the VALUE field of the instruction is wrapped in “=”, it’s a literal integer value. If we hadn’t put the “=” signs, it would have added the contents of the address 1 to rA. So why didn’t we use =4= in line 3? Well, CON instruction is never followed by a memory address and thus 4 is a literal.

Line 6 stores the value of rA to N. You can now notice that the value of memory address 2000 (remember: N stands for address 2000) has the value 5.

Finally line 7 stops the execution of our program. Line 8 indicates that the program that started in address START (address 2000), ends.

Feel free to run the program. You can also run it step by step to see the result after the execution of each instruction. Notice that the PC increments by 1 every time you hit step and the CPU moves to the next instruction/memory address.

This was a very basic program. If you can understand the structure of the source file, writing MIXAL programs is a piece of cake. All you need to do is learn the instructions and their syntax by reading this.

That’s all for now. Hopefully, I will write some more posts on developing on MIXAL, explaining some tricky instructions or explaining some source code.


Comments