SlideShare ist ein Scribd-Unternehmen logo
1 von 8
Downloaden Sie, um offline zu lesen
I'm having difficulty with the directives i figured out a duplication issue but i still can't get it to
work as intended. It's supposed to be a mips compiler that handles a subset of mips instruction
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this
license
* Click nbfs://nbhost/SystemFileSystem/Templates/cFiles/main.c to edit this template
*/
/*
* File: main.c
* Author: Andrew Schelb
*
* Created on March 7, 2023, 2:51 PM
*/
#include
#include
#define TEXT_SEGMENT 0x0200
#define DATA_SEGMENT 0x0000
#define MAX_SIZE 1024
typedef struct {
char* name;
int opcode;
int funct;
int rs;
int rt;
int rd;
int imm;
int add;
} Instruction;
typedef struct {
int n;
int size;
char* name;
} Directive;
// MIPS Instructions
Instruction instructions[] = {
{"add", 0, 32, 0, 0, 0, 0, 0},
{"addu", 0, 33, 0, 0, 0, 0, 0},
{"sub", 0, 34, 0, 0, 0, 0, 0},
{"sll", 0, 0, 0, 0, 0, 0, 0},
{"slt", 0, 42, 0, 0, 0, 0, 0},
{"addi", 8, 0, 0, 0, 0, 0, 0},
{"lui", 15, 0, 0, 0, 0, 0, 0},
{"ori", 13, 0, 0, 0, 0, 0, 0},
{"lw", 35, 0, 0, 0, 0, 0, 0},
{"sw", 43, 0, 0, 0, 0, 0, 0},
{NULL, 0, 0, 0, 0, 0, 0, 0}
};
// MIPS Directives
Directive directives[] = {
{0, 0, ".text"},
{1, 0, ".data"},
{2, 1, ".dbyte"},
{3, 4, ".integer"},
{-1, 0, NULL}
};
// Function Prototypes
Instruction* getInstruction(char*);
Directive* getDirective(char*);
int getRegisterNumber(char*);
int parseInstruction(char*, int*, int*);
int parseDirective(char*, int*, int*);
void assemble(char*, int*);
int main(int argc, char** argv) {
// Checking command line arguments
if (argc < 2) {
printf("Usage: %s n", argv[0]);
return -1;
}
// Initializing memory
int text_segment[MAX_SIZE];
int data_segment[MAX_SIZE];
memset(text_segment, 0, MAX_SIZE);
memset(data_segment, 0, MAX_SIZE);
// Assembling
assemble(argv[1], text_segment);
// Writing to output file
FILE* fp = fopen(argv[2], "wb");
fwrite(data_segment, 1, MAX_SIZE, fp);
fwrite(text_segment, 1, MAX_SIZE, fp);
fclose(fp);
return 0;
}
// Assembling the input file
void assemble(char* filename, int* text_segment) {
int line_number = 0;
int text_offset = 0;
int data_offset = 0;
char line[256];
FILE* fp = fopen(filename, "r");
while (fgets(line, 256, fp)) {
// Handling blank lines
if (strcmp(line, "n") == 0) {
continue;
}
// Parsing line
char* label = NULL;
char* instruction = NULL;
char* directive = NULL;
char* operands = NULL;
label = strtok(line, ":");
instruction = strtok(NULL, " nt");
directive = strtok(NULL, " nt");
operands = strtok(NULL, "nt");
// Handling instructions
if (instruction) {
// Parsing instruction
int opcode = 0;
int funct = 0;
int rs = 0;
int rt = 0;
int rd = 0;
int imm = 0;
int add = 0;
if (parseInstruction(instruction, &opcode, &funct)) {
rs = getRegisterNumber(strtok(operands, ","));
rt = getRegisterNumber(strtok(NULL, ","));
if (opcode == 0) {
rd = getRegisterNumber(strtok(NULL, ","));
} else {
imm = atoi(strtok(NULL, ","));
}
// Generating machine code
int machine_code = 0;
machine_code |= (opcode << 26);
machine_code |= (rs << 21);
machine_code |= (rt << 16);
machine_code |= (rd << 11);
machine_code |= (imm << 0);
machine_code |= (add << 0);
machine_code |= (funct << 0);
// Writing machine code to text segment
text_segment[text_offset] = machine_code;
text_offset += 1;
}
}
// Handling directives
if (directive) {
// Parsing directive
int type = 0;
int size = 0;
if (parseDirective(directive, &type, &size)) {
char* token = strtok(operands, ",");
while (token) {
int value = 0;
sscanf(token, "%d", &value);
// Handling .dbyte
if (type == 2) {
data_segment[data_offset] = value;
data_offset += 1;
}
// Handling .integer
if (type == 3) {
int* int_ptr = (int*)&data_segment[data_offset];
*int_ptr = value;
data_offset += size;
}
token = strtok(NULL, ",");
}
}
}
} // End of while loop
fclose(fp);
}
// Freeing memory
free(line);
// Handling missing .text directive
if (text_offset == 0) {
printf("Error: No .text directive found.n");
exit(-1);
}
// Parsing instruction and returning opcode and funct
int parseInstruction(char* instruction, int* opcode, int* funct) {
Instruction* current = instructions;
while (current->name != NULL) {
if (strcmp(current->name, instruction) == 0) {
*opcode = current->opcode;
*funct = current->funct;
return 1;
}
current++;
}
return 0;
}
// Parsing directive and returning type and size
int parseDirective(char* directive, int* type, int* size) {
Directive* current = directives;
while (current->name != NULL) {
if (strcmp(current->name, directive) == 0) {
*type = current->n;
*size = current->size;
return 1;
}
current++;
}
return 0;
}
// Getting register number
int getRegisterNumber(char* reg) {
if (reg[0] == '$') {
if (reg[1] == 's' && reg[2] >= '0' && reg[2] <= '7') {
return reg[2] - '0' + 16;
} else if (reg[1] == 't' && reg[2] >= '0' && reg[2] <= '9') {
return reg[2] - '0' + 8;
} else if (strcmp(reg, "$zero") == 0) {
return 0;
} else if (strcmp(reg, "$sp") == 0) {
return 29;
} else if (strcmp(reg, "$fp") == 0) {
return 30;
} else if (strcmp(reg, "$ra") == 0) {
return 31;
}
}
return -1;
}
// Getting instruction from name
Instruction* getInstruction(char* name) {
Instruction* current = instructions;
while (current->name != NULL) {
if (strcmp(current->name, name) == 0) {
return current;
}
current++;
}
return NULL;
}
// Getting directive from name
Directive* getDirective(char* name) {
Directive* current = directives;
while (current->name != NULL) {
if (strcmp(current->name, name) == 0) {
return current;
}
current++;
}
return NULL;
}
The assembler generates an output file of size 1KB consisting of 512B of data segment (begins at
0x0000) and 512B of text segment (begins at 0x0200). Since each data and instruction is 4B
long, there will be at maximum 128 word data and at maximum 128 instruction words. This file
can be considered a binary executable file. Do not try to open this file as it causes an error.
The assembler does not detect syntax errors and assumes the assembly input is correctly formed.
This example assumes big-endian.
For a sample assembly input file (test.asm):
.data .integer 120
.dbyte 8 .
integer 121
.text
addi $1,$0,10
lw $2,123($4)
sw $3,4($1)
add $10,$0,$0
sub $11,$0,$0
sll $4,$4,10
lui $5,123
slt $10,$0,$0
ori $11,$0,10
addu $6,$10,$13

Weitere ähnliche Inhalte

Ähnlich wie Im having difficulty with the directives i figured out a duplicatio.pdf

The Ring programming language version 1.9 book - Part 94 of 210
The Ring programming language version 1.9 book - Part 94 of 210The Ring programming language version 1.9 book - Part 94 of 210
The Ring programming language version 1.9 book - Part 94 of 210Mahmoud Samir Fayed
 
I need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docxI need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docxPhil4IDBrownh
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxgordienaysmythe
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
Using an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdfUsing an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdfgiriraj65
 
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...GkhanGirgin3
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...ssuserd6b1fd
 
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docxAdamq0DJonese
 
Implementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdfImplementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdffunkybabyindia
 
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docxMARRY7
 
#include stdafx.h using namespace std; #include stdlib.h.docx
#include stdafx.h using namespace std; #include stdlib.h.docx#include stdafx.h using namespace std; #include stdlib.h.docx
#include stdafx.h using namespace std; #include stdlib.h.docxajoy21
 

Ähnlich wie Im having difficulty with the directives i figured out a duplicatio.pdf (20)

The Ring programming language version 1.9 book - Part 94 of 210
The Ring programming language version 1.9 book - Part 94 of 210The Ring programming language version 1.9 book - Part 94 of 210
The Ring programming language version 1.9 book - Part 94 of 210
 
Codes
CodesCodes
Codes
 
C programming
C programmingC programming
C programming
 
I need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docxI need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docx
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Using an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdfUsing an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdf
 
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
 
Implementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdfImplementing a basic directory-tree structure that is derived from a.pdf
Implementing a basic directory-tree structure that is derived from a.pdf
 
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 
Gps c
Gps cGps c
Gps c
 
Embedding perl
Embedding perlEmbedding perl
Embedding perl
 
c programming
c programmingc programming
c programming
 
#include stdafx.h using namespace std; #include stdlib.h.docx
#include stdafx.h using namespace std; #include stdlib.h.docx#include stdafx.h using namespace std; #include stdlib.h.docx
#include stdafx.h using namespace std; #include stdlib.h.docx
 
Unit3 C
Unit3 C Unit3 C
Unit3 C
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 

Mehr von maheshkumar12354

In a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdfIn a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdfmaheshkumar12354
 
In a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdfIn a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdfmaheshkumar12354
 
In a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdfIn a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdfmaheshkumar12354
 
In a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdfIn a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdfmaheshkumar12354
 
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdfIn a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdfmaheshkumar12354
 
In 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdfIn 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdfmaheshkumar12354
 
In 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdfIn 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdfmaheshkumar12354
 
In 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdfIn 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdfmaheshkumar12354
 
Implement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdfImplement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdfmaheshkumar12354
 
Implement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdfImplement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdfmaheshkumar12354
 
Implement a singly linked list as a functional data structure in Kot.pdf
Implement a singly linked list as a functional data structure in Kot.pdfImplement a singly linked list as a functional data structure in Kot.pdf
Implement a singly linked list as a functional data structure in Kot.pdfmaheshkumar12354
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfmaheshkumar12354
 
import java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdfimport java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdfmaheshkumar12354
 
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdfImplement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdfmaheshkumar12354
 
If you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdfIf you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdfmaheshkumar12354
 
Imagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdfImagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdfmaheshkumar12354
 
Im trying to run make qemu-nox In a putty terminal but it.pdf
Im trying to run  make qemu-nox  In a putty terminal but it.pdfIm trying to run  make qemu-nox  In a putty terminal but it.pdf
Im trying to run make qemu-nox In a putty terminal but it.pdfmaheshkumar12354
 
Im posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdfIm posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdfmaheshkumar12354
 
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdfIgnacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdfmaheshkumar12354
 
imagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdfimagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdfmaheshkumar12354
 

Mehr von maheshkumar12354 (20)

In a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdfIn a single strand of DNA, the individual nucleotides are covalenty .pdf
In a single strand of DNA, the individual nucleotides are covalenty .pdf
 
In a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdfIn a recent survey conducted, a random sample of adults 18 years of .pdf
In a recent survey conducted, a random sample of adults 18 years of .pdf
 
In a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdfIn a recent survey conducted a random sample of adults 18 years of a.pdf
In a recent survey conducted a random sample of adults 18 years of a.pdf
 
In a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdfIn a hypothetical study, a researcher finds that police officers are.pdf
In a hypothetical study, a researcher finds that police officers are.pdf
 
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdfIn a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
In a 1 to 2 page paper, Times New Roman 12, double spaced, APA forma.pdf
 
In 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdfIn 2017, President Donald Trump was considering a major increase in .pdf
In 2017, President Donald Trump was considering a major increase in .pdf
 
In 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdfIn 2021, Lee Jones put the final touches on a product she had worked.pdf
In 2021, Lee Jones put the final touches on a product she had worked.pdf
 
In 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdfIn 2000 the Vermont state legislature approved a bill authorizing �c.pdf
In 2000 the Vermont state legislature approved a bill authorizing �c.pdf
 
Implement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdfImplement the class Linked List to create a list of integers. You ne.pdf
Implement the class Linked List to create a list of integers. You ne.pdf
 
Implement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdfImplement a program in C++ a by creating a list ADT using the Object.pdf
Implement a program in C++ a by creating a list ADT using the Object.pdf
 
Implement a singly linked list as a functional data structure in Kot.pdf
Implement a singly linked list as a functional data structure in Kot.pdfImplement a singly linked list as a functional data structure in Kot.pdf
Implement a singly linked list as a functional data structure in Kot.pdf
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
 
import java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdfimport java.awt.Color; import java.awt.Dimension; import.pdf
import java.awt.Color; import java.awt.Dimension; import.pdf
 
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdfImplement two project schedules.BENCHMARKSImplement mechanisms t.pdf
Implement two project schedules.BENCHMARKSImplement mechanisms t.pdf
 
If you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdfIf you were to write an application program that needs to maintain s.pdf
If you were to write an application program that needs to maintain s.pdf
 
Imagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdfImagine that you are using a learning management system (such as Bla.pdf
Imagine that you are using a learning management system (such as Bla.pdf
 
Im trying to run make qemu-nox In a putty terminal but it.pdf
Im trying to run  make qemu-nox  In a putty terminal but it.pdfIm trying to run  make qemu-nox  In a putty terminal but it.pdf
Im trying to run make qemu-nox In a putty terminal but it.pdf
 
Im posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdfIm posting this again because the answer wasnt correct.Please .pdf
Im posting this again because the answer wasnt correct.Please .pdf
 
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdfIgnacio enters into a game of chance. A bag of money has twelve $1 b.pdf
Ignacio enters into a game of chance. A bag of money has twelve $1 b.pdf
 
imagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdfimagine a protein that has been engineered to contain a nuclear loca.pdf
imagine a protein that has been engineered to contain a nuclear loca.pdf
 

Kürzlich hochgeladen

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 

Kürzlich hochgeladen (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

Im having difficulty with the directives i figured out a duplicatio.pdf

  • 1. I'm having difficulty with the directives i figured out a duplication issue but i still can't get it to work as intended. It's supposed to be a mips compiler that handles a subset of mips instruction /* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/cFiles/main.c to edit this template */ /* * File: main.c * Author: Andrew Schelb * * Created on March 7, 2023, 2:51 PM */ #include #include #define TEXT_SEGMENT 0x0200 #define DATA_SEGMENT 0x0000 #define MAX_SIZE 1024 typedef struct { char* name; int opcode; int funct; int rs; int rt; int rd; int imm; int add; } Instruction; typedef struct { int n; int size; char* name; } Directive; // MIPS Instructions Instruction instructions[] = {
  • 2. {"add", 0, 32, 0, 0, 0, 0, 0}, {"addu", 0, 33, 0, 0, 0, 0, 0}, {"sub", 0, 34, 0, 0, 0, 0, 0}, {"sll", 0, 0, 0, 0, 0, 0, 0}, {"slt", 0, 42, 0, 0, 0, 0, 0}, {"addi", 8, 0, 0, 0, 0, 0, 0}, {"lui", 15, 0, 0, 0, 0, 0, 0}, {"ori", 13, 0, 0, 0, 0, 0, 0}, {"lw", 35, 0, 0, 0, 0, 0, 0}, {"sw", 43, 0, 0, 0, 0, 0, 0}, {NULL, 0, 0, 0, 0, 0, 0, 0} }; // MIPS Directives Directive directives[] = { {0, 0, ".text"}, {1, 0, ".data"}, {2, 1, ".dbyte"}, {3, 4, ".integer"}, {-1, 0, NULL} }; // Function Prototypes Instruction* getInstruction(char*); Directive* getDirective(char*); int getRegisterNumber(char*); int parseInstruction(char*, int*, int*); int parseDirective(char*, int*, int*); void assemble(char*, int*); int main(int argc, char** argv) { // Checking command line arguments if (argc < 2) { printf("Usage: %s n", argv[0]); return -1; } // Initializing memory int text_segment[MAX_SIZE]; int data_segment[MAX_SIZE];
  • 3. memset(text_segment, 0, MAX_SIZE); memset(data_segment, 0, MAX_SIZE); // Assembling assemble(argv[1], text_segment); // Writing to output file FILE* fp = fopen(argv[2], "wb"); fwrite(data_segment, 1, MAX_SIZE, fp); fwrite(text_segment, 1, MAX_SIZE, fp); fclose(fp); return 0; } // Assembling the input file void assemble(char* filename, int* text_segment) { int line_number = 0; int text_offset = 0; int data_offset = 0; char line[256]; FILE* fp = fopen(filename, "r"); while (fgets(line, 256, fp)) { // Handling blank lines if (strcmp(line, "n") == 0) { continue; } // Parsing line char* label = NULL; char* instruction = NULL; char* directive = NULL; char* operands = NULL; label = strtok(line, ":"); instruction = strtok(NULL, " nt"); directive = strtok(NULL, " nt"); operands = strtok(NULL, "nt"); // Handling instructions if (instruction) { // Parsing instruction int opcode = 0;
  • 4. int funct = 0; int rs = 0; int rt = 0; int rd = 0; int imm = 0; int add = 0; if (parseInstruction(instruction, &opcode, &funct)) { rs = getRegisterNumber(strtok(operands, ",")); rt = getRegisterNumber(strtok(NULL, ",")); if (opcode == 0) { rd = getRegisterNumber(strtok(NULL, ",")); } else { imm = atoi(strtok(NULL, ",")); } // Generating machine code int machine_code = 0; machine_code |= (opcode << 26); machine_code |= (rs << 21); machine_code |= (rt << 16); machine_code |= (rd << 11); machine_code |= (imm << 0); machine_code |= (add << 0); machine_code |= (funct << 0); // Writing machine code to text segment text_segment[text_offset] = machine_code; text_offset += 1; } } // Handling directives if (directive) { // Parsing directive int type = 0; int size = 0; if (parseDirective(directive, &type, &size)) { char* token = strtok(operands, ","); while (token) {
  • 5. int value = 0; sscanf(token, "%d", &value); // Handling .dbyte if (type == 2) { data_segment[data_offset] = value; data_offset += 1; } // Handling .integer if (type == 3) { int* int_ptr = (int*)&data_segment[data_offset]; *int_ptr = value; data_offset += size; } token = strtok(NULL, ","); } } } } // End of while loop fclose(fp); } // Freeing memory free(line); // Handling missing .text directive if (text_offset == 0) { printf("Error: No .text directive found.n"); exit(-1); } // Parsing instruction and returning opcode and funct int parseInstruction(char* instruction, int* opcode, int* funct) { Instruction* current = instructions; while (current->name != NULL) { if (strcmp(current->name, instruction) == 0) { *opcode = current->opcode; *funct = current->funct; return 1;
  • 6. } current++; } return 0; } // Parsing directive and returning type and size int parseDirective(char* directive, int* type, int* size) { Directive* current = directives; while (current->name != NULL) { if (strcmp(current->name, directive) == 0) { *type = current->n; *size = current->size; return 1; } current++; } return 0; } // Getting register number int getRegisterNumber(char* reg) { if (reg[0] == '$') { if (reg[1] == 's' && reg[2] >= '0' && reg[2] <= '7') { return reg[2] - '0' + 16; } else if (reg[1] == 't' && reg[2] >= '0' && reg[2] <= '9') { return reg[2] - '0' + 8; } else if (strcmp(reg, "$zero") == 0) { return 0; } else if (strcmp(reg, "$sp") == 0) { return 29; } else if (strcmp(reg, "$fp") == 0) { return 30; } else if (strcmp(reg, "$ra") == 0) { return 31; } } return -1;
  • 7. } // Getting instruction from name Instruction* getInstruction(char* name) { Instruction* current = instructions; while (current->name != NULL) { if (strcmp(current->name, name) == 0) { return current; } current++; } return NULL; } // Getting directive from name Directive* getDirective(char* name) { Directive* current = directives; while (current->name != NULL) { if (strcmp(current->name, name) == 0) { return current; } current++; } return NULL; } The assembler generates an output file of size 1KB consisting of 512B of data segment (begins at 0x0000) and 512B of text segment (begins at 0x0200). Since each data and instruction is 4B long, there will be at maximum 128 word data and at maximum 128 instruction words. This file can be considered a binary executable file. Do not try to open this file as it causes an error. The assembler does not detect syntax errors and assumes the assembly input is correctly formed. This example assumes big-endian. For a sample assembly input file (test.asm): .data .integer 120 .dbyte 8 . integer 121 .text addi $1,$0,10
  • 8. lw $2,123($4) sw $3,4($1) add $10,$0,$0 sub $11,$0,$0 sll $4,$4,10 lui $5,123 slt $10,$0,$0 ori $11,$0,10 addu $6,$10,$13