Spring 2001 CSCI 255 Lab 8

This lab is scheduled for the week of 19 March - 23 March.

Goals and Methods

This week we'll look at:

  1. Writing correct assembly language programs
  2. Implementing an absolutely perfect C <= operator
  3. Using trap routines

Start off by creating and then connecting to a directory called csci/255/lab8. Copy your lab7ex2.asm program into this directory. Be sure to include the period at the end of the third command!

Getting lab 7 correct

You should have received a copy of a printout of a test run of your Lab 7 program in class this week. If your program passed the nine test cases for the assembler program with no problems, you can go on to the next section. If it didn't pass all nine test cases, fix it. Be sure to run the lc2sim to verify your fixes.

Do not modify lab7ex2.asm after this point in the lab.

Doing a correct <= operation

In the remainder of the lab you are to write, in both pseudo-code and LC-2 assembly language, an absolutely flawless program to compute R3 <= R4 and then print

if R3 <= R4 is 0, or print

if R3 <= R4 is 1.

Of course your program will have some similarities with the first part of lab7ex2.asm, but the differences are so significant that you should start from scratch with your code. Keep in mind that, like in Lab 7, your program is not allowed to modify R3 and R4.

Avoiding overflow

Overflow can only occur in addition when two numbers of the same sign are added. In subtraction, overflow can only occur when the difference is taken of two numbers with different signs.

Undoubtedly your program calculates either R3 - R4 or R4 - R3. What happens if this calculation overflows? For example, suppose R3 is 24000 (x5DC0) and R4 is -25000 (x9E58). Then R3 - R4 should be 49000 (xBF68). However because we're doing 16-bit arithmetic, this xBF68 is interpreted as a negative number (-16536). This means that your program might conclude R3 - R4 <= 0 and, consequently, R3 <= R4. That ain't right.

As previously mentioned, this overflow can only occur if R3 and R4 are of opposite signs. You need to write code so that subtracts only if R3 and R4 are the same sign. If R3 and R4 are of opposite signs, your code must still return the right result; however, it need not perform a subtraction. For example, if R3 is negative and R4 is positive, your program can immediately conclude that R3 <= R4.

Avoiding more overflow

There is one more potential problem. Suppose R4 is equal to -32768, the smallest possible 16-bit twos-complement number. Since the largest possible 16-bit twos-complement number is 32767; you also will have a problem if you try to compute -R4 while calculating R3 - R4. A similar problem occurs when R3 is -32768 if you try to compute R4 - R3.

The solution to this particular dilemma is to compute R3 - R4 as (R3 + 1)+ ~R4 rather than the usual R3 + (~R4 + 1) if both R3 and R4 are negative. (Think about why this doesn't always work if both are positive.)

Now go to the Lab 8 check off sheet and write, in pseudo-code or pseudo-C, a fail-proof algorithm for computing R3 <= R4. You should have several if-then-else statements in your code.

Using a TRAP for I/O

Use the PUTS trap routine, described on page 449 of the textbook, to output the result of your comparison as described earlier in the lab.

Do not try to print the values of R3 and R4. That's too hard -- at least for a couple of weeks.

Typing in the code

Now translate your pseudo-code into LC-assembly and save your assembly code into the file lab8.asm. Start the program with a comment giving the name of the two members of your team. Be sure to assemble and test your code.

Going home

Do not delete your files. Your lab instructor will examine them later. Your instructor can also verify that you have placed your code in the correct place by executing the following command on woodfin.