Loading lessons...
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:
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
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:
| Function | Microoperation | Name | Function | Microoperation | Name |
|---|---|---|---|---|---|
| F0 = 0 | F ← 0 | clear | F8 = (x + y)' | F ← overline{A vee B} | NOR |
| F1 = xy | F ← A wedge B | AND | F9 = (x oplus y)' | F ← overline{A oplus B} | XNOR |
| F2 = xy' | F ← A wedge overline{B} | F10 = y' | F ← overline{B} | complement B | |
| F3 = x | F ← A | transfer A | F11 = x + y' | F ← A vee overline{B} | |
| F4 = x'y | F ← overline{A} wedge B | F12 = x' | F ← overline{A} | complement A | |
| F5 = y | F ← B | transfer B | F13 = x' + y | F ← overline{A} vee B | |
| F6 = x oplus y | F ← A oplus B | XOR | F14 = (xy)' | F ← overline{A wedge B} | NAND |
| F7 = x + y | F ← A vee B | OR | F15 = 1 | F ← all 1s | set 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 circuit
| S1 | S0 | Output | Operation |
|---|---|---|---|
| 0 | 0 | E = A wedge B | AND |
| 0 | 1 | E = A vee B | OR |
| 1 | 0 | E = A oplus B | XOR |
| 1 | 1 | E = 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).
| Operation | Microoperation | Effect on A where the B bit is 1 | Where the B bit is 0 |
|---|---|---|---|
| Selective set | A ← A vee B | set to 1 | unchanged |
| Selective complement | A ← A oplus B | complemented | unchanged |
| Selective clear | A ← A wedge overline{B} | cleared to 0 | unchanged |
| Mask (keep) | A ← A wedge B | unchanged | cleared to 0 |
| Clear (compare) | A ← A oplus B | result 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.
A = 11011001, B = 10110100. Find A after selective set, selective complement, selective clear and mask.
- Selective set, A vee B: 11011001 OR 10110100 = 11111101.
- Selective complement, A oplus B: 01101101.
- Selective clear, A wedge overline{B}: overline{B} = 01001011, so 11011001 AND 01001011 = 01001001.
- Mask, A wedge B: 10010000.
A = 0110 1010. Replace its leftmost four bits by 1001 without disturbing the rest.
- Mask the high half: A ∧ 0000 1111 = 0000 1010.
- Insert: (0000 1010) ∨ (1001 0000) = 1001 1010.
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).
- 01101010 → 01111110: bits 4 and 2 became 1, nothing else changed. Selective set with B = 00010100: A ← A vee B.
- 01111110 → 01000010: bits 5, 4, 3 and 2 became 0. Selective clear with B = 00111100: A ← A wedge overline{B}.
3. Shift Microoperations
Shift microoperations move the bits of a register one position left or right. They differ in what enters the vacated end.
| Symbol | Name | Effect |
|---|---|---|
| shl R | logical shift left | bits move left, 0 enters at the right |
| shr R | logical shift right | bits move right, 0 enters at the left |
| cil R | circular shift left | the leftmost bit goes round into the right end |
| cir R | circular shift right | the rightmost bit goes round into the left end |
| ashl R | arithmetic shift left | as shl (multiply by 2); overflow if the sign changes |
| ashr R | arithmetic shift right | the sign bit is copied (divide by 2) |
The 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 = 1 means the result cannot be represented.
Apply ashr and ashl to R = 10011100 (−100), then ashl to R = 01001010 (74) and R = 11001010 (−54).
- ashr 10011100 = 11001110 = −50 ✓ (sign 1 copied).
- ashl 10011100: Vs = R7 oplus R6 = 1 oplus 0 = 1, overflow (−200 does not fit in 8 bits). The register gets 00111000.
- ashl 01001010: Vs = 0 oplus 1 = 1, overflow (148 > 127).
- ashl 11001010: Vs = 1 oplus 1 = 0, no overflow: 10010100 = −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 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 unit
S3 S2 picks the kind of operation; S1 S0 (and Cin) pick the operation within the arithmetic or logic circuit:
| S3 | S2 | S1 | S0 | Cin | Operation | Function |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | F = A + B | addition |
| 0 | 0 | 0 | 0 | 1 | F = A + B + 1 | add with carry |
| 0 | 0 | 0 | 1 | 0 | F = A + overline{B} | subtract with borrow |
| 0 | 0 | 0 | 1 | 1 | F = A + overline{B} + 1 | subtraction |
| 0 | 0 | 1 | 0 | 0 | F = A | transfer A |
| 0 | 0 | 1 | 0 | 1 | F = A + 1 | increment |
| 0 | 0 | 1 | 1 | 0 | F = A - 1 | decrement |
| 0 | 0 | 1 | 1 | 1 | F = A | transfer A |
| 0 | 1 | 0 | 0 | x | F = A wedge B | AND |
| 0 | 1 | 0 | 1 | x | F = A vee B | OR |
| 0 | 1 | 1 | 0 | x | F = A oplus B | XOR |
| 0 | 1 | 1 | 1 | x | F = overline{A} | complement A |
| 1 | 0 | x | x | x | F = shr A | shift right A into F |
| 1 | 1 | x | x | x | F = shl A | shift left A into F |
That is 14 distinct operations (8 arithmetic with two repeats of "transfer A", 4 logic, 2 shift).
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.
Give S3 S2 S1 S0 Cin for: (a) R ← A - B, (b) R ← A oplus B, (c) R ← A + 1, (d) shift A left.
- (a) subtraction: 0001 1.
- (b) XOR: 0110 x.
- (c) increment: 0010 1.
- (d) shl: 11xx x.
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:
// 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
endmoduleA + 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:
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 AKey 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.
📝 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)