     [file version.texi not included]

This file documents the the MDK utilities for developing programs using Donald Knuth's MIX language.

Copyright (C) 2000 José Antonio Ortega Ruiz

Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.

Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided also that the sections entitled ``Copying'' and ``GNU General Public License'' are included exactly as in the original, and provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Free Software Foundation.

This file documents the MDK utilities to develop programs using Donald Knuth's MIXAL language.  MIXAL is an assembler-like language for writing programs for the (virtual) MIX computer. They are described in the first volume of The Art of Computer Programming by D. Knuth (Addison Wesley, 1997), and used throughout the (up to now) three published volumes.

MDK was written by José Antonio Ortega Ruiz and is released under the GNU General Public license (see Copying), so that users are free to share and change it.

Introduction
************

In his book series The Art of Computer Programming (published by Addison Wesley), D. Knuth uses an imaginary computer, the MIX, and its associated machine-code and assembly languages to ilustrate the concepts and algorithms as they are presented.

The MIX's architecture is a simplified version of those found in real CSIC CPUs, and the MIX assembly language provides a set of primitives that will be very familiar to any person with a minimum experience in assembly programming. The MIX/MIXAL definition is powerful and complete enough to provide a virtual development platform for writing quite complex programs, and close enough to real computers to be worth using when learning programming techniques. At any rate, if you want to read and learn from Knuth excellent books on computer programming, a MIX development environment would come in handy.

The MDK package aims at providing such virtual development environment on a GNU box. Thus, MDK offers you a set of utilities to simulate the MIX computer and to write, compile, run and debug MIXAL programs. As of version VERSION, MDK includes the following programs:

mixvm
     MIX virtual machine. Emulation of the MIX computer.

mixasm
     MIXAL assembler. Assembler which translates MIXAL source files into programs that can be run (and debugged) by mixvm.

mixvm implements a simulator of the MIX computer, giving you a virtual machine for executing and debugging MIX programs. These binary programs could be written by hand, but it is easier to produce them compiling MIXAL source files, using the MIXAL assembler mixasm.

This manual gives you a brief survey of MIX and MIXAL, and a thorough description of the use of the MDK utilities.

MIX and MIXAL overview
**********************

In the book series The Art of Computer Programming, by D. Knuth, a virtual computer, the MIX, is used by the author, together with the set of binary instructions that the virtual CPU accepts, in the example programs and exercises. Like any other real computer, there is a symbolic assembler language that can be used to program the MIX: the MIX assembly language, or MIXAL for short. The MIX computer architecture and the MIXAL language are defined in volume 1 of the series, Fundamental Algorithms. In the following subsections you will find a brief survey of these topics, which is not meant to replace the precise description given in the book (if you are interested in using the MDK utilities, most probably the reason is that you have access to a copy of TAOCP), but to serve as a quick reminder of key points and nomenclature used in the rest of this manual.

The MIX computer
================

In this section, you will find a brief description of the MIX computer, its components and instruction set.

MIX architecture
----------------

The basic information storage unit in the MIX computer is the byte, which stores positive values in the range 0-63 . Note that a MIX byte can be then represented as 6 bits, instead of the common 8 bits for a regular byte. Unless otherwise stated, we shall use the word byte to refer to a MIX 6-bit byte.

A MIX word is defined as a set of 5 bytes plus a sign. The bytes within a word are numbered for 1 to 5, being byte number one the most significant one. The sign is denoted by index 0. Graphically,

      -----------------------------------------------
     |   0   |   1   |   2   |   3   |   4   |   5   |
      -----------------------------------------------
     |  +/-  | byte  | byte  | byte  | byte  | byte  |
      -----------------------------------------------

You can refer to subfields within a word using a field specification or fspec of the form (L:R), where L denotes the first byte and R the last byte of the subfield. When L is zero, the subfield includes the word's sign. An fspec can be also represented as a single value F, given by F = 8*L + R.

The MIX computer stores information in registers, that can store either a word or two bytes and sign (see below), and memory cells, each one containing a word. Specifically, the MIX computer has 4000 memory cells with addresses 0 to 3999 (i.e., two bytes are enough to address a memory cell) and the following registers:

rA
     A register. General purpose register holding a word. Usually its contents serves as operator for arithmetic and storing instructions.

rX
     X register. General purpose register holding a word. Often it acts as an extension or a replacement of rA.

rJ
     J (jump) register. This register stores positive two-byte values, usually representing a jump address.

rI[1-6]
     Index registers. These six registers can store a signed two-byte value. Their contents is used as indexing values for the computation of an effective memory address.

In addition, the MIX computer contains:

  - An overflow toggle (a single bit with values on or off). In this manual, this toggle is denoted OV.

  - A comparison indicator (having three values: EQUAL, GREATER or LESS). In this manual, this indicator is denoted CM.

  - Input-output devices. Each device is labelled as Un, where n runs from 0 to 19 (U0-7 are magnetic tape units, U8-15 are disks and drums, U16 is a card reader, U17 is a card writer, U18 is a line printer and U19, a paper tape.).

MIX instructions are codified as words with the following subfield structure:

Subfield   fspec   Description

ADDRESS   (0:2)
The first two bytes plus sign are the address field. Combined
with the INDEX field, denotes the memory address to be used by the
instruction.
INDEX   (3:3)
The third byte is the index, normally used for indexing the
address (The actual memory address the instruction refers to, is
obtained by adding to ADDRESS the value of the rI register
denoted by INDEX.).
MOD   (4:4)
Byte four is used either as an operation code modifier or as a field
specification.
OPCODE   (5:5)
The last (least significant) byte in the word denotes the operation
code.

or, graphically,

      ------------------------------------------------
     |   0   |   1   |   2   |   3   |   4   |   5    |
      ------------------------------------------------
     |        ADDRESS        | INDEX |  MOD  | OPCODE |
      ------------------------------------------------

The MIX computer understands a quite complete set of instructions commonly found in real computers, including arithmetic, logical, storing, comparison and jump instructions. We refer the reader to D. Knuth's TAOCP (volume 1, section 1.3.1) for a complete description of these instructions. See MIX instruction set, gives a quick synopsis of the available instructions.

MIX instruction set
-------------------

The following subsections summarize the instruction set of the MIX computer. In this description M stands for the memory address obtained after indexing the ADDRESS subfield of the instruction (using its INDEX byte), and V is the contents of the subfield indicated by MOD of the memory cell with address M.

Loading operators
.................

The following instructions are used to load memory contents into a register.

LDA
     Load rA. OPCODE = 8, MOD = fspec. rA <- V.

LDX
     Load rX. OPCODE = 15, MOD = fspec. rX <- V.

LDi
     Load rIi. OPCODE = 8 + i, MOD = fspec. rIi <- V.

LDAN
     Load rA negative. OPCODE = 16, MOD = fspec. rA <- -V.

LDXN
     Load rX negative. OPCODE = 23, MOD = fspec. rX <- -V.

LDiN
     Load rIi negative. OPCODE = 16 + i, MOD = fspec. rIi <- -V.

Storing operators
.................

The following instructions are used to store a subfield of a register into a memory location.

STA
     Store rA. OPCODE = 24, MOD = fspec. V <- rA.

STX
     Store rX. OPCODE = 31, MOD = fspec. V <- rX.

STi
     Store rIi. OPCODE = 24 + i, MOD = fspec. V <- rIi.

STJ
     Store rJ. OPCODE = 32, MOD = fspec. V <- rJ.

STZ
     Store zero. OPCODE = 33, MOD = fspec. V <- 0.

Arithmetic operators
....................

The following instructions perform arithmetic operations between rA and rX register and memory contents.

ADD
     Add and set OV if overflow. OPCODE = 1, MOD = fspec. rA <- rA +V.

SUB
     Sub and set OV if overflow. OPCODE = 2, MOD = fspec. rA <- rA - V.

MUL
     Multiply V times rA and store the 10-bytes product in rAX. OPCODE = 3, MOD = fspec. rAX <- rA x V.

DIV
     rAX is considered a 10-bytes number, and it is divided by V. OPCODE = 4, MOD = fspec. rA <- rAX / V, rX <- reminder.

Address transfer operators
..........................

In these instructions, M (the address of the instruction after indexing) is used as a number instead of as the address of a memory cell.

ENTA
     Enter rA. OPCODE = 48, MOD = 2. rA <- M.

ENTX
     Enter rX. OPCODE = 55, MOD = 2. rX <- M.

ENTi
     Enter rIi. OPCODE = 48 + i, MOD = 2. rIi <- M.

ENNA
     Enter negative rA. OPCODE = 48, MOD = 3. rA <- -M.

ENNX
     Enter negative rX. OPCODE = 55, MOD = 3. rX <- -M.

ENNi
     Enter negative rIi. OPCODE = 48 + i, MOD = 3. rIi <- -M.

INCA
     Increase rA. OPCODE = 48, MOD = 0. rA <- rA + M.

INCX
     Increase rX. OPCODE = 55, MOD = 0. rX <- rX + M.

INCi
     Increase rIi. OPCODE = 48 + i, MOD = 0. rIi <- rIi + M.

DECA
     Decrease rA. OPCODE = 48, MOD = 1. rA <- rA - M.

DECX
     Decrease rX. OPCODE = 55, MOD = 0. rX <- rX - M.

DECi
     Decrease rIi. OPCODE = 48 + i, MOD = 0. rIi <- rIi - M.

Comparison operators
....................

The following instructions compare the value of a register with V, and set the CM indicator to the result of the comparison.

CMPA
     Compare rA with V. OPCODE = 56, MOD = fspec.

CMPX
     Compare rX with V. OPCODE = 63, MOD = fspec.

CMPi
     Compare rIi with V. OPCODE = 56 + i, MOD = fspec.

Jump operators
..............

The following instructions provoke jumps by setting the program counter (address of the next instruction to fetch) to M (if a condition is met). If a jump occurs, the value of the next instruction address that would have been fetched in the absence of the jump is stored in rJ (except for JSJ).

JMP
     Unconditional jump. OPCODE = 39, MOD = 0.

JSJ
     Unconditional jump, but rJ is not modified. OPCODE = 39, MOD = 1.

JOV
     Jump if OV is set (and turn it off). OPCODE = 39, MOD = 2.

JNOV
     Jump if OV is not set (and turn it off). OPCODE = 39, MOD = 3.

JL
JE
JG
JGE
JNE
JLE
     Jump according to the value of CM. OPCODE = 39, MOD = 4, 5, 6, 7, 8, 9.

JAN
JAZ
JAP
JANN
JANZ
JANP
     Jump if the contents of rA is, respectively, negative, zero, positive, non-negative, non-zero or non-positive. OPCODE = 40, MOD = 0, 1, 2, 3, 4, 5.

JXN
JXZ
JXP
JXNN
JXNZ
JXNP
     Jump if the contents of rX is, respectively, negative, zero, positive, non-negative, non-zero or non-positive. OPCODE = 47, MOD = 0, 1, 2, 3, 4, 5.

JiN
JiZ
JiP
JiNN
JiNZ
JiNP
     Jump if the contents of rIi is, respectively, negative, zero, positive, non-negative, non-zero or non-positive. OPCODE = 40 + i, MOD = 0, 1, 2, 3, 4, 5.

Miscellaneous operators
.......................

SLA
SRA
SLAX
SRAX
SLC
SRC
     Shift rA or rAX left, right, or rAX circularly left or right. M specifies the number of bytes to be shifted. OPCODE = 6, MOD = 0, 1, 2, 3, 4, 5.

MOVE
     Move MOD words from M to the location stored in rI1. OPCODE = 7, MOD = no. of bytes.

NOP
     No operation. OPCODE = 0, MOD = 0.

HLT
     Halt. Stops instruction fetching. OPCODE = 5, MOD = 2.

Input-output operators
......................

The following instructions perform input-output operations.

IN
     Transfer a block of words from the specified unit to memory, starting at address M. OPCODE = 36, MOD = I/O unit.

OUT
     Transfer a block of words from memory (starting at address M) to the specified unit. OPCODE = 37, MOD = I/O unit.

IOC
     Perfom a control operation (given by M) on the specified unit. OPCODE = 35, MOD = I/O unit.

JRED
     Jump to M if the specified unit is ready. OPCODE = 38, MOD = I/O unit.

JBUS
     Jump to M if the specified unit is busy. OPCODE = 34, MOD = I/O unit.

Conversion operators
....................

The following instructions convert between numerical values and their character representations.

NUM
     Convert rAX, assumed to contain a character representation of a number, to its numerical value and store it in rA. OPCODE = 5, MOD = 0.

CHAR
     Convert the number stored in rA to a character representation and store it in rAX. OPCODE = 5, MOD = 1.

MIXAL
=====

The MIX computer can be programmed using an assembly language, MIXAL, which provides a symbolic way of writing the binary instructions understood by the imaginary MIX computer. If you have used assembler languages before, you will find MIXAL a very familiar language. MIXAL is fully described in volume 1 of D. Knuth's TAOCP. This section is not meant as a replacement of the book's description, but as a brief survey of MIXAL.

Instructions
------------

MIX instructions are written in MIXAL according to the following pattern:

     [LABEL]   OPCODE  [OPERAND]   [COMMENT]

where OPERAND is of the form

     [ADDRESS][,INDEX][(MOD)]

Items between square brackets are optional, and

LABEL
     Is an alphanumeric identifier (a symbol) which gets the value of the current compilation address, and can be used in subsequent expressions.

OPCODE
     Is a literal denoting the operation code of the instruction (e.g. LDA, STA) or an assembly pseudoinstruction (e.g. ORG, EQU).

ADDRESS
     Expression evaluating to the address subfield of the instruction.

INDEX
     Expression evaluating to the index subfield of the instruction. It defaults to 0 (i.e., no use of indexing) and can only be used when ADDRESS is present.

MOD
     Expression evaluating to the mod subfield of the instruction. Its default value, when omitted, depends on OPCODE.

COMMENT
     Any number of spaces after the operand mark the beggining of a comment, i.e. any text separated by white space from the operand is ignored by the assembler (note that spaces are not allowed within the OPERAND field).

Note that spaces are not allowed between the ADDRESS, INDEX and MOD fields if they are present. White space is used to separate the label, operation code and operand parts of the instruction (In fact, Knuth's definition of MIXAL restricts the column number at which each of these instruction parts must start. The MIXAL assembler included in MDK, mixasm, does not impose such restriction.).

MIXAL instructions can be either one of the MIX machine instructions (see MIX instruction set) or one of the following assembly pseudoinstructions:

ORIG
     Sets the value of the memory address to which following instructions will be allocated after compilation.

EQU
     Used to define a symbol's value, e.g. SYM  EQU  2*200/3.

CON
     The value of the given expression is copied directly into the current memory address.

ALF
     Takes as operand five characters, constituting the five bytes of a word which is copied directly into the current memory address.

END
     Marks the end of the program. Its operand gives the start address for program execution.

Sample MIXAL instructions are

     HERE     LDA  2000
              LDX  HERE,2(1:3)  this is a comment
              JMP  1234
     NEXT     STA  HERE*2(8)
              ORG  4000
     VALUE    EQU  NEXT+HERE//2

Comments
--------

Any line starting with an asterisk is treated as a comment and ignored by the assembler.

     * This is a comment: this line is ignored.
         * This line is an error: * must be in column 1.

As noted in the previous section, comments can also be located after the OPERAND field of an instruction, separated from it by white space, as in

     LABEL     LDA   100  This is also a comment

Expressions
-----------

The ADDRESS, INDEX and MOD fields of a MIXAL instruction can be expressions, formed by numbers, identifiers and binary operators (+ - * / // :). + and - can also be used as unary operators. Order of evaluation is from left to right: there is no other operator precedence rule, and parentheses cannot be used for grouping. A stand-alone asterisk denotes the current memory location; thus, for instance,

          4+2**

evaluates to 4 plus two times the current memory location. White space is not allowed within expressions.

All symbols appearing within an expression must be defined. Future references are only allowed when appearing stand-alone (or modified by an unary operator) in the ADDRESS part of a MIXAL instruction, e.g.

     * OK: stand alone future reference
              STA  -S1(1:5)
     * ERROR: future reference in expression
              LDX  2-S1
     S1       LD1  2000

Local symbols
-------------

Besides user defined symbols, MIXAL programmers can use the so called local symbols, which are symbols of the form [1-9][HBF]. A local symbol nB refers to the address of the last previous occurrence of nH as a label, while nF refers to the next nH occurrence. Unlike user defined symbols, nH can appear multiple times in the LABEL part of different MIXAL instructions. The following code shows an instance of local symbols' usage:

     * line 1
     1H    LDA  100
     * line 2: 1B refers to address of line 1, 3F refers to address of line 4
           STA  3F,2(1B//2)
     * line 3: redefinition of 1H
     1H    STZ
     * line 4: 1B refers to address of line 3
     3H    JMP  1B

Miscellaneous
-------------

MIXAL allows the introduction of literal constants, which are automatically stored in memory addresses after the end of the program by the assembler. Literal constants are denoted as =exp=, where exp is an expression. For instance, the code

     L         EQU   10
               LDA   =20-L=

causes the assembler to add after the program's end an instruction with contents 10, and to assemble the above code as the instruction LDA a, where a stands for the address in which the value 10 is stored. In other words, the compiled code is equivalent to the following:

     L         EQU  10
               LDA  a
     ...
     a         CON  20-L
               END  start

Getting started
***************

In this chapter, you will find a sample code-compile-run-debug session using the MDK utilities. Familiarity with the MIX mythical computer and its assembly language MIXAL (as described in Knuth's TAOCP) is assumed; for a compact reminder, see MIX and MIXAL overview.

Writing a source file
=====================

MIXAL programs can be written as ASCII files with your editor of choice. Here you have the mandatory hello world as written in the MIXAL assembly language:

     *                                                        (1)
     * hello.mixal: say 'hello world' in MIXAL                (2)
     *                                                        (3)
     * label ins    operand     comment                       (4)
     TERM    EQU    19          the MIX console device number (5)
             ORIG   1000        start address                 (6)
     START   OUT    MSG(TERM)   output data at address MSG    (7)
             HLT                halt execution                (8)
     MSG     ALF    "MIXAL"                                   (9)
             ALF    " HELL"                                   (10)
             ALF    "O WOR"                                   (11)
             ALF    "LD   "                                   (12)
             END    START       end of the program            (13)

MIXAL source files should have the extension .mixal when used with the MDK utilities. As you can see in the above sample, each line in a MIXAL file can be divided into four fields separated by an arbitrary amount of whitespace characters (blanks and or tabs). While Knuth's definition of MIXAL each field must start at a fixed pre-defined column number, the MDK assembler loosens this requirement and lets you format the file as you see fit. The only restrictions retained are for comment lines (like 1-4) which must begin with an asterisk (*) placed at column 1, and for the label field (see below) which, if present, must also start at column 1. The four fields in each non-comment line are:

  - an optional label, which either refers to the current memory address (as START and MSG in lines 7 and 9) or a defined symbol (TERM) (if present, the label must always start at the first column in its line, for the first whitespace in the line maks the beginning of the second field),

  - an operation mnemonic, which can represent either a MIX instruction (OUT and HLT in lines 6 and 7 above), or an assembly pseudoinstruction.

  - an optional operand for the (pseudo)instruction, and

  - an optional free text comment.

Lines 9-12 of the hello.mixal file above also show the second (and last) difference between Knuth's MIXAL definition and ours: the operand of the ALF pseudoinstruction (a word of five characters) must be quoted with using "" (In Knuth's definition, the operand always starts at a fixed column number, and the use of quotation is therefore unnecessary. As mixasm releases this requirement, marking the beginning and end of the ALF operand disambiguates the parser's recognition of this operand when it includes blanks).

The workings of this sample program should be straightforward if you are familiar with MIXAL. See TAOCP vol. 1 for a thorought definition or MIX and MIXAL overview, for a quick tutorial.

Compiling
=========

A simulator of the MIX computer, called mixvm (MIX virtual machine) is included in the MDK tools. It is able to run binary files containing MIX instructions written in their binary representation. You can translate MIXAL source files into this binary form using mixasm, the MIXAL assembler. So, in order to compile the hello.mixal file, you can type the following command at your shell prompt:

     mixasm -g hello RET

If the source file contains no errors, this will produce a binary file called hello.mix which can be loaded and run by the MIX virtual machine. The -g flag tells the assembler to include debug information in the executable file (for a complete description of all the compilation options, see mixasm.) Now, your are ready to run your first MIX program, as described in the following section.

Running the program
===================

MIX is a mythical computer, so it is no use ordering it from your favorite hardware provider. MDK provides a software simulator of the computer, though. It is called mixvm, which stands for MIX virtual machine. Using it, you can run your MIXAL programs, after compiling them with mixasm into binary .mix files. mixvm can be used either in interactive or non-interactive mode. In the second case, mixvm will load your program into memory, execute it (producing any output due to MIXAL OUT instructions present in the program), and exit when it encounters a HLT instruction. In interactive mode, you will enter a shell prompt which allows you issuing commands to the running virtual machine. This commands will permit you loading, running and debugging programs, as well as inspecting the MIX computer state (register contents, memory cells contents and so on).

Non-interactive mode
--------------------

To make mixvm work in non-interactive mode, use the -r flag. Thus, to run our hello.mix program, simply type

     mixvm -r hello RET

at your command prompt, and you will get the following output:

     MIXAL HELLO WORLD

Since our hello world program uses MIX's device number 19 as its output device (see Writing a source file), the output is redirected to the shell's standard output. Had you used any other MIX output devices (disks, drums, line printer, etc.), mixvm would have created a file named after the device used (e.g. disk4.dev) and written its output there.

Sometimes, you will prefer to store the results of your program in MIX registers rather than writing them to a device. In such cases, mixvm's -d flag is your friend: it makes mixvm to dump the contents of its registers and flags after executing the loaded program. For instance, typing the following command at your shell's prompt

     mixvm -d -r hello

you will obtain the following output:

     MIXAL HELLO WORLD
     rA: + 00 00 00 00 00 (0000000000)
     rX: + 00 00 00 00 00 (0000000000)
     rJ: + 00 00 (0000)
     rI1: + 00 00 (0000)     rI2: + 00 00 (0000)
     rI3: + 00 00 (0000)     rI4: + 00 00 (0000)
     rI5: + 00 00 (0000)     rI6: + 00 00 (0000)
     Overflow: F
     Cmp: E

which, in addition to the program's outputs, gives you the contents of the MIX registers and the values of the overflow toggle and comparison flag (admittedly, rather uninteresting in our sample).

As you can see, running programs non-interactively has many limitations. You cannot peek the virtual machine's memory contents, not to mention stepping through your program's instructions or setting breakpoints. Enter interactive mode.

Interactive mode
----------------

To enter the MIX virtual machine interactive mode, simply type

     mixvm RET

at your shell command prompt. This command enters the mixvm command shell. You will be presented the following command prompt:

     MIX >

The virtual machine is initialised and ready to accept your commands. The mixvm command shell uses GNU's readline, so that you have at your disposal command completion (using TAB) and history functionality, as well as other line editing shortcuts common to all utilities using this library (for a complete description of readline's line editing usage, see Command Line Editing (Readline).)

Usually, the first thing you will want to do is loading a compiled MIX program into memory. This is acomplished by the load command, which takes as an argument the name of the .mix file to be loaded. Thus, typing

     MIX > load hello RET
     Program loaded. Start address: 3000
     MIX >

will load hello.mix into the virtual machine's memory and set the program counter to the address of the first instruction. You can obtain the contents of the program counter using the command pc:

     MIX > pc
     Current address: 3000
     MIX >

After loading it, you are ready to run the program, using, as you surely have guessed, the run command:

     MIX > run
     Running ...
     MIXAL HELLO WORLD
     ... done
     MIX >

After running the program, the program counter will point to the address after the one containing the HLT instruction. In our case, asking the value of the program counter after executing the program will give us

     MIX > pc
     Current address: 3002
     MIX >

You can check the contents of a memory cell giving its address as an argument of the command pmem, like this

     MIX > pmem 3001
     3001: + 00 00 00 02 05 (0000000133)
     MIX >

and convince yourself that address 3001 contains the binary representation of the instruction HLT. An address range of the form FROM-TO can also be used as the argument of pmem:

     MIX > pmem 3000-3006
     3000: + 46 58 00 19 37 (0786957541)
     3001: + 00 00 00 02 05 (0000000133)
     3002: + 14 09 27 01 13 (0237350989)
     3003: + 00 08 05 13 13 (0002118477)
     3004: + 16 00 26 16 19 (0268542995)
     3005: + 13 04 00 00 00 (0219152384)
     3006: + 00 00 00 00 00 (0000000000)
     MIX >

In a similar manner, you can look at the contents of the MIX registers and flags. For instance, to ask for the contents of the A register you can type

     MIX > preg A
     rA: + 00 00 00 00 00 (0000000000)
     MIX >

Use the comand help to obtain a list of all available commands, and help COMMAND for help on a specific command, e.g.

     MIX > help run
     run             Run loaded or given MIX code file. Usage: run [FILENAME]
     MIX >

For a complete list of commands available at the MIX propmt, see mixvm. In the following subsection, you will find a quick tour over commands useful for debugging your programs.

Debugging commands
------------------

The interactive mode of mixvm lets you step by step execution of programs as well as breakpoint setting. Use next to step through the program, running its instructions one by one. To run our two-instruction hello.mix sample you can do the following:

     MIX > load hello
     Program loaded. Start address: 3000
     MIX > pc
     Current address: 3000
     MIX > next
     MIXAL HELLO WORLD
     MIX > pc
     Current address: 3001
     MIX > next
     MIX > pc
     Current address: 3002
     MIX > next
     End of program reached at address 3002
     MIX >

You can set a breakpoint at a given address using the command sbpa (set breakpoint at address). When a breakpoint is set, run will stop before executing the instruction at the given address. Typing run again will resume program execution. Coming back to our hello world example, we would have:

     MIX > sbpa 3001
     Breakpoint set at address 3001
     MIX > run
     Running ...
     MIXAL HELLO WORLD
     ... stopped: breakpoint at line 8 (address 3001)
     MIX > run
     Running ...
     ... done
     MIX >

Note that, since we compiled hello.mixal with debug info enabled (the -g flag of mixasm), the virtual machine is able to tell us the line in the source file corresponding to the breakpoint we are setting. As a matter of fact, you can directly set breakpoints at source code lines using the command sbp LINE_NO, e.g.

     MIX > sbp 4
     Breakpoint set at line 7
     MIX >

sbp sets the breakpoint at the first meaningful source code line; thus, in the above example we have requested a breakpoint at a line which does not correspond to a MIX instruction and the breakpoint is set at the first line containing a real instruction after the given one. To unset breakpoints, use cbpa ADDRESS and cbp LINE_NO, or cabp to remove all currently set breakpoints.

MIXAL lets you define symbolic constants, either using the EQU pseudoinstruction or starting an instruction line with a label (which assigns to the label the value of the current memory address). Each MIXAL program has, therefore, an associated symbol table which you can inspect using the psym command. For our hello world sample, you will obtain the following output:

     MIX > psym
     START:  3000
     TERM:  19
     MSG:  3002
     MIX >

For a complete description of all available MIX commands, see mixvm.

mixvm, the MIX computer simulator
*********************************

This chapter describes mixvm, the MIX computer simulator. mixvm is a command line interface programme which simulates the MIX computer (see The MIX computer). It is able to run MIXAL programs (see MIXAL) previously compiled with the MIX assembler (see mixasm). The simulator allows inspection of the MIX computer components (registers, memory cells, comparison flag and overflow toggle), step by step execution of MIX programmes, and breakpoint setting to aid you in debugging your code. For a tutorial description of mixvm usage, see Running the program.

Invoking mixvm
==============

mixvm can be invoked with the following command line options (note, that, following GNU's conventions, we provide a long option name for each available single letter switch):

     mixvm [-vhurd] [--version] [--help] [--usage] [--run] [--dump]
           [FILE[.mix]]

The meaning of these options is as follows:

 -- User Option: -v
 -- User Option: --version
     Prints version and copyleft information and exits.

 -- User Option: -h
 -- User Option: --help
 -- User Option: -u
 -- User Option: --usage
     Prints a summary of available options and exits.

 -- User Option: -r
 -- User Option: --run
     Loads the specified FILE and executes it. After the program execution, mixvm exits. FILE must be the name of a binary .mix program compiled with mixasm. If your program does not produce any output, use the -d flag (see below) to peek at the virtual machine's state after execution.

 -- User Option: -d
 -- User Option: --dump
     This option must be used in conjuction with -r, and tells mixvm to print the value of the virtual machine's registers, comparison flag and overflow toggle after executing the program named FILE. See Non-interactive mode, for sample usage.

When run without the -r flag, mixvm enters its interactive mode, showing you a prompt like this one:

     MIX >

and waiting for your commands (see Commands). If the optional FILE argument is given, the file FILE.mix will be loaded into the virtual machine memory before entering the interactive mode.

Interactive commands
====================

You can enter the interactive mode of the MIX virtual machine by simply invoking mixvm without arguments. You will then presented a shell prompt

     MIX >

which indicates that a new virtual machine has been initialised and is ready to execute your commands. As we have already mentioned, this command prompt offers you command line editing facilities as described in the Readline user's manual (chances are that you are already familiar with these command line editing capabilities, as they are present in many GNU utilities, e.g. the bash shell). As a beginner, your best friend will be the help command, which shows you a summary of all available MIX commands and their usage; its syntax is as follows:

 -- mixvm command: help [command]
 -- mixvm command: ? [command]
     Prints a short description of the given COMMAND and its usage. If COMMAND is omitted, all available commands are described.

You have at your disposal a series of commands that let you load and execute MIX executable file, as well as manipulate MIXAL source files:

 -- file command: load file[.mix]
     This command loads a binary file, FILE.MIX into the virtual machine memory, and positions the program counter at the beginning of the loaded program. This address is indicated in the MIXAL source file as the operand of the END pseudoinstruction. Thus, if your sample.mixal source file contains the line:

               END 3000

     and you compile it with mixasm to produce the binary file sample.mix, you will load it into the virtual machine as follows:

          MIX > load sample
          Program loaded. Start address: 3000
          MIX >

 -- file command: run [file[.mix]]
     When executed without argument, this command initiates or resumes execution of instructions from the current program counter address. Therefore, issuing this command after a successful load, will run the loaded program until either a HLT instruction or a breakpoint is found. If you provide a MIX filename as argument, the given file will be loaded (as with load FILE) and executed. If run is invoked again after program execution completion (i.e., after the HLT instruction has been found in a previous run), the program counter is repositioned and execution starts again from the beginning.

 -- file command: edit file[.mixal]
     The source file FILE.MIXAL is edited using the editor defined in the environment variable MIX_EDITOR. If this variable is not set, the following ones are tried out in order: X_EDITOR, EDITOR and VISUAL.

 -- file command: compile file[.mixal]
     The source file FILE.MIXAL is compiled (with debug information enabled) using mixasm.

Sequential execution of loaded programs can be interrupted using the following debug commands:

 -- debug command: next [ins_number]
     This command causes the virtual machine to fetch and execute  up to INS_NUMBER instructions, beginning from the current program counter position. Execution is interrupted either when the specified number of instructions have been fetched or a breakpoint is found, whatever happens first. If run without arguments, one instruction is executed.

 -- debug command: sbp line_number
     Sets a breakpoint at the specified source file line number. If the line specified corresponds to a command or to a MIXAL pseudoinstruction which does not produce a MIX instruction in the binary file (such as ORIG or EQU) the breakpoint is set at the first source code line giving rise to a MIX instruction after the specified one. Thus, for our sample hello.mixal file:

          *                                                        (1)
          * hello.mixal: say 'hello world' in MIXAL                (2)
          *                                                        (3)
          * label ins    operand     comment                       (4)
          TERM    EQU    19          the MIX console device number (5)
                  ORIG   1000        start address                 (6)
          START   OUT    MSG(TERM)   output data at address MSG    (7)
          ...

     trying to set a breakpoint at line 5, will produce the following result:

          MIX > sbp 5
          Breakpoint set at line 7
          MIX >

     since line 7 is the first one compiled into a MIX instruction (at address 3000). In order to sbp to work, the source file must be compiled using the -g flags, which tells mixasm to include debug information in the binary .mix file.

 -- debug command: spba address
     Sets a breakpoint at the given memory ADDRESS. The argument must be a valid MIX memory address, i.e., it must belong into the range [0-3999]. Note that no check is performed to verify that the specified address is reachable during program execution. No debug information is needed to set a breakpoint by address with sbpa.

 -- debug command: cbp line_no
     Clears a (previously set) breakpoint at the given source file line.

 -- debug command: cbpa address
     Clears a (previously set) breakpoint at the given memory address.

 -- debug command: cabp
     Clears all currently set breakpoints.

 -- debug command: psym [symbol_name]
     MIXAL programs can define symbolic constants, using either the EQU pseudoinstruction or a label at the beginning of a line. Thus, in the program fragment

          VAR     EQU  2168
                  ORIG 4000
          START   LDA  VAR

     the symbol VAR stands for the value 2168, while START is assigned the value 4000. When MIXAL programs are compiled using the -g flag (which tells mixasm to include debug information in the binary .mix file), the symbol table can be consulted from the mixvm command line using psym followed by the name of the symbol whose contents you are interested in. When run without arguments, psym will print all defined symbols and their values.

Inspection and modification of the virtual machine state (memory, registers, overflow toggle and comparison flag contents) is accomplished using the following commands:

 -- state command: pc
     Prints the current value of the program counter, which stores the address of the next instruction to be executed in a non-halted program.

 -- state command: preg [A | X | J | I[1-6]]
 -- state command: pall
 -- state command: sreg A | X | J | I[1-6] value
     preg prints the contents of a given MIX register. For instance, preg A will print the contents of the A-register. When invoked without arguments, all registers shall be printed:

          MIX > preg
          rA: - 00 00 00 00 35 (0000000035)
          rX: + 00 00 00 15 40 (0000001000)
          rJ: + 00 00 (0000)
          rI1: + 00 00 (0000)	rI2: + 00 00 (0000)
          rI3: + 00 00 (0000)	rI4: + 00 00 (0000)
          rI5: + 00 00 (0000)	rI6: + 00 00 (0000)
          MIX >

     As you can see in the above sample, the contents is printed as the sign plus the values of the MIX bytes stored in the register and, between parenthesis, the decimal representation of its module.

     pall prints the contents of all registers plus the comparison flag and overflow toggle.

     Finally, sreg Sets the contents of the given register to VALUE, expressed as a decimal constant. If VALUE exceeds the maximum value storable in the given register, VALUE mod MAXIMU_VALUE is stored, e.g.

          MIX > sreg I1 1000
          MIX > preg I1
          rI1: + 15 40 (1000)
          MIX > sreg I1 1000000
          MIX > preg I1
          rI1: + 09 00 (0576)
          MIX >

 -- state command: pflags
 -- state command: scmp E | G | L
 -- state command: sover F | T
     pflags prints the value of the comparison flag and overflow toggle of the virtual machine, e.g.

          MIX > pflags
          Overflow: F
          Cmp: E
          MIX >

     The values of the overflow toggle are either F (false) or T (true), and, for the comparison flag, E, G, L (equal, greater, lesser). scmp and sover are setters of the comparison flag and overflow toggle values.

 -- state command: pmem from[-to]
 -- state command: smem address value
     pmem prints the contents of memory cells in the address range [FROM-TO]. If the upper limit TO is omitted, only the contents of the memory cell with address FROM is printed, as in

          MIX > pmem 3000
          3000: + 46 58 00 19 37 (0786957541)
          MIX >

     The memory contents is displayed both as the set of five MIX bytes plus sign composing the stored MIX word and, between parenthesis, the decimal representation of the module of the stored value.

     smem sets the content of the memory cell with address ADDRESS to VALUE, expressed as a decimal constant.

Finally, you can use the quit command to exit mixvm.

MIX block devices
=================

The MIX computer comes equipped with a set of block devices for input-output operations (see Input-output operators). mixvm implements these block devices as disk files, with the exception of block device no. 19 (typewriter terminal) which is redirected to standard output. When you request an output operation on any other (output) device, a file named according to the following table will be created in the current directory, and the specified MIX words will be written to the file in binary form (for binary devices) or in ASCII (for char devices). Files corresponding to input block devices should be created and filled beforehand to be used by the MIX virtual machine (for input-output devices this creation can be accomplished by a MIXAL program writing to the device the required data, or, if you prefer, with your favourite editor).

Device    No.   filename   type
Tape   0-7   tape[0-7].dev   bin i/o
Disks   8-15   disk[0-7].dev   bin i/o
Card reader   16   cardrd.dev   char in
Card writer   17   cardwr.dev   char out
Line printer   18   printer.dev   char out
Terminal   19   stdout   char out
Paper tape   20   paper.dev   char out

mixasm, the MIXAL assembler
***************************

MIX programs, as executed by mixvm, are composed of binary instructions loaded into the virtual machine memory as MIX words. Although you could write your MIX programs directly as a series of words in binary format, you have at your disposal a more friendly assembly language, MIXAL (see MIXAL) which is compiled into binary form by mixasm, the MIXAL assembler included in MDK. In this chapter, you will find a complete description of mixasm options.

Invoking mixasm
===============

In its simplest form, mixasm is invoked with a single argument, which is the name of the MIXAL file to be compiled, e.g.

     mixasm hello

will compile either hello or hello.mixal, producing a binary file named hello.mix if no errors are found.

In addition, mixasm can be invoked with the following command line options (note, that, following GNU's conventions, we provide a long option name for each available single letter switch):

     mixasm [-vhulg] [-o OUTPUT_FILE] [--version] [--help]
            [--usage] [--debug] [--output=OUTPUT_FILE] [--list[=LIST_FILE]] file

The meaning of these options is as follows:

 -- User Option: -v
 -- User Option: --version
     Prints version and copyleft information and exits.

 -- User Option: -h
 -- User Option: --help
 -- User Option: -u
 -- User Option: --usage
     Prints a summary of available options and exits.

 -- User Option: -g
 -- User Option: --debug
     Includes debugging information in the compiled file, allowing breakpoint setting at source level and symbol table inspection under mixvm.

 -- User Option: -o output_file
 -- User Option: --output=output_file
     By default, the given source file FILE.MIXAL is compiled into FILE.MIX. You can provide a different name for the output file using this option.

 -- User Option: -l
 -- User Option: --list[=list_file]
     This option causes mixasm to produce, in addion to the .mix file, an ASCII file containing a summary of the compilation results. The file is named after the MIXAL source file, changing its extension to .mls if no argument is provided; otherwise, the listing file is named according to the argument.

Copying
*******

     [file gpl.texi not included]

Reporting Bugs
**************

If you find a bug in MDK (or have questions, comments or suggestions about it), please send electronic mail to the author.

In your report, please include the version number, which you can find by running mixasm --version.  Also include in your message the output that the program produced and the output you expected.