The control unit switches on a different set of signals for each instruction type. Look them up.
What to build
A program that prints the active control signals for each instruction in a list.
Requirements
add: RegWrite.addi: RegWrite, ALUSrc.lw: RegWrite, ALUSrc, MemRead, MemToReg.sw: ALUSrc, MemWrite.beq: Branch.- For each instruction print
name:followed by its signals separated by,, in the order written above.
Input
A line with n, then n instruction names, one per line.
Output
n lines.
Constraints
1 ≤ n ≤ 50; every name is one of the five above, in lower case.
Example
Input:
3
add
lw
sw
Output:
add: RegWrite
lw: RegWrite, ALUSrc, MemRead, MemToReg
sw: ALUSrc, MemWrite
Why it works
Each signal is a switch. A load must read memory and write a register using a constant offset, while a store writes memory but no register, and a branch only decides about the PC. The table captures these needs.
Examples
3 add lw sw
add: RegWrite lw: RegWrite, ALUSrc, MemRead, MemToReg sw: ALUSrc, MemWrite
2 beq addi
beq: Branch addi: RegWrite, ALUSrc
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Keep a dictionary from each instruction to the list of its signals, written in the order of the table.
Hint 2
Join the list with ", " when printing.
Solution
Show one correct solution (try it yourself first)
SIGNALS = {
"add": "RegWrite",
"addi": "RegWrite, ALUSrc",
"lw": "RegWrite, ALUSrc, MemRead, MemToReg",
"sw": "ALUSrc, MemWrite",
"beq": "Branch",
}
n = int(input())
for _ in range(n):
name = input().strip()
print(f"{name}: {SIGNALS[name]}")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step