Homework 2

Due Wednesday, February 3

In this problem you will extend the Mac-1 instruction set, by adding a MDN instruction, which calculates the median of an array of numbers. The median is the "middle value," defined to be the k-th smallest of n values, where k = ceiling(n/2). The array is NOT assumed to be in sorted order.

The MDN instruction will have no explicit operands. Instead, the ac register is assumed to point to a location in memory which contains the array length n, with the location immediately following this location to contain a pointer to the array itself.

For example, suppose our array consists of 3 elements starting at location 220. We might have our Mac-1 code store 3 and 220 into, say, locations 104 and 105, and then set ac to 104. Then we would have the instruction


(again, with no explicit operands).

The instruction will place the median of the array into ac.

Use the DePaul University MicMac simulator, described on my DePaul Mic-1/Mac-1 simulator Web page. Make sure you understand this package very, very thoroughly before you begin work.

Use an unused op code of your choice for mdn.

Your design must allow the correct execution of any existing Mac-1 machine-language (not just assembly-langauge) program. The files you submit for grading will consist of expanded versions of both one.mal and one.def.

The Reader will have written a shell or Perl script which automatically runs ma, assem and sim on your files. The Reader will use his own (secret) .asm file, which will include a loop containing an MDN instruction. The loop will have three iterations, in which MDN will be tested on three different examples. In this manner, the Reader's .asm file will:

The reason for the last item is that EXTRA CREDIT will be given for the 10 homework teams who submit the fastest implementations of MDN, as measured above.

By the way, this "contest" is in keeping with microcoding in general. In high-level programming, we usually don't care about small increases in speed; usually the clarity of the code is more important. But microcode directly affects the overall speed of the machine itself, so optimal code is of great value.