' Toy CPU Assembler/Simulator — step-by-step execution
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL r0 AS LONG, r1 AS LONG, r2 AS LONG, r3 AS LONG
    LOCAL r4 AS LONG, r5 AS LONG, r6 AS LONG, r7 AS LONG
    LOCAL pc AS LONG, running AS LONG
    LOCAL flags AS LONG
    LOCAL inst AS LONG, arg0 AS LONG, arg1 AS LONG

    PRINT "Toy CPU Assembler/Simulator"
    PRINT "=========================="
    PRINT ""

    PRINT "Program: (R0 = 10 + 20 * 3)"

    r0 = 10: r1 = 20: r2 = 3
    PRINT "  Initial: R0=10, R1=20, R2=3"

    ' MUL R1, R2
    r1 = r1 * r2
    PRINT "  MUL R1, R2  -> R1 = "; r1

    ' ADD R0, R1
    r0 = r0 + r1
    PRINT "  ADD R0, R1  -> R0 = 10 + "; r1; " = "; r0

    ' MOV R3, R0
    r3 = r0
    PRINT "  MOV R3, R0  -> R3 = "; r3

    ' CMP R0, 70
    IF r0 = 70 THEN
        flags = 0
        PRINT "  CMP R0, 70 -> EQUAL"
    ELSE
        flags = 1
        PRINT "  CMP R0, 70 -> NOT EQUAL"
    END IF

    ' JE (jump if equal)
    IF flags = 0 THEN
        r4 = 0
        PRINT "  JE taken -> R4 = 0"
    ELSE
        r4 = 1
        PRINT "  JE not taken -> R4 = 1"
    END IF

    PRINT ""
    PRINT "Final registers:"
    PRINT "  R0 = "; r0; " (expected: 70)"
    PRINT "  R1 = "; r1; " (expected: 60)"
    PRINT "  R2 = "; r2; " (expected: 3)"
    PRINT "  R3 = "; r3; " (expected: 70)"
    PRINT "  R4 = "; r4; " (expected: 0)"
    PRINT "  R5 = "; r5; " (expected: 0)"
    PRINT "  R6 = "; r6; " (expected: 0)"
    PRINT "  R7 = "; r7; " (expected: 0)"

    PRINT ""
    PRINT "Instruction set:"
    PRINT "  MOV Rd, Rs  - Copy Rs to Rd"
    PRINT "  ADD Rd, Rs  - Rd = Rd + Rs"
    PRINT "  MUL Rd, Rs  - Rd = Rd * Rs"
    PRINT "  CMP Rd, val - Compare Rd with value"
    PRINT "  JE label    - Jump if equal"
    PRINT "  JMP label   - Jump always"
    PRINT "  HLT         - Halt"

    PRINT ""
    PRINT "Simulation complete."

    FUNCTION = 0
END FUNCTION
