# Assignment 6 for CSCI 255 / ECE 109

## Due date

This assignment must be submitted as a single `.asm` file for Assignment 6 of the CSCI 255/ECE 109 section on UNCA moodle by 5:00 PM on Monday, 27 April.

This is one of those "real world" problems. Beware.

Every day gazillions of IP packets are transmitted over the Internet or perhaps merely between HVAC units. Each of these packets begins with an IP header field documented back in September 1981 in RFC 791. Although this RFC is a historical document worth reading, the only part you really need to read for this assignment are those related to the Internet header format; in particular, the layout of the header (p. 11) and the descriptions of the length field (p. 11) and the header checksum (p. 14).

Your task is to write an LC-3 function that receives the address of an IP header in `R0` and then computes the IP header checksum as defined in the RFC, which states "The checksum field is the 16 bit one's complement of the one's complement sum of all 16 bit words in the header. For purposes of computing the checksum, the value of the checksum field is zero." The IP checksum is then returned in `R1`. (Your program is allowed to modify any other registers and should not follow the LC-3 ABI of Chapter 14.)

### Computing the one's complement sum

You can Google for programs that compute the IP checksum, but most results will be C programs using 32-bit arithmetic. These won't be useful on a 16-bit computer like the LC-3. You can find some useful information about one's complement addition with 16-bit arithmetic from Dr. Math of the Math Forum at Drexel. However, Dr. Math is assuming that you have access to the carry-bit of the addition, which unfortunately is not the case on the LC-3.

### Finding the carry-bit on the LC-3

Let's assume you are adding two 16-bit numbers, x and y, on the LC-3. We want to find out if there there is a carry from the most significant bit in this addition. If both x and y are zero or positive, there will not be a carry. If both x and y are negative, there will be a carry. These are the easy cases.

But suppose x and y have different sums. In this case, you must compute x + y. If x + y is negative, then there was not a carry. Otherwise, there was a carry. Think about it.

### Computing the one's complement of the one's complement sum

That one is easy. Just use the `NOT` instruction.

### Little vs Big Endian

We are going to assume that the packet is stored in little endian order in full 16-bit words; because (1) that's how it's done on the Internet and (2) it's easier to program. This means that the first sixteen bits of the IP header will be stored in the first word of your input. (That is, it can be loaded with the `LDR  Rx,R0,#0` instruction.) Furthermore, the `IHL` (Internet Header Length) of this packet is stored in bits four to seven of this word. Since the `IHL` is expressed as 32-bit words, you must multiply it by two to determine the length of the input packet in 16-bit words.

## Suggest way to solve

Start by writing a program that just computes the usual two's complement sum. This should take less than twenty lines of LC-3 instructions. (But less over thirty when you add the appropriate comments.) When you run the test program, you should see results that are close, but not equal, to the expected answers.

Now add the code to add the numbers in one's complement. My complete solution is about thirty lines of LC-3 code.

## Test program

You can use the LC-3 program TestAssign6.asm to test your code.