SlideShare ist ein Scribd-Unternehmen logo
1 von 6
Downloaden Sie, um offline zu lesen
/*
* 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. Continue
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 {.
// 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;
// Writing value to data segments
data_segment[data_offset] = value;
data_offset += 1;
}
// Handling .integer
if (type == 3) {
// Writing value to data segment
data_segment[data_offset] = value;
data_offset += 1;
}
token = strtok(NULL, ",");
}
}
}
}
fclose(fp);
}
// Getting MIPS instruction
Instruction* getInstruction(char* instruction) {
int i = 0;
while (instructions[i].name) {
if (strcmp(instructions[i].name, instruction) == 0) {
return &instructions[i];
}
i++;
}
return NULL;
}
// Getting MIPS directive
Directive* getDirective(char* directive) {
int i = 0;
while (directives[i].name) {
if (strcmp(directives[i].name, continue
return &directives[i];
}
i++;
}
return NULL;
}
// Getting register number
int getRegisterNumber(char* register) {
int reg = 0;
sscanf(register, "$%d", &reg);
return reg;
}
// Parsing instruction
int parseInstruction(char* instruction, int* opcode, int* funct) {
Instruction* instr = getInstruction(instruction);
if (instr) {
*opcode = instr->opcode;
*funct = instr->funct;
return 1;
} else {
return 0;
}
}
// Parsing directive
int parseDirective(char* directive, int* type, int* size) {
Directive* dir = getDirective(directive);
if (dir) {
*type = dir->n;
*size = dir->size;
return 1;
} else {
return 0;
}
}
not sure why its compiling errors. i'm running in netbeans

Weitere ähnliche Inhalte

Ähnlich wie Click nbfsnbhostSystemFileSystemTemplatesLicenseslice.pdf

The TCP/IP stack in the FreeBSD kernel COSCUP 2014
The TCP/IP stack in the FreeBSD kernel COSCUP 2014The TCP/IP stack in the FreeBSD kernel COSCUP 2014
The TCP/IP stack in the FreeBSD kernel COSCUP 2014Kevin Lo
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source codeAndrey Karpov
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source codePVS-Studio
 
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019corehard_by
 
External Language Stored Procedures for MySQL
External Language Stored Procedures for MySQLExternal Language Stored Procedures for MySQL
External Language Stored Procedures for MySQLAntony T Curtis
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfmallik3000
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfPart 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfmohammadirfan136964
 
Introduction to Kernel Programming
Introduction to Kernel ProgrammingIntroduction to Kernel Programming
Introduction to Kernel ProgrammingAhmed Mekkawy
 
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...Andrey Karpov
 
Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6scuhurricane
 
JVM code reading -- C2
JVM code reading -- C2JVM code reading -- C2
JVM code reading -- C2ytoshima
 
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321Teddy Hsiung
 
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
 

Ähnlich wie Click nbfsnbhostSystemFileSystemTemplatesLicenseslice.pdf (20)

The TCP/IP stack in the FreeBSD kernel COSCUP 2014
The TCP/IP stack in the FreeBSD kernel COSCUP 2014The TCP/IP stack in the FreeBSD kernel COSCUP 2014
The TCP/IP stack in the FreeBSD kernel COSCUP 2014
 
Sysprog17
Sysprog17Sysprog17
Sysprog17
 
Writing MySQL UDFs
Writing MySQL UDFsWriting MySQL UDFs
Writing MySQL UDFs
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source code
 
Static analysis of C++ source code
Static analysis of C++ source codeStatic analysis of C++ source code
Static analysis of C++ source code
 
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019Статичный SQL в С++14. Евгений Захаров ➠  CoreHard Autumn 2019
Статичный SQL в С++14. Евгений Захаров ➠ CoreHard Autumn 2019
 
Gps c
Gps cGps c
Gps c
 
Quiz 9
Quiz 9Quiz 9
Quiz 9
 
External Language Stored Procedures for MySQL
External Language Stored Procedures for MySQLExternal Language Stored Procedures for MySQL
External Language Stored Procedures for MySQL
 
Modify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdfModify this code to use multiple threads with the same data1.Modif.pdf
Modify this code to use multiple threads with the same data1.Modif.pdf
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfPart 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
 
Introduction to Kernel Programming
Introduction to Kernel ProgrammingIntroduction to Kernel Programming
Introduction to Kernel Programming
 
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
 
Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6
 
Sysprog 13
Sysprog 13Sysprog 13
Sysprog 13
 
JVM code reading -- C2
JVM code reading -- C2JVM code reading -- C2
JVM code reading -- C2
 
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
 
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
 
Osol Pgsql
Osol PgsqlOsol Pgsql
Osol Pgsql
 

Mehr von mdualudin007

(5 points 1. Which of the followhy statements munt be trie 2. What i.pdf
 (5 points 1. Which of the followhy statements munt be trie 2. What i.pdf (5 points 1. Which of the followhy statements munt be trie 2. What i.pdf
(5 points 1. Which of the followhy statements munt be trie 2. What i.pdfmdualudin007
 
(4) With regard to virulence factors, what do the letters O,H and K.pdf
 (4) With regard to virulence factors, what do the letters O,H and K.pdf (4) With regard to virulence factors, what do the letters O,H and K.pdf
(4) With regard to virulence factors, what do the letters O,H and K.pdfmdualudin007
 
(4 points) Match the following data with the correct level of measure.pdf
 (4 points) Match the following data with the correct level of measure.pdf (4 points) Match the following data with the correct level of measure.pdf
(4 points) Match the following data with the correct level of measure.pdfmdualudin007
 
(3) If events occur accordiy to a poidson porles with hate =10minnt..pdf
 (3) If events occur accordiy to a poidson porles with hate =10minnt..pdf (3) If events occur accordiy to a poidson porles with hate =10minnt..pdf
(3) If events occur accordiy to a poidson porles with hate =10minnt..pdfmdualudin007
 
(4 marks) Suppose the production function of the fishing industry is .pdf
 (4 marks) Suppose the production function of the fishing industry is .pdf (4 marks) Suppose the production function of the fishing industry is .pdf
(4 marks) Suppose the production function of the fishing industry is .pdfmdualudin007
 
(3 points) Employee is a derived class of Person, and Worker is a der.pdf
 (3 points) Employee is a derived class of Person, and Worker is a der.pdf (3 points) Employee is a derived class of Person, and Worker is a der.pdf
(3 points) Employee is a derived class of Person, and Worker is a der.pdfmdualudin007
 
(1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdf
 (1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdf (1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdf
(1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdfmdualudin007
 
() Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdf
 () Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdf () Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdf
() Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdfmdualudin007
 
(1 Point) Please match the following variable properties with their d.pdf
 (1 Point) Please match the following variable properties with their d.pdf (1 Point) Please match the following variable properties with their d.pdf
(1 Point) Please match the following variable properties with their d.pdfmdualudin007
 
(1 point) If samples of size 37 are taken from a population with mean.pdf
 (1 point) If samples of size 37 are taken from a population with mean.pdf (1 point) If samples of size 37 are taken from a population with mean.pdf
(1 point) If samples of size 37 are taken from a population with mean.pdfmdualudin007
 
Bag breaks open; included as delay in the allowance factor Conveyo.pdf
  Bag breaks open; included as delay in the allowance factor  Conveyo.pdf  Bag breaks open; included as delay in the allowance factor  Conveyo.pdf
Bag breaks open; included as delay in the allowance factor Conveyo.pdfmdualudin007
 
Prove the following E(MSR)=2+12(Xix)2.pdf
  Prove the following E(MSR)=2+12(Xix)2.pdf  Prove the following E(MSR)=2+12(Xix)2.pdf
Prove the following E(MSR)=2+12(Xix)2.pdfmdualudin007
 
Computer Architecture HOW do I design a computer = Instruction .pdf
  Computer Architecture HOW do I design a computer  = Instruction .pdf  Computer Architecture HOW do I design a computer  = Instruction .pdf
Computer Architecture HOW do I design a computer = Instruction .pdfmdualudin007
 
( Ch2 ISA)3. Assume that the top of the stack in a progra.pdf
 ( Ch2 ISA)3. Assume that the top of the stack in a progra.pdf ( Ch2 ISA)3. Assume that the top of the stack in a progra.pdf
( Ch2 ISA)3. Assume that the top of the stack in a progra.pdfmdualudin007
 
The goal of this assignment is to get familiar with pipelining in C.pdf
  The goal of this assignment is to get familiar with pipelining in C.pdf  The goal of this assignment is to get familiar with pipelining in C.pdf
The goal of this assignment is to get familiar with pipelining in C.pdfmdualudin007
 
( 20 points) Benzene is a common groundwater contaminant. Based on a .pdf
 ( 20 points) Benzene is a common groundwater contaminant. Based on a .pdf ( 20 points) Benzene is a common groundwater contaminant. Based on a .pdf
( 20 points) Benzene is a common groundwater contaminant. Based on a .pdfmdualudin007
 
(2) Solve the following recurrence relation by using Recursion Trees .pdf
 (2) Solve the following recurrence relation by using Recursion Trees .pdf (2) Solve the following recurrence relation by using Recursion Trees .pdf
(2) Solve the following recurrence relation by using Recursion Trees .pdfmdualudin007
 
(20 pts) Let X and Y be two statistically independent random variable.pdf
 (20 pts) Let X and Y be two statistically independent random variable.pdf (20 pts) Let X and Y be two statistically independent random variable.pdf
(20 pts) Let X and Y be two statistically independent random variable.pdfmdualudin007
 
(2 pts.) Consider the following ordered list of data, x and a key.pdf
 (2 pts.) Consider the following ordered list of data,  x  and a key.pdf (2 pts.) Consider the following ordered list of data,  x  and a key.pdf
(2 pts.) Consider the following ordered list of data, x and a key.pdfmdualudin007
 
(2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdf
 (2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdf (2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdf
(2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdfmdualudin007
 

Mehr von mdualudin007 (20)

(5 points 1. Which of the followhy statements munt be trie 2. What i.pdf
 (5 points 1. Which of the followhy statements munt be trie 2. What i.pdf (5 points 1. Which of the followhy statements munt be trie 2. What i.pdf
(5 points 1. Which of the followhy statements munt be trie 2. What i.pdf
 
(4) With regard to virulence factors, what do the letters O,H and K.pdf
 (4) With regard to virulence factors, what do the letters O,H and K.pdf (4) With regard to virulence factors, what do the letters O,H and K.pdf
(4) With regard to virulence factors, what do the letters O,H and K.pdf
 
(4 points) Match the following data with the correct level of measure.pdf
 (4 points) Match the following data with the correct level of measure.pdf (4 points) Match the following data with the correct level of measure.pdf
(4 points) Match the following data with the correct level of measure.pdf
 
(3) If events occur accordiy to a poidson porles with hate =10minnt..pdf
 (3) If events occur accordiy to a poidson porles with hate =10minnt..pdf (3) If events occur accordiy to a poidson porles with hate =10minnt..pdf
(3) If events occur accordiy to a poidson porles with hate =10minnt..pdf
 
(4 marks) Suppose the production function of the fishing industry is .pdf
 (4 marks) Suppose the production function of the fishing industry is .pdf (4 marks) Suppose the production function of the fishing industry is .pdf
(4 marks) Suppose the production function of the fishing industry is .pdf
 
(3 points) Employee is a derived class of Person, and Worker is a der.pdf
 (3 points) Employee is a derived class of Person, and Worker is a der.pdf (3 points) Employee is a derived class of Person, and Worker is a der.pdf
(3 points) Employee is a derived class of Person, and Worker is a der.pdf
 
(1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdf
 (1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdf (1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdf
(1 point) Suppose that the demand of a certain item is x=10+p21. Eval.pdf
 
() Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdf
 () Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdf () Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdf
() Source Monthly Bulletins of the Central Bank of Venezuela. All l.pdf
 
(1 Point) Please match the following variable properties with their d.pdf
 (1 Point) Please match the following variable properties with their d.pdf (1 Point) Please match the following variable properties with their d.pdf
(1 Point) Please match the following variable properties with their d.pdf
 
(1 point) If samples of size 37 are taken from a population with mean.pdf
 (1 point) If samples of size 37 are taken from a population with mean.pdf (1 point) If samples of size 37 are taken from a population with mean.pdf
(1 point) If samples of size 37 are taken from a population with mean.pdf
 
Bag breaks open; included as delay in the allowance factor Conveyo.pdf
  Bag breaks open; included as delay in the allowance factor  Conveyo.pdf  Bag breaks open; included as delay in the allowance factor  Conveyo.pdf
Bag breaks open; included as delay in the allowance factor Conveyo.pdf
 
Prove the following E(MSR)=2+12(Xix)2.pdf
  Prove the following E(MSR)=2+12(Xix)2.pdf  Prove the following E(MSR)=2+12(Xix)2.pdf
Prove the following E(MSR)=2+12(Xix)2.pdf
 
Computer Architecture HOW do I design a computer = Instruction .pdf
  Computer Architecture HOW do I design a computer  = Instruction .pdf  Computer Architecture HOW do I design a computer  = Instruction .pdf
Computer Architecture HOW do I design a computer = Instruction .pdf
 
( Ch2 ISA)3. Assume that the top of the stack in a progra.pdf
 ( Ch2 ISA)3. Assume that the top of the stack in a progra.pdf ( Ch2 ISA)3. Assume that the top of the stack in a progra.pdf
( Ch2 ISA)3. Assume that the top of the stack in a progra.pdf
 
The goal of this assignment is to get familiar with pipelining in C.pdf
  The goal of this assignment is to get familiar with pipelining in C.pdf  The goal of this assignment is to get familiar with pipelining in C.pdf
The goal of this assignment is to get familiar with pipelining in C.pdf
 
( 20 points) Benzene is a common groundwater contaminant. Based on a .pdf
 ( 20 points) Benzene is a common groundwater contaminant. Based on a .pdf ( 20 points) Benzene is a common groundwater contaminant. Based on a .pdf
( 20 points) Benzene is a common groundwater contaminant. Based on a .pdf
 
(2) Solve the following recurrence relation by using Recursion Trees .pdf
 (2) Solve the following recurrence relation by using Recursion Trees .pdf (2) Solve the following recurrence relation by using Recursion Trees .pdf
(2) Solve the following recurrence relation by using Recursion Trees .pdf
 
(20 pts) Let X and Y be two statistically independent random variable.pdf
 (20 pts) Let X and Y be two statistically independent random variable.pdf (20 pts) Let X and Y be two statistically independent random variable.pdf
(20 pts) Let X and Y be two statistically independent random variable.pdf
 
(2 pts.) Consider the following ordered list of data, x and a key.pdf
 (2 pts.) Consider the following ordered list of data,  x  and a key.pdf (2 pts.) Consider the following ordered list of data,  x  and a key.pdf
(2 pts.) Consider the following ordered list of data, x and a key.pdf
 
(2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdf
 (2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdf (2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdf
(2 points) (Problem 4.96) Suppose that a random variable Y has a prob.pdf
 

Kürzlich hochgeladen

Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 

Kürzlich hochgeladen (20)

Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 

Click nbfsnbhostSystemFileSystemTemplatesLicenseslice.pdf

  • 1. /* * 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},
  • 2. {"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);
  • 3. // 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. Continue 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;
  • 4. 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 {. // 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
  • 5. if (type == 2) { data_segment; // Writing value to data segments data_segment[data_offset] = value; data_offset += 1; } // Handling .integer if (type == 3) { // Writing value to data segment data_segment[data_offset] = value; data_offset += 1; } token = strtok(NULL, ","); } } } } fclose(fp); } // Getting MIPS instruction Instruction* getInstruction(char* instruction) { int i = 0; while (instructions[i].name) { if (strcmp(instructions[i].name, instruction) == 0) { return &instructions[i]; } i++; } return NULL; } // Getting MIPS directive Directive* getDirective(char* directive) { int i = 0; while (directives[i].name) { if (strcmp(directives[i].name, continue return &directives[i];
  • 6. } i++; } return NULL; } // Getting register number int getRegisterNumber(char* register) { int reg = 0; sscanf(register, "$%d", &reg); return reg; } // Parsing instruction int parseInstruction(char* instruction, int* opcode, int* funct) { Instruction* instr = getInstruction(instruction); if (instr) { *opcode = instr->opcode; *funct = instr->funct; return 1; } else { return 0; } } // Parsing directive int parseDirective(char* directive, int* type, int* size) { Directive* dir = getDirective(directive); if (dir) { *type = dir->n; *size = dir->size; return 1; } else { return 0; } } not sure why its compiling errors. i'm running in netbeans