Running the MIXAL Insertion Sort

With the information gained in last posts investigations, I now know how to turn the smaple code of the insertion sort out of TAOCP into runnable code.

The key insight I had was that the Accumulator was operating on the whole value it would fetch or store, and the I# registers were just used for counters. Thus, the buffer needed to be of word length elements. For MIX that means 5 characters long.

Since the logic works assuming element N+1 is the start of the data, I pad the first field with blanks.

*                                                       
* insertion.mixal: insertion sort
*                                                        
* label ins    operand     comment                       
TERM    EQU    19          the MIX console device number
N       EQU    26          Number of elements in the array 
INPUT   ALF    "     "
        ALF    "ZZZZZ"
        ALF    "YYYYY"
        ALF    "XXXXX"
        ALF    "WWWWW"
        ALF    "VVVVV"
        ALF    "UUUUU"
        ALF    "TTTTT"
        ALF    "SSSSS"     
        ALF    "RRRRR"
        ALF    "QQQQQ"
	ALF    "PPPPP"
	ALF    "OOOOO"
        ALF    "NNNNN"
        ALF    "MMMMM"
        ALF    "LLLLL"
        ALF    "KKKKK"
        ALF    "JJJJJ"
        ALF    "IIIII"
        ALF    "HHHHH"
        ALF    "GGGGG"
        ALF    "FFFFF"
        ALF    "EEEEE"
        ALF    "DDDDD"
        ALF    "CCCCC"
        ALF    "BBBBB"
        ALF    "AAAAA"
        ORIG   3000        start address                 
START   OUT    INPUT(TERM)
        OUT    INPUT+14(TERM)
        ENT1   2-N
2H      LDA    INPUT+N,1
        ENT2   N-1,1
3H      CMPA   INPUT,2
        JGE    5F
4H      LDX    INPUT,2
        STX    INPUT+1,2
        DEC2   1
        J2P    3B
5H      STA    INPUT+1,2
        INC1   1
        J1NP   2B
        OUT    INPUT(TERM)
        OUT    INPUT+14(TERM)
        HLT
        END    START       end of the program

This produces the following output:

$ mixasm insertion.mixal
[ayoung@ayoung-home mix]$ mixvm insertion.mix
Program loaded. Start address: 3000
MIX> run
Running ...
     ZZZZZYYYYYXXXXXWWWWWVVVVVUUUUUTTTTTSSSSSRRRRRQQQQQPPPPPOOOOONNNNN
MMMMMLLLLLKKKKKJJJJJIIIIIHHHHHGGGGGFFFFFEEEEEDDDDDCCCCCBBBBBAAAAA     
     AAAAABBBBBCCCCCDDDDDEEEEEFFFFFGGGGGHHHHHIIIIIJJJJJKKKKKLLLLLMMMMM
NNNNNOOOOOPPPPPQQQQQRRRRRSSSSSTTTTTUUUUUVVVVVWWWWWXXXXXYYYYYZZZZZ     
... done
Elapsed time: 3115 /Total program time: 3115 (Total uptime: 3115)
MIX>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.