1. Goals for this week:

  1. ALU condition codes (flags) Lab 3.

2. Starting Point Code

Start by creating a week04 in your cs31/weeklylabs subdirectory and copying over some files:

$ cd ~/cs31/weeklylabs
$ mkdir week04
$ cd week04
$ pwd
/home/you/cs31/weeklylabs/week04
$ cp ~adanner/public/cs31/week04/* ./
$ ls
Makefile conditions.c

3. The Lab 3 ALU Flags

Next, let’s talk about the flags (the condition codes) part of the ALU lab. We will do this in the context of examining some example operations, and considering how the flags should be set

Let’s look at the conditions.c program:

$ vim conditions.c

This program includes some example arithmetic and bit-wise operations, operations that your ALU will implement. We will use it to reason about the ALU flags part of Lab 3.

Recall the flags (or condition codes) are used encode state about the result of an ALU operation. For the ALU you are building in Logisim, the flags are defined as the following:

  • EF: Is set (1), if the two operands are equal. Otherwise, it is clear (0).

  • ZF: Is set, if the computed result is zero.

  • SF: Is set, if the computed result’s most significant bit is set (i.e., it’s negative if interpreted as signed).

  • CF: Is set, if the computed result caused an overflow, when interpreted as an unsigned operation.

  • OF: Is set, if the computed result caused an overflow, when interpreted as a signed operation.

Let’s compile conditions.c and run the program and look at some of the example operations and think about what the flag settings should be for these examples.

$ make
$ ./conditions

ADD
---
unsigned 2147483647 + 4 is     2147483651 (0x80000003)
  signed 2147483647 + 4 is    -2147483645 (0x80000003)

SUB
---
unsigned 3 - 5 is     4294967294 (0xfffffffe)
  signed 3 - 5 is             -2 (0xfffffffe)
...

The first couple examples are ADD and SUB operations. Think about the value of each of the 5 flags for each of these example operations. In other words, which flags should be set (1) and which should be clear (0) as the result of adding 2147483647 + 4? And which should be set and which should be clear as the result of 3 - 5?

Next, let’s consider the OR and AND examples, and how the flags should be set for these examples.

3.1. Logisim Tips

  • You can often change the orientation of circuits in the circuit menu and sometimes some pin locations for easier wiring.

  • If connections seem to not be working properly check if a wire is shorting pins by being connected along an edge of a component.

  • Wires can occasionally pass underneath components and create invisible connections. Gently wiggle a component to check for this if you experience problems.

4. Handy References