Computer architecture · A level · OCR H446 1.1.1, AQA 7517 4.7.3.1, Eduqas A500QS 2.1 · about 20 min
The ALU, control unit and clock, general-purpose and dedicated registers, and the status flags an 8-bit ALU sets.
[1 mark]Which register holds the instruction currently being decoded and executed?
[1 mark]What does the control unit do?
[1 mark]An 8-bit ALU adds 200 and 100. Which flag is set?
[1 mark]What does this program print?
def flags(a, b):
r = (a + b) & 255
return r, r >> 7, int(r == 0)
print(flags(100, 50))
print(flags(128, 128))(150, 1, 0) (0, 0, 1)
150 has its top bit set, so N is 1; 128 + 128 wraps round to 0, so Z is 1.
[1 mark]What is the difference between a general-purpose register and a dedicated register?
Write alu_add(a, b) yourself for an 8-bit ALU. Its inputs a and b are whole numbers from 0 to 255 (bytes). It works out the 8-bit result (0 to 255, anything that does not fit is lost) and the four flags N, Z, C and V, each 0 or 1, exactly as the table above defines them.
For each pair in pairs, print one line in the form 200 + 100 = 44 N=0 Z=0 C=1 V=0: the two inputs, the 8-bit result, then the flags in the order N, Z, C, V. The robot does not move.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
pairs = [(20, 30), (100, 50), (200, 100), (128, 128), (255, 1)]
def alu_add(a, b):
return a + bThe hint students can ask for: Work out the full sum first: carry is whether it went past what 8 bits can hold, and the stored result is only the part that fits. For overflow, read both inputs and the result as two's complement and ask whether two numbers of the same sign gave a result of the other sign.
from bugbot import *
connect()
pairs = [(20, 30), (100, 50), (200, 100), (128, 128), (255, 1)]
def signed(byte):
return byte - 256 if byte >= 128 else byte
def alu_add(a, b):
total = a + b
result = total & 255
n = result >> 7
z = 1 if result == 0 else 0
c = 1 if total > 255 else 0
v = 1 if (signed(a) < 0) == (signed(b) < 0) and (signed(result) < 0) != (signed(a) < 0) else 0
return result, n, z, c, v
for a, b in pairs:
result, n, z, c, v = alu_add(a, b)
print(f"{a} + {b} = {result} N={n} Z={z} C={c} V={v}")
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.