import stdio
import t, c


# ============================================================
# c.Asm 内联汇编测试
# ============================================================

def asm_test() -> int:
    # 1. 简单无操作数汇编（nop + clobber）
    c.Asm("nop", op=[t.ASM_DESCR.CLOBBER_MEMORY])
    stdio.printf("asm: nop ok\n")

    # 2. 仅汇编文本无 clobber
    c.Asm("nop")
    stdio.printf("asm: nop2 ok\n")

    # 3. 多 clobber
    c.Asm("nop", op=[t.ASM_DESCR.CLOBBER_MEMORY, t.ASM_DESCR.CLOBBER_CC])
    stdio.printf("asm: multi-clobber ok\n")

    # 4. 带输入操作数的汇编（读取 CPU ID）
    # 使用 f-string 内联 c.AsmInp
    # mov eax, 输入值; nop（保持简单，不使用会产生异常的指令）
    val: int = 42
    c.Asm(f"""mov eax, {c.AsmInp(val, t.ASM_DESCR.REG_ANY)}
nop""", op=[t.ASM_DESCR.CLOBBER_RAX])
    stdio.printf("asm: input ok val=%d\n", val)

    # 5. 带输出操作数的汇编
    # c.AsmOut 获取结果
    result: int = 0
    c.Asm(f"""mov {c.AsmOut(result, t.ASM_DESCR.OUTPUT_REG)}, 123
nop""", op=[t.ASM_DESCR.CLOBBER_RAX])
    stdio.printf("asm: output result=%d\n", result)

    # 6. 带输入和输出操作数
    # result2 = val + 1
    result2: int = 0
    input_val: int = 10
    c.Asm(f"""mov eax, {c.AsmInp(input_val, t.ASM_DESCR.REG_ANY)}
add eax, 1
mov {c.AsmOut(result2, t.ASM_DESCR.OUTPUT_REG)}, eax
nop""", op=[t.ASM_DESCR.CLOBBER_RAX])
    stdio.printf("asm: in+out result2=%d (expect 11)\n", result2)

    return 0
