Lesson 28 +35 XP

Logic & Shift Microoperations and the Arithmetic Logic Shift Unit

Logic & Shift Microoperations and the Arithmetic Logic Shift Unit

The previous lesson built the arithmetic microoperations. A processor also needs logic microoperations (bit-by-bit operations that set, clear, complement or pick out bits) and shift microoperations (move bits left or right). This lesson covers both, with the classic applications (selective set, clear, mask, insert), and then combines everything into one arithmetic logic shift unit (ALU).

1. Logic Microoperations

A logic microoperation treats each bit of a register as a separate binary variable and applies the same Boolean operation to every bit position. For example, with P = 1:

P: quad R1 ← R1 oplus R2

If R1 = 1010 and R2 = 1100, then R1 becomes 0110.

Special symbols avoid confusion with arithmetic: vee for OR, wedge for AND, oplus for XOR, and a bar for complement. The + sign always means arithmetic addition in RTL; it means OR only inside a control function. So

P + Q: quad R1 ← R2 + R3, quad R4 ← R5 vee R6

has an OR in the control condition, an add for R1 and an OR for R4.

1.1 The sixteen logic microoperations

Two variables x and y have 222 = 16 Boolean functions, so there are 16 logic microoperations:

FunctionMicrooperationNameFunctionMicrooperationName
F0 = 0F ← 0clearF8 = (x + y)'F ← overline{A vee B}NOR
F1 = xyF ← A wedge BANDF9 = (x oplus y)'F ← overline{A oplus B}XNOR
F2 = xy'F ← A wedge overline{B}F10 = y'F ← overline{B}complement B
F3 = xF ← Atransfer AF11 = x + y'F ← A vee overline{B}
F4 = x'yF ← overline{A} wedge BF12 = x'F ← overline{A}complement A
F5 = yF ← Btransfer BF13 = x' + yF ← overline{A} vee B
F6 = x oplus yF ← A oplus BXORF14 = (xy)'F ← overline{A wedge B}NAND
F7 = x + yF ← A vee BORF15 = 1F ← all 1sset to all 1s

1.2 Hardware

Most computers implement only four (AND, OR, XOR, complement); the other twelve can be made from them. One stage of the logic circuit has the four gates and a 4 × 1 multiplexer selected by S1 S0:

One stage of the logic circuitOne stage of the logic circuit

S1S0OutputOperation
00E = A wedge BAND
01E = A vee BOR
10E = A oplus BXOR
11E = overline{A}complement

An n-bit logic circuit repeats this stage n times with common select lines.

2. Applications of Logic Microoperations

Logic microoperations change chosen bits of a register A using a second register B as a pattern (a mask).

OperationMicrooperationEffect on A where the B bit is 1Where the B bit is 0
Selective setA ← A vee Bset to 1unchanged
Selective complementA ← A oplus Bcomplementedunchanged
Selective clearA ← A wedge overline{B}cleared to 0unchanged
Mask (keep)A ← A wedge Bunchangedcleared to 0
Clear (compare)A ← A oplus Bresult is 0 in every position where A and B are equal

Insert puts a new value into a group of bits: first mask those bits to 0 (AND with 0s in that group and 1s elsewhere), then OR in the new value.

SolvedExample 1 · All the selective operations on one pair

A = 11011001, B = 10110100. Find A after selective set, selective complement, selective clear and mask.

  1. Selective set, A vee B: 11011001 OR 10110100 = 11111101.
  2. Selective complement, A oplus B: 01101101.
  3. Selective clear, A wedge overline{B}: overline{B} = 01001011, so 11011001 AND 01001011 = 01001001.
  4. Mask, A wedge B: 10010000.
Answer11111101, 01101101, 01001001, 10010000
SolvedExample 2 · Insert a new value into the high half

A = 0110 1010. Replace its leftmost four bits by 1001 without disturbing the rest.

  1. Mask the high half: A ∧ 0000 1111 = 0000 1010.
  2. Insert: (0000 1010) ∨ (1001 0000) = 1001 1010.
Answertwo microoperations: mask with 00001111, then OR with 10010000; A = 10011010
SolvedExample 3 · Choosing the operation from before and after values

An 8-bit register holds A = 01101010. Find B and the operation that makes A = 01111110 (set some bits) and then A = 01000010 (clear some bits).

  1. 01101010 → 01111110: bits 4 and 2 became 1, nothing else changed. Selective set with B = 00010100: A ← A vee B.
  2. 01111110 → 01000010: bits 5, 4, 3 and 2 became 0. Selective clear with B = 00111100: A ← A wedge overline{B}.
AnswerOR with 00010100, then AND with the complement of 00111100

3. Shift Microoperations

Shift microoperations move the bits of a register one position left or right. They differ in what enters the vacated end.

SymbolNameEffect
shl Rlogical shift leftbits move left, 0 enters at the right
shr Rlogical shift rightbits move right, 0 enters at the left
cil Rcircular shift leftthe leftmost bit goes round into the right end
cir Rcircular shift rightthe rightmost bit goes round into the left end
ashl Rarithmetic shift leftas shl (multiply by 2); overflow if the sign changes
ashr Rarithmetic shift rightthe sign bit is copied (divide by 2)

The six shifts applied to R = 10011100The six shifts applied to R = 10011100

3.1 Arithmetic shifts

For a signed (2's complement) number in register R with sign bit Rn-1:

  • ashr keeps Rn-1 unchanged and shifts it into Rn-2. The value is divided by 2 (rounded towards -∞).
  • ashl inserts 0 at R0 and multiplies by 2. Overflow occurs if the sign bit changes, detected by
Vs = Rn-1 oplus Rn-2 quad (before the shift)

Vs = 1 means the result cannot be represented.

SolvedExample 4 · Arithmetic shifts with overflow check

Apply ashr and ashl to R = 10011100 (−100), then ashl to R = 01001010 (74) and R = 11001010 (−54).

  1. ashr 10011100 = 11001110 = −50 ✓ (sign 1 copied).
  2. ashl 10011100: Vs = R7 oplus R6 = 1 oplus 0 = 1, overflow (−200 does not fit in 8 bits). The register gets 00111000.
  3. ashl 01001010: Vs = 0 oplus 1 = 1, overflow (148 > 127).
  4. ashl 11001010: Vs = 1 oplus 1 = 0, no overflow: 10010100 = −108 ✓.
Answer−50; overflow; overflow; −108

3.2 Hardware: combinational shifter

A shift can be done in a shift register (one clock per position) or, faster, by a combinational shifter built from multiplexers. The 4-bit shifter below has one 2 × 1 MUX per output bit.

With select S = 0 each output takes the bit to its left (shift right), with S = 1 the bit to its right (shift left). The serial inputs IR and IL supply the bit that enters: 0 for a logical shift, the outgoing bit for a circular shift.

4-bit combinational shifter4-bit combinational shifter

4. The Arithmetic Logic Shift Unit

Instead of separate registers each with their own circuit, a processor uses one ALU: the source registers drive its inputs, the result goes to a destination register, all within one clock period. One stage of the ALU combines one stage of the arithmetic circuit, one stage of the logic circuit, and a 4 × 1 MUX that chooses between them and the two shifts:

One stage of the arithmetic logic shift unitOne stage of the arithmetic logic shift unit

S3 S2 picks the kind of operation; S1 S0 (and Cin) pick the operation within the arithmetic or logic circuit:

S3S2S1S0CinOperationFunction
00000F = A + Baddition
00001F = A + B + 1add with carry
00010F = A + overline{B}subtract with borrow
00011F = A + overline{B} + 1subtraction
00100F = Atransfer A
00101F = A + 1increment
00110F = A - 1decrement
00111F = Atransfer A
0100xF = A wedge BAND
0101xF = A vee BOR
0110xF = A oplus BXOR
0111xF = overline{A}complement A
10xxxF = shr Ashift right A into F
11xxxF = shl Ashift left A into F

That is 14 distinct operations (8 arithmetic with two repeats of "transfer A", 4 logic, 2 shift).

⚠️ Warning

Some textbook editions list the arithmetic rows in a different order (starting with "transfer A" at 00000). The structure is the same; always state the table you are using in an answer.

SolvedExample 5 · Select inputs for given operations

Give S3 S2 S1 S0 Cin for: (a) R ← A - B, (b) R ← A oplus B, (c) R ← A + 1, (d) shift A left.

  1. (a) subtraction: 0001 1.
  2. (b) XOR: 0110 x.
  3. (c) increment: 0010 1.
  4. (d) shl: 11xx x.
Answer00011, 0110x, 00101, 11xxx

5. Verilog: A 4-Bit ALU

The model follows the stage diagram: a MUX on B and an adder (arithmetic), four gate functions (logic), and an output MUX on S3 S2. The testbench uses A = 1010 and B = 1100, so the logic rows double as the selective operations of section 2:

VERILOG
// 4-bit arithmetic logic shift unit, built stage by stage like the diagram
module alu4 (
    input [3:0] a,
    input [3:0] b,
    input [3:0] s, // S3 S2 S1 S0
    input cin,
    output reg [3:0] f,
    output cout
);
    reg [3:0] y;
    wire [4:0] sum;
    reg [3:0] e;
    always @(*)
        case (s[1:0]) // MUX on the B inputs of the arithmetic circuit
            2'b00: y = b;
            2'b01: y = ~b;
            2'b10: y = 4'b0000;
            default: y = 4'b1111;
        endcase
    assign sum = a + y + cin;
    assign cout = sum[4];
    always @(*)
        case (s[1:0]) // logic circuit
            2'b00: e = a & b;
            2'b01: e = a | b;
            2'b10: e = a ^ b;
            default: e = ~a;
        endcase
    always @(*)
        case (s[3:2]) // output MUX: arithmetic, logic, shr, shl
            2'b00: f = sum[3:0];
            2'b01: f = e;
            2'b10: f = {1'b0, a[3:1]};
            default: f = {a[2:0], 1'b0};
        endcase
endmodule

module tb;
    reg [3:0] a = 4'b1010, b = 4'b1100, s;
    reg cin;
    wire [3:0] f;
    wire cout;
    alu4 u (a, b, s, cin, f, cout);
    task op(input [3:0] sel, input c, input [8*32:1] name);
        begin
            s = sel;
            cin = c;
            #1 $display("S=%b Cin=%b  F=%b  %0s", s, cin, f, name);
        end
    endtask
    initial begin
        $display("A=%b  B=%b", a, b);
        op(4'b0000, 0, "A + B");
        op(4'b0001, 1, "A - B");
        op(4'b0010, 1, "A + 1");
        op(4'b0011, 0, "A - 1");
        op(4'b0100, 0, "A AND B  (mask)");
        op(4'b0101, 0, "A OR B   (selective set)");
        op(4'b0110, 0, "A XOR B  (selective complement)");
        op(4'b0111, 0, "NOT A");
        op(4'b1000, 0, "shr A");
        op(4'b1100, 0, "shl A");
    end
endmodule

A + B = 10 + 12 = 22 shows as 0110 (the carry is lost in 4 bits), A − B = −2 is 1110, and the AND, OR and XOR rows are the mask, selective set and selective complement of A by B:

OUTPUT
A=1010  B=1100
S=0000 Cin=0  F=0110  A + B
S=0001 Cin=1  F=1110  A - B
S=0010 Cin=1  F=1011  A + 1
S=0011 Cin=0  F=1001  A - 1
S=0100 Cin=0  F=1000  A AND B  (mask)
S=0101 Cin=0  F=1110  A OR B   (selective set)
S=0110 Cin=0  F=0110  A XOR B  (selective complement)
S=0111 Cin=0  F=0101  NOT A
S=1000 Cin=0  F=0101  shr A
S=1100 Cin=0  F=0100  shl A
💡 Tip

Key points to remember

  • 16 logic microoperations; hardware usually gives AND, OR, XOR, complement.
  • Selective set OR, selective complement XOR, selective clear A wedge overline{B}, mask AND, insert = mask then OR.
  • Logical shifts insert 0; circular shifts wrap; ashr copies the sign; ashl overflows when Rn-1 oplus Rn-2 = 1.
  • ALU stage: arithmetic stage + logic stage + 4 × 1 MUX (S3 S2); 14 operations.
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. List the 16 logic microoperations. Draw one stage of a logic circuit implementing AND, OR, XOR and complement. (10 Marks)
  • Q2. Explain selective set, selective complement, selective clear, mask and insert with an example for each. (5 Marks)
  • Write: each rule as RTL, and a worked pair such as A = 11011001, B = 10110100.
  • Q3. Explain logical, circular and arithmetic shifts. How is overflow detected in an arithmetic shift left? (5 Marks)
  • Q4. Draw one stage of an arithmetic logic shift unit and give its function table. (10 Marks)
  • Q5. Draw a 4-bit combinational shifter using multiplexers. (5 Marks)