first successful parsing from .asm to notational representationrun testParse

This commit is contained in:
Josh Ashton
2023-10-10 13:41:32 -06:00
parent 87014ffc01
commit 1c2ce3e971
4 changed files with 155 additions and 85 deletions
+53 -53
View File
@@ -1,55 +1,55 @@
Compiling... Compiling...
Output... Output...
.data .dataU
welcomeMessage:.asciiz"WelcometoaMIPSassemblyFibonacciSequencecalculator!\nThiswillcalculatethenthtermoftheFibonacciSequence.\n" welcomeMessage: .asciiz "Welcome to a MIPS assembly Fibonacci Sequence calculator!\nThis will calculate the nth term of the Fibonacci Sequence.\n"!
prompt:.asciiz"Enteranumber(ninputn>2)tofindthenthterm:" prompt: .asciiz "Enter a number (n input n > 2) to find the nth term:"
foundTerm:.asciiz"Thenthtermis:" foundTerm: .asciiz "The nth term is: "u
f
.text .textT
.
main: main:T
li$v04 li v0 4r
la$a0welcomeMessage la a0 welcomeMessageT
syscall syscalle
s
jalgetInput jal getInpute
j
jalfibonacci jal fibonacciM
move$a0$v0 move a0 v0c
li$v01 li v0 1
syscall syscall
s
ori$v0$010 ori v0 0 10c
syscall syscall0
s
getInput: getInput:1
li$v04 li v0 4t
la$a0prompt la a0 prompti
syscall syscallr
s
li$v05 li v0 5r
syscall syscallr
move$a0$v0 move a0 v0p
m
jr$ra jr raa
j
fibonacci: fibonacci:p
move$s1$a0 move s1 a0p
li$t02 li t0 2
li$t10 li t1 0
li$t21 li t2 1
li$t30 li t3 0
l
while: while:0
bge$t0$s1done bge t0 s1 donee
add$t3$t1$t2 add t3 t1 t2n
move$t1$t2 move t1 t2t
m
move$t2$t3 move t2 t3t
addi$t0$t01 addi t0 t0 1n
a
bwhile b while
b
done: done:l
move$v0$t2 move v0 t2
jr$ra jr rav
+14
View File
@@ -0,0 +1,14 @@
#define FOREACH_FUNCTION(FUNCTION) \
FUNCTION(add) \
FUNCTION(li) \
#define GENERATE_ENUM(ENUM) ENUM,
#define GENERATE_STRING(STRING) #STRING,
enum Function {
FOREACH_FUNCTION(GENERATE_ENUM)
};
static char* FUNCTION_STRING[] = {
FOREACH_FUNCTION(GENERATE_STRING)
};
+6 -6
View File
@@ -10,24 +10,24 @@ void initFunctions() {
iFormat = (i*)malloc(sizeof(i)); iFormat = (i*)malloc(sizeof(i));
} }
int * add(int * args[]) { char * add(int * args[]) {
rFormat->opcode = 0; rFormat->opcode = 0;
rFormat->rd = *args[0]; rFormat->rd = *args[0];
rFormat->rs = *args[1]; rFormat->rs = *args[1];
rFormat->rt = *args[2]; rFormat->rt = *args[2];
rFormat->shamt = *args[4]; rFormat->shamt = *args[3];
rFormat->funct = 20; rFormat->funct = 20;
return rToBinary(rFormat); return rToHex(rFormat);
} }
int * sub(int * args[]) { char * sub(int * args[]) {
rFormat->opcode = 0; rFormat->opcode = 0;
rFormat->rd = *args[0]; rFormat->rd = *args[0];
rFormat->rs = *args[1]; rFormat->rs = *args[1];
rFormat->rt = *args[2]; rFormat->rt = *args[2];
rFormat->shamt = *args[4]; rFormat->shamt = *args[3];
rFormat->funct = 22; rFormat->funct = 22;
return rToBinary(rFormat); return rToHex(rFormat);
} }
+82 -26
View File
@@ -5,39 +5,90 @@
#include <string.h> #include <string.h>
#include "formats.h" #include "formats.h"
#include "registers.h"
#include "functionAssigner.h"
FILE * file; FILE * file;
char * removeCommentsAndWhiteSpace(char * line) { // Determines if the parser should keep a space, based upon if the space is surrounded by two non-space characters.
int j = 0; int determineKeepSpace(char prior, char after) {
char * newLine; if(prior != NULL && !isspace(prior))
for(int i = 0; i < strlen(line); i++) { if(after != NULL && !isspace(after))
// If we've reached a comment, we can break to return what we have return 1;
if(line[i] == '#') break;
// Remove commas and spaces. return 0;
if((line[i] != ',') && (isspace(line[i]) == 0)) {
newLine[j] = line[i];
j++;
}
}
// Remove any excess that is stored from a prior line.
for(; j < strlen(line); j++)
newLine[j] = (char)NULL;
return newLine;
} }
// Extract the R, I, or J format instruction from each "bare line". A bare line has no spaces or commas. // Cleans up the input string to prepare for parsing into command and arguments.
r* parse(char * bareLine) { char * removeCommentsAndWhiteSpace(char * line) {
r* format = (r*)malloc(sizeof(r)); int len = strlen(line);
char tmpLine[256];
int parsedIndex = 0;
for(int i = 0; i < strlen(bareLine); i++) { for(int i = 0; i < len; i++) {
if(bareLine[i] == '$') {
// Check if we've reached a comment and if so, break out of the loop
if(line[i] == '#') break;
// Check if the current character is not a comma or dollar sign.
if(line[i] != ',' && line[i] != '$' && !isspace(line[i])) {
tmpLine[parsedIndex] = line[i];
parsedIndex++;
} else if(isspace(line[i])) {
// Check if we're within bounds of the array to check whitespace
if((i - 1 >= 0) && (i + 1 < len)) {
if(determineKeepSpace(line[i - 1], line[i + 1]) == 1) {
tmpLine[parsedIndex] = line[i];
parsedIndex++;
}
}
} }
} }
// Create a dynamically sized array containing only the valid characters
char * parsed = (char *)calloc(parsedIndex, sizeof(char));
for(int i = 0; i < parsedIndex; i++)
parsed[i] = tmpLine[i];
return parsed;
}
// Parse a line of assembly into numerical representations for the function (eg. add = 0), registers, offset, etc.
int * parseIntoNotation(char * bareLine) {
int numFunctions = 2; // TODO: Need to obtain dynamically.
int * notation = (int *)calloc(4, sizeof(int));
//char * tokens = strtok(bareLine, " ");
char * token = strtok(bareLine, " ");
int counter = 0;
while (token != NULL)
{
for(int i = 0; i < numFunctions; i++) {
if(strcmp(token, FUNCTION_STRING[i]) == 0) {
printf("found a match between %s and %s, value is %d\n", token, FUNCTION_STRING[i], i);
notation[counter] = i;
counter++;
continue;
}
}
if(counter == 0) break;
for(int i = 0; i < 32; i++) {
if(strcmp(token, REGISTER_STRING[i]) == 0) {
printf("found a match between %s and %s, value is %d\n", token, REGISTER_STRING[i], i);
notation[counter] = i;
counter++;
continue;
}
}
token = strtok(NULL, " ");
}
return notation;
} }
int initParse(char * filepath) { int initParse(char * filepath) {
@@ -54,8 +105,13 @@ int initParse(char * filepath) {
} }
// Get each line of the file, remove comments and whitespace, and attempt to parse // Get each line of the file, remove comments and whitespace, and attempt to parse
while(fgets(lines, sizeof(lines), file)) while(fgets(lines, sizeof(lines), file)) {
printf("%s\n", removeCommentsAndWhiteSpace(lines)); char * line = removeCommentsAndWhiteSpace(lines);
int * notation = parseIntoNotation(line);
printf("notation: %d %d %d %d\n", notation[0], notation[1], notation[2], notation[3]);
free(notation);
}
fclose(file); fclose(file);