Skip to content

ALU Stage

ALU is the Arithmetic-Logic Unit. This is where the "real work" for most instructions are done:

  • Arithmetic: add, sub, and, or, etc.
  • Memory: Address calculation for lw and sw.
  • Branch: Register comparison for beq and bne.

Requirements

The ALU stage was previously called the execution stage. The execution stage was split into into the ALU stage and memory stage.

  1. Read input from previous stage (i.e., decode stage).

  2. Calculate result.

  3. The result is given as output to the next stage (i.e., memory stage).

Block Diagram

Basic

ALU Stage

This part of the datapath is rather simple because the main complexity lies in the control. In particular, how do we decide which operation to perform.## Elements

Branch

ALU Branch

When a branch is taken, we will need to compute the target address as $PC' = ($PC + 4) + (immediate × 4). The control signal PCSrc determines whether the branch is taken or not.

We will explain each step in this.

PC+4

immediate×4

PC+4

Complete ALU Stage

Complete ALU

Non-Branch

ALU Non-Branch

Branch

ALU Branch

Elements

ALU

ALU

The ALU is the "brain" of the processor. The block diagram is shown on the right. Again, we can view it conceptually as a function instead. Similar to multiplexer, we have the control line.

  1. Inputs

    • 2 inputs A and B shown by an arrow going in to the ALU.
    • Each input is a 32-bit numbers.
  2. Outputs

    • 2 outputs A op B and (A op B) == 0? shown by an arrow going out of the ALU.
    • A op B is a 32-bit numbers while (A op B) == 0? is a 1-bit value.
  3. Control

    • One control ALUcontrol with a width of 4 bits shown by the control line at the top.
    • This controls which operation is performed by the ALU.

The control can be summarised as follows. The value of ALUcontrol can be thought of as simply a "convention". Certain values are simpler to implement, but at the end of the day, other arrangements can also be implemented.

ALUcontrol Function
0000 and
0001 or
0010 add
0110 sub
0111 slt
1100 nor
Quick Quiz

Notice how there is no equality check available on ALUcontrol. The question is, how do we check if two values are equal? We do have isZero? output, but do we need to do additional operations to get this result?

No, we can simply use the sub operation through the following equality:

Equality Check
1
2
3
   A   = B
=> A-B = B-B
=> A-B = 0

So, to check $rs == $rt, we need to perform the ALU operation $rs - $rt == 0.

This will be set using opcode and funct field. We will discuss more of this on processor control.

Functional Conceptual View of ALU
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
[Word, bool] execute(Word A, Word B, int ALUcontrol) {
  switch(ALUcontrol) {
    case 0:
      return [A & B, A == B];
    case 1:
      return [A | B, A == B];
    case 2:
      return [A + B, A == B];
    case 6:
      return [A - B, A == B];
    case 7:
      return [A < B, A == B];
    case 12:
      return [~(A | B), A == B]; // not ( A or B )
  }
}