Computer architecture · A level · OCR H446 1.2.4, AQA 7517 4.7.3.5 · about 25 min
AQA's instruction set, compare and branch, masks and shifts, and the bit fields that drive BugBot's motors.
[1 mark]What is the difference between MOV R1, #50 and LDR R1, 50?
[1 mark]What does this program print?
r0 = 0b11001010 print(format(r0 & 0b00001111, '08b')) print(format(r0 | 0b00000001, '08b')) print(format(r0 >> 3, '08b'))
00001010 11001011 00011001
AND keeps the bottom four bits, OR sets bit 0, and a right shift by 3 moves every bit three places down.
[1 mark]R0 holds 13. What does R1 hold after LSL R1, R0, #2?
[1 mark]Which instruction toggles (flips) the lowest four bits of R2, leaving the others alone?
[1 mark]A program does CMP R0, #10 then BLT small. When does it branch?
[1 mark]Why does an if-else in AQA assembly need an unconditional B after the 'if' part?
commands holds four motor control bytes, each a whole number from 0 to 255 laid out as in the table above. For each byte, use bitwise operators (>> and &) to split it into its speed (0 to 63) and its mode, and:
- print a line in the form 161: speed 40 forward: the byte in denary, the speed, then the mode as coast, forward, reverse or brake;
- then act on it: forward drives forward 10 cm at that speed, reverse drives backward 10 cm at that speed, brake calls stop(), and coast does nothing.
Do not convert the byte to a string of bits.
# the two lines every program starts with: the commands, then the robot from bugbot import * connect() commands = [161, 98, 3, 0] modes = ["coast", "forward", "reverse", "brake"]
The hint students can ask for: Draw the byte as eight boxes. Work out which way and how far to shift so only the speed bits are left, and which mask keeps only the bottom two bits. The mode number can then choose a word from the list and decide what the robot does.
from bugbot import *
connect()
commands = [161, 98, 3, 0]
modes = ["coast", "forward", "reverse", "brake"]
for byte in commands:
speed = byte >> 2
mode = modes[byte & 0b11]
print(f"{byte}: speed {speed} {mode}")
if mode == "forward":
forward(speed, distance=10)
elif mode == "reverse":
backward(speed, distance=10)
elif mode == "brake":
stop()
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.