Skip to content

Haxous742/MARS-ASSEMBLER

Repository files navigation

MARS-ASSEMBLER

A python assembler that converts the assembly code written in MARS to machine code

Contents

  • Assembler.py: Assembles the MIPS code.
  • Machine_code.txt: Stores the output from the assembler.
  • Binary_search.asm
  • GCD.asm
  • GCD Folder:
    • GCD.cpp: Code for GCD.
    • GCD.asm: Assembler code for GCD.
    • GCD_MARS.bin: Machine code generated from MARS.
    • GCD_mc.bin: Machine code generated from my assembler.
  • Binary Search Folder:
    • Binary_search.cpp: Code for Binary Search.
    • Binary_search.asm: Assembly code for Binary Search.
    • Binary_search_MARS.bin: Machine code generated from MARS.
    • Binary_search_mc.bin: Machine code generated from my assembler.
  • Comparator Folder:
    • Assembled.txt: Assembled code.
    • Original.txt: MARS code.
    • Compare.py: Compares the codes in Assembled.txt and Original.txt to find discrepancies.

GCD MIPS Code

The code implements the Recursive Stein’s Algorithm to compute the GCD of two numbers. After assembling, it will prompt for two numbers and display the result.

image

Binary Search Code

After assembling, the program will:

  1. Ask for the number of integers in the array.

image

  1. Prompt for the starting address of the array (e.g., 268501216).

image

  1. Require input of the sorted numbers one by one.

image

  1. Ask for the target number to search.

image

If the number is found, the output will display its index (0-indexed); otherwise, it will state that the number was not found.

image

Assembler

The assembler takes an .asm or .txt file as input and writes the machine code to Machine_code.txt.

For giving the input file, we have to put the file name at this location and compile the code:

image

Output file:

image

After compiling, the machine code will be available in the output file and the process the assembler went with will be displayed in the cmd lines:

image

image

image

image

Comparator

This tool checks two text files: Assembled.txt and Original.txt. It confirms if the files are identical or displays the lines with mismatches.

image

image

How to Run the Comparator:

python Compare.py Original.txt Assembled.txt

DISCLAIMER-

While giving the input to the assembler, we have to give the entire file including the .data segment as well, as it will use that to make address references for the data segments as well, and when we compare the assembled text with the MARS machine code, the mismatches shown are all address mis-matches of the .data segment as the assembler is not able to precisely calculate the address of all the .data segments as the MARS simulator, apart from that everything else should match.

The instruction included in the assembler are:

R-Type Instructions (Register-based operations)

Instruction Opcode (6 bits) rs (5 bits) rt (5 bits) rd (5 bits) shamt (5 bits) funct (6 bits) Description
and 000000 rs rt rd 00000 100100 Bitwise AND (rd = rs & rt)
or 000000 rs rt rd 00000 100101 Bitwise OR (rd = rs | rt)
sub 000000 rs rt rd 00000 100010 Subtract (rd = rs - rt)
sll 000000 00000 rt rd shamt 000000 Shift Left Logical (rd = rt << shamt)
sllv 000000 rs rt rd 00000 000100 Shift Left Logical Variable (rd = rt << rs)
srl 000000 00000 rt rd shamt 000010 Shift Right Logical (rd = rt >> shamt)
slt 000000 rs rt rd 00000 101010 Set on Less Than (rd = 1 if rs < rt else 0)
jr 000000 rs 00000 00000 00000 001000 Jump Register (PC ← rs)
syscall 000000 (unused) (unused) (unused) 00000 001100 System call

I-Type Instructions (Immediate-based operations)

Instruction Opcode (6 bits) rs (5 bits) rt (5 bits) Immediate (16 bits) Description
addi 001000 rs rt imm Add Immediate (rt = rs + imm)
andi 001100 rs rt imm Bitwise AND Immediate (rt = rs & imm)
beq 000100 rs rt offset Branch if Equal (if rs == rt then PC ← PC + (offset << 2))
bne 000101 rs rt offset Branch if Not Equal (if rs != rt then PC ← PC + (offset << 2))
lw 100011 base rt offset Load Word (rt = MEM[base + offset])
sw 101011 base rt offset Store Word (MEM[base + offset] = rt)
ble (pseudo) - - - - Branch if Less or Equal (pseudo-instruction, expands to other ops)
la (pseudo) - - - - Load Address (pseudo-instruction)
li (pseudo) - - - - Load Immediate (pseudo-instruction)

J-Type Instructions (Jump-based operations)

Instruction Opcode (6 bits) Target (26 bits) Description
j 000010 target Jump (PC[31..28] stays same, PC[27..2] = target, PC[1..0] = 0)
jal 000011 target Jump and Link ($ra = PC + 4 then jump to target)

About

A python assembler that converts the assembly code written in MARS to machine code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published