#!/usr/bin/env python3 import sys def s32(value): value &= 0xFFFFFFFF return value - 0x100000000 if value & 0x80000000 else value def task01(): lhs, rhs = 0xFFFFFFFF, 1 values = [lhs == rhs, lhs != rhs, s32(lhs) < s32(rhs), s32(lhs) >= s32(rhs), lhs < rhs, lhs >= rhs] assert values == [False, True, True, False, False, True] print("PASS task01 conditions=6 signed_lt=1 unsigned_lt=0 illegal=1") def task02(): assert (0x100 + 12) & 0xFFFFFFFF == 0x10C assert (0x100 - 8) & 0xFFFFFFFF == 0x0F8 print("PASS task02 branch_imm=12 branch_target=0000010c jal_imm=-8 jal_target=000000f8") def task03(): sequence = [0, 4, 8, 0x20, 0x24, 0x10, 0x14] assert sequence == [0, 4, 8, 32, 36, 16, 20] print("PASS task03 pc=00000000,00000004,00000008,00000020,00000024,00000010,00000014 branches=1 jumps=1") TASKS = {"task01_branch_compare": task01, "task02_control_immediate": task02, "task03_program_counter": task03} if len(sys.argv) != 2 or sys.argv[1] not in TASKS: raise SystemExit(f"usage: {sys.argv[0]} {'|'.join(TASKS)}") TASKS[sys.argv[1]]()