Files
2026-07-21 17:19:51 +02:00

42 lines
1.3 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
def task01():
pc = 0x100
assert pc + 24 == 0x118 and pc + 4 == 0x104
assert ((0x205 - 4) & ~1) == 0x200
print("PASS task01 jal_target=00000118 link=00000104 jalr_target=00000200")
def abi_class(index):
if index == 0: return "zero"
if index == 1: return "ra"
if index == 2: return "sp"
if 10 <= index <= 17: return "arg"
if index in (8, 9) or 18 <= index <= 27: return "saved"
if 5 <= index <= 7 or 28 <= index <= 31: return "temp"
return "system"
def task02():
classes = [abi_class(index) for index in range(32)]
assert classes.count("arg") == 8 and classes.count("saved") == 12
assert classes.count("temp") == 7 and sum(classes.count(x) for x in ("zero", "ra", "sp", "system")) == 5
print("PASS task02 args=8 saved=12 temporaries=7 fixed=5")
def task03():
stack = []
stack.extend([0x104, 0x204])
returns = [stack.pop(), stack.pop()]
underflow = not stack
assert returns == [0x204, 0x104] and underflow
print("PASS task03 returns=00000204,00000104 underflow=1 max_depth=2")
TASKS = {"task01_link_address": task01, "task02_abi_registers": task02, "task03_call_stack": 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]]()