This project provides a simple simulator and assembler for a custom assembly language, allowing to understand and experiment with the fundamentals of computer organization.
COURSE OVERVIEW
Course | CSE112 |
---|---|
Course Title | Computer Organisation |
Instructor | Tammam Tillo |
PROJECT MEMBERS
Name | Student ID |
---|---|
Palak Bhardwaj | 2022344 |
Suhani Kalyani | 2022511 |
Vanshika Pal | 2022560 |
Nandini Jain | 2022316 |
- CO-project -> CO_A_P1 -> Simulator -> Simple-Assembler -> bonus_ssembler.py
- CO-project -> CO_A_P1 -> Simulator -> SimpleSimulator -> bonus_simulator.py
In the terminal, go to the automated testing directory and enter `./run``. A complete report sheet will be generated in the terminal
-
Label type instruction
: only 1 label declaration per line i.e., ":"occurs only once. -
Mov R1 FLAGS
is taken to be a valid command since FLAGS doesn't represent a variable but instead represents a valid register name , where -
MOV can be used with two types of instructions, type1- mov register value , mov reg1 reg2 , hence we have taken to be a valid name
-
Use of FLAG register:
FLAGS
is a valid register namemov reg1 FLAGS
is the only valid instruction involving FLAG register.
automatedTesting
: This folder contains
the sample test cases provided by the evaluator: tests/ the grading script: src the run file: run
Simple-Assembler
: This folder contains
Python program which gives stdin and stdout and is used for automated testing:- main.py Python program that reads from read.txt and writes output in write.txt:- readable.py Python program that contains basic decimal-binary conversion functions which are used at many instances in the assembler:- CONVERTME.py
SimpleSimulator
: This folder contains
Python program which takes the binary output from Simple-Assembler/main.py and contains the main programme for execution of each instruction in class ee (execution engine) and the flow of simulator execution and gives the required output in stdout:- main.py MEM.py contains the class for program memory and all the memory functions required RF.py contains the class for register data and all the register accessing functions required PC.py contains the class for the program counter and all the program counter operations required opcodes.py contains all the dictionaries for opcodes and registers and their binary address equivalents Python program that contains basic decimal-binary conversion functions which are used in many instances in the simulator:- CONVERTME.py
This repository contains a simple assembly language compiler that translates assembly code into binary machine code. The compiler supports instructions and handles labels, variables, and immediate values.
The compiler operates under the following assumptions:
-
Label Type Instruction: Only one label declaration per line is allowed. The colon (":") occurs only once in a line.
-
MOV Instruction: The
MOV
instruction is versatile and can be used with two types of operands:MOV register value
: Valid when the destination is a register.MOV reg1 reg2
: Valid when both operands are registers.
The compiler uses the following opcodes and register mappings:
op_code = {
'add': '00000',
'sub': '00001',
'mov': {'00010', '00011'}, # B, C
'ld': '00100',
'st': '00101',
'mul': '00110',
'div': '00111',
'rs': '01000',
'ls': '01001',
'xor': '01010',
'or': '01011',
'and': '01100',
'not': '01101',
'cmp': '01110',
'jmp': '01111',
'jlt': '11100',
'jgt': '11101',
'je': '11111',
'hlt': '11010',
'addf': '10000',
'subf': '10001',
'movf': '10010'
}
reg = {
'R0': '000',
'r0': '000',
'R1': '001',
'r1': '001',
'R2': '010',
'r2': '010',
'R3': '011',
'r3': '011',
'R4': '100',
'r4': '100',
'R5': '101',
'r5': '101',
'R6': '110',
'r6': '110',
'FLAGS': '111'
}
-
Input Format:
- The input assembly code should be provided via standard input (stdin).
- Labels are declared with a colon (
:
) at the end of a line. - Variables are declared using the
var
keyword.
-
Running the Compiler:
python compiler.py < input_code.txt
Replace
input_code.txt
with the name of your input file.
The compiler performs various error checks, including checking for undefined variables, label misuse, typos in instruction names, and more. If errors are detected, the compiler outputs detailed error messages.
var X
mov R1 X
add R2 R1 R3
ld R4 X
hlt
This example code declares a variable X
, moves its value to R1
, adds the values in R1
and R3
and loads the value of X
into R4
, finally halting the program.
A simple assembly simulator that executes instructions and updates the register and memory accordingly.
- add: '00000'
- sub: '00001'
- movb: '00010'
- movc: '00011'
- ld: '00100'
- st: '00101'
- mul: '00110'
- div: '00111'
- rs: '01000'
- ls: '01001'
- xor: '01010'
- or: '01011'
- and: '01100'
- not: '01101'
- cmp: '01110'
- jmp: '01111'
- jlt: '11100'
- jgt: '11101'
- je: '11111'
- hlt: '11010'
- A: add, sub, mul, xor, or, and
- B: movb, rs, ls
- C: movc, div, not, cmp
- D: ld, st
- E: jmp, jlt, jgt, je
- F: hlt
- R0: '000'
- R1: '001'
- R2: '010'
- R3: '011'
- R4: '100'
- R5: '101'
- R6: '110'
- FLAGS: '111'
{
'R0': '0' * 16,
'R1': '0' * 16,
'R2': '0' * 16,
'R3': '0' * 16,
'R4': '0' * 16,
'R5': '0' * 16,
'R6': '0' * 16,
'FLAGS': '0' * 16,
}
- Input your assembly code into the simulator.
- Run the simulator to execute the instructions.
- View the updated register and memory values after execution.
# Example Assembly Code
ld 00000001 00000100
add 00000100 00000101 00000110
st 00000010 00000110
hlt
$ python simulator.py
This code is provided under the MIT License.