CS-343 Assignment 8

Due November 26


Your assignment is to add two new instructions, sub and subcc, to the ARC Simulator developed in class. It will be sufficient for you to write the code for this homework on a piece of paper and bring it to class on the due date, but you will receive extra credit if you submit a working Java modification of file ARC_Simulator.java (see below) as an email attachment instead. If you prefer to reimplement the entire simulation in C++ instead of modifying the Java version, that's okay too.

Code Available

The following files are available for you to download to work with for this assignment:

Documentation for our ARC simulator is available below:

If you are not able to run the simulator, here is a complete copy of the output generated when it was run using sample.bin as the input file:

Assignment Description

Your assignment is to modify ARC_Simulator.java so that it executes two new ARC machine instructions, sub and subcc. These instructions are recognized by the textbook's assembler and simulator, but are not described in the textbook itself. And they are not currently recognized by our simulator.

The procedure to follow is as follows: First, download the .jar file and be sure you can run it using the command, "java -jar arc_simulator.jar". You may need to install the Java Runtime Environment (Java VM) from Sun Microsystems if you don't already have Java installed on your computer. If you are going to try to change the simulator, you should install the Java Software Development Kit (J2SE SDK) instead of just the Java VM. Both are available from Sun's Java Website -- look under "Popular Downloads."

Once you are able to run the simulator, give it a program to simulate, for example sample.bin listed above. See the simulator documentation for class ARC_Simulator for instructions on how to simulate a program. Study the output of the simulator to make sure you understand it and can relate each line of output to the program being simulated.

The simulator generates quite a bit of output, so you might want to capture it in a file, which you can examine with a text editor. You can do that by putting "> simulator.out" at the end of the java command line. (Of course, you can use any file name you wish instead of simulator.out.)

Now modify sample.asm so that it includes both a sub and a subcc instruction. Modify the addcc instruction just before the branch to do a subtraction instead of addition of a negative one. You can put any sub instruction just before the halt instruction. When you run the simulation now, the last part of the output should look like this:

             PC: 000324  IR: 80200000  Unimplemented Arithmetic op3: 004
    Register  32 changes to  00000328
    Memory at 000328 returns 91D02000
    Register  37 changes to  91D02000
             PC: 000328  IR: 91D02000  halt

Execution summary for file sample.bin:
  Instructions executed:  10
  Invalid op codes:       2
  Nops:                   0
  Clock cycles:           29

There should be another "Unimplemented" message earlier in the output, for the subcc instruction. Note that the value (004) is displayed in octal, which makes sense given that the op3 field is six bits wide and thus fits nicely in two octal digits.

Now you're ready to start working on the assignment! Study the documentation for the various classes to see what methods are available in each one, and then study the code given to you for ARC_Simulator.java. From the "Unimplemented" messages printed by the current version of the simulator you can deduce what the op3 values for subcc and sub are, and your job is to find where to put the code that will handle these two new instructions.

Note 1: In C, C++, and Java numeric constants that start with a zero (0) are octal numbers. So, for example, the op3 value for orcc instructions is 010 010 in binary, but appears as 022 in the code, which is the octal equivalent.

Note 2: Be sure you use the simulated ALU to do the subtraction! You have to figure out how to do it using the ALU function codes available.

If you are actually going to test that your code works, the easiest way to proceed as follows: extract all the class file from the jar file, using the command, "jar -xvf arc_simulator.jar". Now edit ARC_Simulator.java and compile it, which will overwrite the orginal copy of ARC_Simulator.class. Test your program with the command "java ARC_Simulator sample.bin" and verify that the new subtract instructions operate correctly.


The solution is to add two cases to the switch on op3 in doArithmetic(), one for subcc and another for sub. Both work by negating the second operand and adding the result with the first operand. The only difference between the two op codes is whether the ALU op code is ADDCC or ADD.

To simulate the ARC CPU faithfully, negating the second operand has to be done in two cycles through the ALU, a NOR cycle to compute the one's complement, and an INC cycle to add one to that result -- the familiar "flip the bits and add 1" algorithm for taking the two's complement of a number. NORing the value with zeros accomplishes the same thing as XORing with ones; examine the truth tables for these functions to be sure you understand the principle.

Another important concept is that none of registers 1-31 may be modified during the negation of the second operand, not even the rd register. Consider an instruction like, subcc %r3, %r2, %r3 where rs1 and rd are the same register. You can't use r2 during the negation because the machine language instruction isn't supposed to modify any register but rd. And you can't use r3 for the negation, because then the final ADD would have the wrong value for the first operand. The only correct solution is to use one of the temp registers (33-36) for holding the negated value of the second operand, as shown in the code given for the solution below: