Skip to content

greenturtlefrank/SimpleCalculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Simple Calculator (2022 IP2 Project1)

This is a compiler-based calculator that translates mathematical expressions into Assembly Code for a custom 32-bit CPU.

1. Execution Flow

Generate & Validate Assembly

  1. Run the Compiler: Execute package/calculator_recursion_mergedcalculator_merged.c and pipe a testcase into it to generate input.txt.
    cd package/calculator_recursion_merged
    
    gcc calculator_merged.c -o calculator_merged.exe
    
    Get-Content "../../assembly_parser/testcase/1.txt" | ./calculator_merged.exe
  2. 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

2. File Descriptions (Codebase)

Compiler Components (package/calculator_loop/ & package/calculator_recursion/) (Experiment Only)

  • 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.

Integrated Environment (package/calculator_recursion_merged/)

  • 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)

Reference & Simulation (assembly_parser/)

  • main.c (Assembly Simulator):
    • Role: Acts as the "Hardware" to execute the input.txt generated by my compiler.
    • Evaluation: It calculates the Total Clock Cycles (CPU Time) and register states to determine the final grade.

3. Project Specifications

Supported Operators & Priority

  • Level 1: ( ), ++ (Prefix), -- (Prefix)
  • Level 2: *, /
  • Level 3: +, -
  • Level 4: &, ^, | (& > ^ > |) (Bitwise logic)
  • Level 5: = (Assignment)

CPU Cycle Calculation (Performance Grading)

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

Note: Variable Initialization

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages