SlideShare a Scribd company logo
The Problem
Using C programming language write a program that simulates the Tiny Machine Architecture.
Your code must implement the basic instruction set that the Tiny Machine Architecture adheres
to (LOAD[1], ADD[2], STORE[3], SUB[4], IN[5], OUT[6], END[7], JMP[8], SKIPZ[9]). Each
piece of the architecture must be accurately represented in your code (Instruction Register,
Program Counter, Memory Address Register, Data Memory, Memory Data Register, and
Accumulator). Data Memory will be represented by a 0-9 array. Your Program Counter will
begin at 10.
For the sake of simplicity Program Memory and Data Memory may be implemented as separate
arrays.
Hint: Implementing a struct for your Instructions and an array of these structs as your Program
Memory greatly simplifies this program.
Example:
typedef struct {
int opCode, deviceOrAddress;
} Instruction;
Instruction programMemory[MAXPROGRAMSIZE];
Input Specifications
Your simulator must run from the command line with a single input file as a parameter to main.
This file will contain a sequence of instructions for your simulator to assemble (store in
“program memory”) and then run via the fetch/execute cycle.
Example:
5 5 //IN 5
6 7 //OUT 7
3 0 //STORE 0
5 5 //IN 5
6 7 //OUT 7
3 1 //STORE 1
1 0 //LOAD 0
4 1 //SUB 1
3 0 //STORE 0
6 7 //OUT 7
1 1 //LOAD 1
6 7 //OUT 7
7 0 //END
Output Specifications
Your simulator should provide output according to the input file. Along with this output your
program should provide status messages identifying details on the workings of your simulator.
Output text does not have to reflect my example word-for-word, but please provide detail on the
program as it runs in a readable format that does not conflict with the actual output of your
simulator. After each instruction print the current state of the Program Counter, Accumulator,
and Data Memory. The INPUT instruction is the only one that should prompt an interaction from
the user.
Example:
Assembling Program…
Program Assembled.
Run.
PC = 10 | A = NULL | MEM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
/* input value */
X
PC = 11 | A = X | MEM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
/* outputting accumulator to screen */
X
PC = 12 | A = X | MEM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
/* storing accumulator to memory location 0 */
PC = 13 | A = X | MEM = [X, 0, 0, 0, 0, 0, 0, 0, 0, 0]
… etc
Program complete.
Grading
Your simulator will be graded on the above criteria. Your program should compile and run from
the command line with one input file parameter. Please note that your program will not just be
graded on whether or not it runs successfully; accurate simulation and a thorough demonstration
of your understanding on the workings of this architecture will constitute a large portion of this
grade. As that is the case it is in your best interest to comment your program in a concise and
readable way. However, if your program does not run or compile the maximum points possible
will be 50 (up to 25 may be recovered by debugging and demonstrating an understanding of your
errors during TA’s office hours).
For instance, to implement FETCH and instruction LOAD you must implement each step:
FETCH
MAR ß PC
PC ß PC + 1
MDR ß M [MAR]
IR ß MDR
LOAD (Execute cycle)
MAR ß IR.ADDR */
MDR ß MEM[MAR] */
A ß MDR */
Note: Lecture 1 describes the instruction set architecture of the Tiny Machine.
Submission
Your program must be submitted as a C file. For example: NameMyProgram.c
Please check and double check your submission.
Solution
#include
#include
#include
#include
//Function used to count the amount of lines in text file
int getProgramSize();
//Function used to parse instruction into machine code
void toMachine();
//Global variable used to store the aforementioned
int MAXPROGRAMSIZE;
//Tiny Machine variables
int programCounter = 10;
int instructionRegister = 0;
int memoryAddressRegister = 0;
int dataMemory[9];
int memoryDataRegister = 0;
int accumulator = 0;
//Creating struct
typedef struct
{
int opCode;
int deviceOrAddress;
}Instruction;
int main(int argc, char *argv[])
{
//This is where we get the size of the program and define it
MAXPROGRAMSIZE = getProgramSize(fopen(argv[argc-1], "r"));
//This creates our struct array based on the amount of instructions
Instruction programMemory[MAXPROGRAMSIZE];
//Reading a file line by line
FILE *file = fopen(argv[argc-1], "r");
char fileBuffer[MAXPROGRAMSIZE];
int i = 0;
if (file == NULL)
{
printf("Error opening file!");
exit(0);
}
//Had to do some manipulation and checks here to make sure I only store the 1st and 3rd char
of every line
while(fgets(fileBuffer, sizeof(MAXPROGRAMSIZE), file)!=NULL)
{
if ((*fileBuffer == ' ' || *fileBuffer != ' ') && isdigit(*fileBuffer) && (int)fileBuffer[2]
!= 0)
{
programMemory[i].opCode = atoi(&fileBuffer[0]);
programMemory[i].deviceOrAddress = atoi(&fileBuffer[2]);
i += 1;
}
}
//Need to close the file
fclose(file);
//Just some text output
printf("  Otis Diaz --- CGS3269 ============================== Tiny Machine
Simulator ==============================  ");
printf("Assembling program...  ");
printf("Program assembled  ");
//Get the instruction from our struct and figure out what they mean by passing it into our
parser function
for (i=(programCounter/10)-1; i> Loading from address [%d]... << ", b);
instructionRegister = b;
memoryAddressRegister = instructionRegister;
memoryDataRegister = dataMemory[memoryAddressRegister];
accumulator = memoryDataRegister;
printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator);
for (i=0; i<9; i+=1)
{ printf("%d,", dataMemory[i]); }
printf("] ");
programCounter += 1;
break;
case 2:
//ADD
printf(" >> Adding accumulator and value obtain from address [%d]... << ", b);
instructionRegister = b;
memoryAddressRegister = instructionRegister;
memoryDataRegister = dataMemory[memoryAddressRegister];
accumulator += memoryDataRegister;
printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator);
for (i=0; i<9; i+=1)
{ printf("%d,", dataMemory[i]); }
printf("] ");
programCounter += 1;
break;
case 3:
//STORE
printf(" >> Storing accumulator value into memory... << ");
memoryDataRegister = accumulator;
instructionRegister = b;
memoryAddressRegister = instructionRegister;
dataMemory[memoryAddressRegister] = memoryDataRegister;
printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator);
for (i=0; i<9; i+=1)
{ printf("%d,", dataMemory[i]); }
printf("] ");
programCounter += 1;
break;
case 4:
//SUB
printf(" >> Subtracting memory address value [%d] from accumulator... << ", b);
instructionRegister = b;
memoryAddressRegister = instructionRegister;
memoryDataRegister = dataMemory[memoryAddressRegister];
accumulator -= memoryDataRegister;
printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator);
for (i=0; i<9; i+=1)
{ printf("%d,", dataMemory[i]); }
printf("] ");
programCounter += 1;
break;
case 5:
//GET INPUT
printf(" Please input a number: ");
scanf("%d", &accumulator);
printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator);
for (i=0; i<9; i+=1)
{ printf("%d,", dataMemory[i]); }
printf("] ");
programCounter += 1;
break;
case 6:
//OUTPUT TO SCREEN
printf(" >> Accumulator current value = %d << ", accumulator);
printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator);
for (i=0; i<9; i+=1)
{ printf("%d,", dataMemory[i]); }
printf("] ");
programCounter += 1;
break;
case 7:
//END PROGRAM
printf(" Program concluded... ");
exit(1);
case 8:
//JMP
// *Jump to address
printf(" >> Setting program counter to %d... << ", b);
programCounter = b;
printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator);
for (i=0; i<9; i+=1)
{ printf("%d,", dataMemory[i]); }
printf("] ");
break;
case 9:
//SKIPZ
// *Check if acc is 0, if it is skip next instruction, else proceed as normal
printf(" >> Skipping the next instruction... << ");
if (accumulator == 0)
{ programCounter += 2; }
else
{ programCounter += 1; }
printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator);
for (i=0; i<9; i+=1)
{ printf("%d,", dataMemory[i]); }
printf("] ");
break;
default:
printf(" There was an error parsing that opcode! Exiting program ");
exit(0);
}
}
int getProgramSize(FILE *file)
{
char fileBuffer[100];
int length = 0;
if (file == NULL)
{
printf("Error opening file!");
exit(0);
}
//This is where I kinda have to buck down and choose an arbitrary number... 1000 might be
overkill but w/e
while(fgets(fileBuffer, 1000 ,file)!=NULL)
{ length += 1; }
fclose(file);
return length;
}

More Related Content

Similar to The ProblemUsing C programming language write a program that simul.pdf (20)

PPTX
Computer arch
Rahul Bhaskar
 
PDF
mca is a microcontroller and accmulator is a third year couse
gauravsharma4365
 
PPT
8085_Microprocessor(simar).ppt
KanikaJindal9
 
PPT
Register Transfer Language and Micro Operations
KAVITHA N S
 
PDF
02 isa
marangburu42
 
PPT
Lect05 Prog Model
anoosdomain
 
PPTX
Basic computer organization design
ndasharath
 
PPTX
1st unit - microcontroller architechture and pin diagram
gokikayal1998
 
PDF
Microprocessor 8086-lab-mannual
yeshwant gadave
 
RTF
Microprocessor File
Sourabh Bhattacharya
 
PPT
intro.ppt
NISHASOMSCS113
 
PPT
Chapter Eight(3)
bolovv
 
PPTX
CH-3 CO-all-about-operating-system(Update).pptx
XyzXyz338506
 
PPT
C Language Unit-1
kasaragadda srinivasrao
 
PPT
Unit1 jwfiles
mrecedu
 
PDF
Lenguaje ensamblador EMU8086
Santy Bolo
 
PPT
Microprocessor system - summarize
Hisham Mat Hussin
 
PPT
8051assembly language
Hisham Mat Hussin
 
PDF
8051 microcontroller
Thirunavakkarasu kannusamy
 
PPTX
Intro (lesson1)comp arch
Roger Duran
 
Computer arch
Rahul Bhaskar
 
mca is a microcontroller and accmulator is a third year couse
gauravsharma4365
 
8085_Microprocessor(simar).ppt
KanikaJindal9
 
Register Transfer Language and Micro Operations
KAVITHA N S
 
02 isa
marangburu42
 
Lect05 Prog Model
anoosdomain
 
Basic computer organization design
ndasharath
 
1st unit - microcontroller architechture and pin diagram
gokikayal1998
 
Microprocessor 8086-lab-mannual
yeshwant gadave
 
Microprocessor File
Sourabh Bhattacharya
 
intro.ppt
NISHASOMSCS113
 
Chapter Eight(3)
bolovv
 
CH-3 CO-all-about-operating-system(Update).pptx
XyzXyz338506
 
C Language Unit-1
kasaragadda srinivasrao
 
Unit1 jwfiles
mrecedu
 
Lenguaje ensamblador EMU8086
Santy Bolo
 
Microprocessor system - summarize
Hisham Mat Hussin
 
8051assembly language
Hisham Mat Hussin
 
8051 microcontroller
Thirunavakkarasu kannusamy
 
Intro (lesson1)comp arch
Roger Duran
 

More from federaleyecare (20)

PDF
A ISCSI SAN consists of Select one or more A. HBA B. LUN C. Fibr.pdf
federaleyecare
 
PDF
Which of the following statements about tRNA molecules is TRUEMul.pdf
federaleyecare
 
PDF
Write a C program to simulate the description below using semaphores.pdf
federaleyecare
 
PDF
When analyzing any particular primary source, historians must consid.pdf
federaleyecare
 
PDF
What is Linux SecuritySolutionLinux Security is a module in.pdf
federaleyecare
 
PDF
What is the runtime complexity of Adams famous string splitter cod.pdf
federaleyecare
 
PDF
What does personalized learning mean to you What does personal.pdf
federaleyecare
 
PDF
What do patients in healthcare expect from their insurance company.pdf
federaleyecare
 
PDF
Use Table A to find the number z such that the proportion of observa.pdf
federaleyecare
 
PDF
Two of the main political systems are democracy and totalitarianism..pdf
federaleyecare
 
PDF
The procedure of transferring journal entries to the ledger accounts .pdf
federaleyecare
 
PDF
5. Joe and Sam are using Public Key encryption. Joe’s public and pri.pdf
federaleyecare
 
PDF
QUESTION 7 When using the indirect method to prepare the statement of.pdf
federaleyecare
 
PDF
Q.1. Define noncontrolling (minority) interest. List three methods th.pdf
federaleyecare
 
PDF
Picking a fruit-flavored or primary colored bean. overlapping event .pdf
federaleyecare
 
PDF
Part F - Viral Evolution and LysogenyOne hypothesis regarding the .pdf
federaleyecare
 
PDF
Match the following Data vs Information Reliability as it adds value .pdf
federaleyecare
 
PDF
Many organizations invest substantial resources in creating their ow.pdf
federaleyecare
 
PDF
Lets compare and contrast the national cultures of Egypt and Brazi.pdf
federaleyecare
 
PDF
Great alveolar (type II) cells of the alveoli secrete a _______ t.pdf
federaleyecare
 
A ISCSI SAN consists of Select one or more A. HBA B. LUN C. Fibr.pdf
federaleyecare
 
Which of the following statements about tRNA molecules is TRUEMul.pdf
federaleyecare
 
Write a C program to simulate the description below using semaphores.pdf
federaleyecare
 
When analyzing any particular primary source, historians must consid.pdf
federaleyecare
 
What is Linux SecuritySolutionLinux Security is a module in.pdf
federaleyecare
 
What is the runtime complexity of Adams famous string splitter cod.pdf
federaleyecare
 
What does personalized learning mean to you What does personal.pdf
federaleyecare
 
What do patients in healthcare expect from their insurance company.pdf
federaleyecare
 
Use Table A to find the number z such that the proportion of observa.pdf
federaleyecare
 
Two of the main political systems are democracy and totalitarianism..pdf
federaleyecare
 
The procedure of transferring journal entries to the ledger accounts .pdf
federaleyecare
 
5. Joe and Sam are using Public Key encryption. Joe’s public and pri.pdf
federaleyecare
 
QUESTION 7 When using the indirect method to prepare the statement of.pdf
federaleyecare
 
Q.1. Define noncontrolling (minority) interest. List three methods th.pdf
federaleyecare
 
Picking a fruit-flavored or primary colored bean. overlapping event .pdf
federaleyecare
 
Part F - Viral Evolution and LysogenyOne hypothesis regarding the .pdf
federaleyecare
 
Match the following Data vs Information Reliability as it adds value .pdf
federaleyecare
 
Many organizations invest substantial resources in creating their ow.pdf
federaleyecare
 
Lets compare and contrast the national cultures of Egypt and Brazi.pdf
federaleyecare
 
Great alveolar (type II) cells of the alveoli secrete a _______ t.pdf
federaleyecare
 
Ad

Recently uploaded (20)

PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Ad

The ProblemUsing C programming language write a program that simul.pdf

  • 1. The Problem Using C programming language write a program that simulates the Tiny Machine Architecture. Your code must implement the basic instruction set that the Tiny Machine Architecture adheres to (LOAD[1], ADD[2], STORE[3], SUB[4], IN[5], OUT[6], END[7], JMP[8], SKIPZ[9]). Each piece of the architecture must be accurately represented in your code (Instruction Register, Program Counter, Memory Address Register, Data Memory, Memory Data Register, and Accumulator). Data Memory will be represented by a 0-9 array. Your Program Counter will begin at 10. For the sake of simplicity Program Memory and Data Memory may be implemented as separate arrays. Hint: Implementing a struct for your Instructions and an array of these structs as your Program Memory greatly simplifies this program. Example: typedef struct { int opCode, deviceOrAddress; } Instruction; Instruction programMemory[MAXPROGRAMSIZE]; Input Specifications Your simulator must run from the command line with a single input file as a parameter to main. This file will contain a sequence of instructions for your simulator to assemble (store in “program memory”) and then run via the fetch/execute cycle. Example: 5 5 //IN 5 6 7 //OUT 7 3 0 //STORE 0 5 5 //IN 5 6 7 //OUT 7 3 1 //STORE 1 1 0 //LOAD 0 4 1 //SUB 1 3 0 //STORE 0 6 7 //OUT 7 1 1 //LOAD 1
  • 2. 6 7 //OUT 7 7 0 //END Output Specifications Your simulator should provide output according to the input file. Along with this output your program should provide status messages identifying details on the workings of your simulator. Output text does not have to reflect my example word-for-word, but please provide detail on the program as it runs in a readable format that does not conflict with the actual output of your simulator. After each instruction print the current state of the Program Counter, Accumulator, and Data Memory. The INPUT instruction is the only one that should prompt an interaction from the user. Example: Assembling Program… Program Assembled. Run. PC = 10 | A = NULL | MEM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] /* input value */ X PC = 11 | A = X | MEM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] /* outputting accumulator to screen */ X PC = 12 | A = X | MEM = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] /* storing accumulator to memory location 0 */ PC = 13 | A = X | MEM = [X, 0, 0, 0, 0, 0, 0, 0, 0, 0] … etc Program complete. Grading Your simulator will be graded on the above criteria. Your program should compile and run from the command line with one input file parameter. Please note that your program will not just be graded on whether or not it runs successfully; accurate simulation and a thorough demonstration of your understanding on the workings of this architecture will constitute a large portion of this grade. As that is the case it is in your best interest to comment your program in a concise and readable way. However, if your program does not run or compile the maximum points possible
  • 3. will be 50 (up to 25 may be recovered by debugging and demonstrating an understanding of your errors during TA’s office hours). For instance, to implement FETCH and instruction LOAD you must implement each step: FETCH MAR ß PC PC ß PC + 1 MDR ß M [MAR] IR ß MDR LOAD (Execute cycle) MAR ß IR.ADDR */ MDR ß MEM[MAR] */ A ß MDR */ Note: Lecture 1 describes the instruction set architecture of the Tiny Machine. Submission Your program must be submitted as a C file. For example: NameMyProgram.c Please check and double check your submission. Solution #include #include #include #include //Function used to count the amount of lines in text file int getProgramSize(); //Function used to parse instruction into machine code void toMachine(); //Global variable used to store the aforementioned int MAXPROGRAMSIZE; //Tiny Machine variables int programCounter = 10; int instructionRegister = 0; int memoryAddressRegister = 0; int dataMemory[9];
  • 4. int memoryDataRegister = 0; int accumulator = 0; //Creating struct typedef struct { int opCode; int deviceOrAddress; }Instruction; int main(int argc, char *argv[]) { //This is where we get the size of the program and define it MAXPROGRAMSIZE = getProgramSize(fopen(argv[argc-1], "r")); //This creates our struct array based on the amount of instructions Instruction programMemory[MAXPROGRAMSIZE]; //Reading a file line by line FILE *file = fopen(argv[argc-1], "r"); char fileBuffer[MAXPROGRAMSIZE]; int i = 0; if (file == NULL) { printf("Error opening file!"); exit(0); } //Had to do some manipulation and checks here to make sure I only store the 1st and 3rd char of every line while(fgets(fileBuffer, sizeof(MAXPROGRAMSIZE), file)!=NULL) { if ((*fileBuffer == ' ' || *fileBuffer != ' ') && isdigit(*fileBuffer) && (int)fileBuffer[2] != 0) { programMemory[i].opCode = atoi(&fileBuffer[0]); programMemory[i].deviceOrAddress = atoi(&fileBuffer[2]); i += 1; } } //Need to close the file
  • 5. fclose(file); //Just some text output printf(" Otis Diaz --- CGS3269 ============================== Tiny Machine Simulator ============================== "); printf("Assembling program... "); printf("Program assembled "); //Get the instruction from our struct and figure out what they mean by passing it into our parser function for (i=(programCounter/10)-1; i> Loading from address [%d]... << ", b); instructionRegister = b; memoryAddressRegister = instructionRegister; memoryDataRegister = dataMemory[memoryAddressRegister]; accumulator = memoryDataRegister; printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator); for (i=0; i<9; i+=1) { printf("%d,", dataMemory[i]); } printf("] "); programCounter += 1; break; case 2: //ADD printf(" >> Adding accumulator and value obtain from address [%d]... << ", b); instructionRegister = b; memoryAddressRegister = instructionRegister; memoryDataRegister = dataMemory[memoryAddressRegister]; accumulator += memoryDataRegister; printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator); for (i=0; i<9; i+=1) { printf("%d,", dataMemory[i]); } printf("] "); programCounter += 1; break; case 3: //STORE printf(" >> Storing accumulator value into memory... << "); memoryDataRegister = accumulator;
  • 6. instructionRegister = b; memoryAddressRegister = instructionRegister; dataMemory[memoryAddressRegister] = memoryDataRegister; printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator); for (i=0; i<9; i+=1) { printf("%d,", dataMemory[i]); } printf("] "); programCounter += 1; break; case 4: //SUB printf(" >> Subtracting memory address value [%d] from accumulator... << ", b); instructionRegister = b; memoryAddressRegister = instructionRegister; memoryDataRegister = dataMemory[memoryAddressRegister]; accumulator -= memoryDataRegister; printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator); for (i=0; i<9; i+=1) { printf("%d,", dataMemory[i]); } printf("] "); programCounter += 1; break; case 5: //GET INPUT printf(" Please input a number: "); scanf("%d", &accumulator); printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator); for (i=0; i<9; i+=1) { printf("%d,", dataMemory[i]); } printf("] "); programCounter += 1; break; case 6: //OUTPUT TO SCREEN printf(" >> Accumulator current value = %d << ", accumulator); printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator);
  • 7. for (i=0; i<9; i+=1) { printf("%d,", dataMemory[i]); } printf("] "); programCounter += 1; break; case 7: //END PROGRAM printf(" Program concluded... "); exit(1); case 8: //JMP // *Jump to address printf(" >> Setting program counter to %d... << ", b); programCounter = b; printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator); for (i=0; i<9; i+=1) { printf("%d,", dataMemory[i]); } printf("] "); break; case 9: //SKIPZ // *Check if acc is 0, if it is skip next instruction, else proceed as normal printf(" >> Skipping the next instruction... << "); if (accumulator == 0) { programCounter += 2; } else { programCounter += 1; } printf(" PC: %d | A: %d | MEM: [", programCounter, accumulator); for (i=0; i<9; i+=1) { printf("%d,", dataMemory[i]); } printf("] "); break; default: printf(" There was an error parsing that opcode! Exiting program "); exit(0); }
  • 8. } int getProgramSize(FILE *file) { char fileBuffer[100]; int length = 0; if (file == NULL) { printf("Error opening file!"); exit(0); } //This is where I kinda have to buck down and choose an arbitrary number... 1000 might be overkill but w/e while(fgets(fileBuffer, 1000 ,file)!=NULL) { length += 1; } fclose(file); return length; }