forked from omk12001/cse4302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
.
- Loading branch information
omer1977
committed
Aug 29, 2019
0 parents
commit 433d40c
Showing
14 changed files
with
2,294 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
HEADERS = file_parser.h | ||
|
||
default: assembler | ||
|
||
assembler: assembler.c file_parser.c | ||
gcc assembler.c file_parser.c -o assembler -std=c99 | ||
|
||
clean: | ||
-rm -f assembler.o | ||
-rm -f assembler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
Mips Assembler | ||
============== | ||
|
||
An assembler for a subset of the MIPS like instruction set architecture | ||
|
||
# How to use | ||
The assembler will take a file written in assembly language as input on the command line and will produce an output file containing the MIPS machine code. The input file should be in ASCII text. Each line in the input assembly file contains either a mnemonic, a section header (such as .data) or a label (jump or branch target. The maximum length of a line is 4 bytes. Section headers such as .data and .text should be in a line by themselves with no other assembly mnemonic. Similarly, branch targets such as loop: will be on a line by themselves with no other assembly mnemonic. The input assembly file should only contain one data section and one text section. The first section in the file will be the .text section, followed by the .data section. | ||
|
||
The assembler supports the following instruction set: | ||
|
||
R-Type | ||
- add | ||
- sub | ||
- and | ||
- or | ||
- sll | ||
- slt | ||
- srl | ||
- jr | ||
|
||
I-Type | ||
- lw | ||
- sw | ||
- andi | ||
- addi | ||
- slti | ||
- ori | ||
- lui | ||
- beq | ||
- bne | ||
|
||
J-Type | ||
- j | ||
- jal | ||
|
||
# Run | ||
to compile the assembler | ||
$ ./make | ||
|
||
to run the assembler on a nop.asm assembly file to write machine code in nop.out | ||
$ ./assembler nop.asm nop.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* assembler.c | ||
* | ||
* Created on: Oct 3, 2011 | ||
* Modified by Masab Ahmad | ||
*/ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include "file_parser.h" | ||
#include "hash_table.h" | ||
|
||
int search(char *instruction); | ||
|
||
// Array that holds the supported instructions | ||
char *instructions[] = { | ||
"la", // 0 | ||
"lui", // 1 | ||
"lw", // 2 | ||
"sw", // 3 | ||
"add", // 4 | ||
"sub", // 5 | ||
"addi", // 6 | ||
"or", // 7 | ||
"and", // 8 | ||
"ori", // 9 | ||
"andi", // 10 | ||
"slt", // 11 | ||
"slti", // 12 | ||
"sll", // 13 | ||
"srl", // 14 | ||
"beq", // 15 | ||
"bne", //16 | ||
"j", //17 | ||
"jr", //18 | ||
"jal" //19 | ||
}; | ||
|
||
// Size of array | ||
size_t inst_len = sizeof(instructions)/sizeof(char *); | ||
|
||
int search(char *instruction) { | ||
|
||
int found = 0; | ||
|
||
for (int i = 0; i < inst_len; i++) { | ||
|
||
if (strcmp(instruction, instructions[i]) == 0) { | ||
found = 1; | ||
return i; | ||
} | ||
} | ||
|
||
if (found == 0) | ||
return -1; | ||
} | ||
|
||
// Quick Sort String Comparison Function | ||
int string_comp(const void *a, const void *b) { | ||
return strcmp(*(char **)a, *(char **)b); | ||
} | ||
|
||
int main (int argc, char *argv[]) { | ||
|
||
// Make sure correct number of arguments input | ||
if (argc != 3) { | ||
printf("Incorrect number of arguments"); | ||
} | ||
|
||
else { | ||
|
||
// Open I/O files | ||
// Check that files opened properly | ||
FILE *In; | ||
In = fopen(argv[1], "r"); | ||
if (In == NULL) { | ||
printf("Input file could not be opened."); | ||
exit(1); | ||
} | ||
|
||
FILE *Out; | ||
Out = fopen(argv[2], "w"); | ||
if (Out == NULL) { | ||
printf("Output file could not opened."); | ||
exit(1); | ||
} | ||
|
||
// Sort the array using qsort for faster search | ||
qsort(instructions, inst_len, sizeof(char *), string_comp); | ||
|
||
// Create a hash table of size 127 | ||
hash_table_t *hash_table = create_hash_table(127); | ||
|
||
// Parse in passes | ||
|
||
int passNumber = 1; | ||
parse_file(In, passNumber, instructions, inst_len, hash_table, Out); | ||
|
||
// Rewind input file & start pass 2 | ||
rewind(In); | ||
passNumber = 2; | ||
parse_file(In, passNumber, instructions, inst_len, hash_table, Out); | ||
|
||
// Close files | ||
fclose(In); | ||
fclose(Out); | ||
|
||
return 0; | ||
} | ||
} |
Oops, something went wrong.