first successful parsing from .asm to notational representationrun testParse
This commit is contained in:
+50
-50
@@ -1,55 +1,55 @@
|
|||||||
[H[2J[3JCompiling...
|
[H[2J[3JCompiling...
|
||||||
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 "Enter a number (n input n > 2) to find the nth term:"
|
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
|
||||||
|
syscalle
|
||||||
|
s
|
||||||
|
jal getInpute
|
||||||
|
j
|
||||||
|
jal fibonacciM
|
||||||
|
move a0 v0c
|
||||||
|
li v0 1
|
||||||
syscall
|
syscall
|
||||||
|
s
|
||||||
jalgetInput
|
ori v0 0 10c
|
||||||
|
syscall0
|
||||||
jalfibonacci
|
s
|
||||||
move$a0$v0
|
getInput:1
|
||||||
li$v01
|
li v0 4t
|
||||||
syscall
|
la a0 prompti
|
||||||
|
syscallr
|
||||||
ori$v0$010
|
s
|
||||||
syscall
|
li v0 5r
|
||||||
|
syscallr
|
||||||
getInput:
|
move a0 v0p
|
||||||
li$v04
|
m
|
||||||
la$a0prompt
|
jr raa
|
||||||
syscall
|
j
|
||||||
|
fibonacci:p
|
||||||
li$v05
|
move s1 a0p
|
||||||
syscall
|
li t0 2
|
||||||
move$a0$v0
|
li t1 0
|
||||||
|
li t2 1
|
||||||
jr$ra
|
li t3 0
|
||||||
|
l
|
||||||
fibonacci:
|
while:0
|
||||||
move$s1$a0
|
bge t0 s1 donee
|
||||||
li$t02
|
add t3 t1 t2n
|
||||||
li$t10
|
move t1 t2t
|
||||||
li$t21
|
m
|
||||||
li$t30
|
move t2 t3t
|
||||||
|
addi t0 t0 1n
|
||||||
while:
|
a
|
||||||
bge$t0$s1done
|
|
||||||
add$t3$t1$t2
|
|
||||||
move$t1$t2
|
|
||||||
|
|
||||||
move$t2$t3
|
|
||||||
addi$t0$t01
|
|
||||||
|
|
||||||
b while
|
b while
|
||||||
|
b
|
||||||
done:
|
done:l
|
||||||
move$v0$t2
|
move v0 t2
|
||||||
jr$ra
|
jr rav
|
||||||
|
|||||||
@@ -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)
|
||||||
|
};
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
+75
-19
@@ -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;
|
||||||
|
|
||||||
|
// Determines if the parser should keep a space, based upon if the space is surrounded by two non-space characters.
|
||||||
|
int determineKeepSpace(char prior, char after) {
|
||||||
|
if(prior != NULL && !isspace(prior))
|
||||||
|
if(after != NULL && !isspace(after))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleans up the input string to prepare for parsing into command and arguments.
|
||||||
char * removeCommentsAndWhiteSpace(char * line) {
|
char * removeCommentsAndWhiteSpace(char * line) {
|
||||||
int j = 0;
|
int len = strlen(line);
|
||||||
char * newLine;
|
char tmpLine[256];
|
||||||
for(int i = 0; i < strlen(line); i++) {
|
int parsedIndex = 0;
|
||||||
// If we've reached a comment, we can break to return what we have
|
|
||||||
|
for(int i = 0; i < len; i++) {
|
||||||
|
|
||||||
|
// Check if we've reached a comment and if so, break out of the loop
|
||||||
if(line[i] == '#') break;
|
if(line[i] == '#') break;
|
||||||
|
|
||||||
// Remove commas and spaces.
|
// Check if the current character is not a comma or dollar sign.
|
||||||
if((line[i] != ',') && (isspace(line[i]) == 0)) {
|
if(line[i] != ',' && line[i] != '$' && !isspace(line[i])) {
|
||||||
newLine[j] = line[i];
|
tmpLine[parsedIndex] = line[i];
|
||||||
j++;
|
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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove any excess that is stored from a prior line.
|
// Create a dynamically sized array containing only the valid characters
|
||||||
for(; j < strlen(line); j++)
|
char * parsed = (char *)calloc(parsedIndex, sizeof(char));
|
||||||
newLine[j] = (char)NULL;
|
for(int i = 0; i < parsedIndex; i++)
|
||||||
|
parsed[i] = tmpLine[i];
|
||||||
|
|
||||||
return newLine;
|
return parsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract the R, I, or J format instruction from each "bare line". A bare line has no spaces or commas.
|
// Parse a line of assembly into numerical representations for the function (eg. add = 0), registers, offset, etc.
|
||||||
r* parse(char * bareLine) {
|
int * parseIntoNotation(char * bareLine) {
|
||||||
r* format = (r*)malloc(sizeof(r));
|
int numFunctions = 2; // TODO: Need to obtain dynamically.
|
||||||
|
int * notation = (int *)calloc(4, sizeof(int));
|
||||||
|
//char * tokens = strtok(bareLine, " ");
|
||||||
|
|
||||||
for(int i = 0; i < strlen(bareLine); i++) {
|
char * token = strtok(bareLine, " ");
|
||||||
if(bareLine[i] == '$') {
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user