This is a compiler-based calculator that translates mathematical expressions into Assembly Code for a custom 32-bit CPU.
- Run the Compiler: Execute
package/calculator_recursion_mergedcalculator_merged.cand pipe a testcase into it to generateinput.txt.cd package/calculator_recursion_merged gcc calculator_merged.c -o calculator_merged.exe Get-Content "../../assembly_parser/testcase/1.txt" | ./calculator_merged.exe
- Verify Results: Use the assembly simulator to read the generated
input.txt. It will output the final register states and Total Clock Cycles (CPU Time).gcc ../../assembly_parser/main.c -o ../../assembly_parser/main.exe & "../../assembly_parser/main.exe" input.txt
main.c: The entry point. Manages the overall compilation flow.lex.c / .h: Lexical Analyzer. Converts raw input strings into structured tokens (Integers, IDs, Operators).parser.c / .h: Syntax Analyzer. Implements recursive descent parsing to build the syntax tree based on operator precedence.codeGen.c / .h: Prefix-order Code Emitter. Performs prefix traversal on the syntax tree to calculate register indices and immediately emit assembly instructions based on node types.
calculator_merged.c(Unified Source): A simulated merge that coordinates the Lexer, Parser, and CodeGen logic as a single entry point to simplify compilation.correct.c(Reference Implementation)
main.c(Assembly Simulator):- Role: Acts as the "Hardware" to execute the
input.txtgenerated by my compiler. - Evaluation: It calculates the Total Clock Cycles (CPU Time) and register states to determine the final grade.
- Role: Acts as the "Hardware" to execute the
- Level 1:
( ),++(Prefix),--(Prefix) - Level 2:
*,/ - Level 3:
+,- - Level 4:
&,^,|(&>^>|) (Bitwise logic) - Level 5:
=(Assignment)
The total score depends on the efficiency of your generated code.
| Instruction Type | Example | Cycle Cost |
|---|---|---|
| Move (Register/Immediate) | MOV r0 10, MOV r0 r1 |
10 |
| Memory Access (Load/Store) | MOV [0] r0, MOV r0 [4] |
200 |
| Basic Arithmetic | ADD, SUB |
10 |
| Bitwise Logic | AND, OR, XOR |
10 |
| Multiplication | MUL |
30 |
| Division | DIV |
50 |
| System Termination | EXIT |
20 |
By default, the variables x, y, and z are initialized to 0 by the simulator. However, The compiler allows for custom initialization if needed in future hardware specifications.