Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
*.out
*.exe
compilador
compiler
AssemblyCode.obj
/bin
all
.vscode
output.txt
log.txt
log.txt
vm
114 changes: 68 additions & 46 deletions AssemblyCode.obj
Original file line number Diff line number Diff line change
@@ -1,65 +1,87 @@
START
ALLOC 3 1
ALLOC 0 1
ALLOC 1 2
JMP 1
2 NULL
ALLOC 4 4
ALLOC 3 3
JMP 3
4 NULL
ALLOC 1 8
ALLOC 6 2
RD
STR 4
STR 6
LDV 6
PRN
RD
STR 7
LDV 6
LDV 7
LDC 10
CME
ADD
LDC 100
CMA
JMPF 5
CALL 4
LDC 1
STR 0
JMP 6
5 NULL
LDC 0
STR 0
6 NULL
RD
STR 5
LDV 5
LDC 1
ADD
LDC 2
LDC 3
MULT
SUB
STR 6
LDC 1
LDC 1
ADD
STR 5
LDV 6
LDV 7
ADD
STR 2
LDV 2
PRN
RETURN
8 NULL
ALLOC 1 9
DALLOC 6 2
RETURNF
3 NULL
RD
STR 9
LDV 9
LDC 10
INV
CME
JMPF 9
STR 5
RD
STR 3
RD
STR 4
CALL 4
JMP 10
10 NULL
RETURN
3 NULL
CALL 8
LDV 0
JMPF 7
LDV 4
INV
LDV 7
SUB
PRN
JMP 8
7 NULL
LDC 0
STR 2
STR 0
DALLOC 5 9
RETURNF
8 NULL
LDV 3
LDV 4
MULT
LDV 2
SUB
STR 1
DALLOC 3 3
RETURN
1 NULL
CALL 2
JMP 2
STR 3
LDV 3
LDV 1
LDC 1500
CMA
JMPF 9
LDV 1
PRN
DALLOC 4 4
HLT
JMP 10
9 NULL
LDV 1
LDV 2
CMA
JMPF 11
LDC 1000
STR 1
LDV 1
PRN
JMP 12
11 NULL
LDV 1
PRN
12 NULL
10 NULL
DALLOC 0 3
HLT
66 changes: 66 additions & 0 deletions VirtualMachine/instruction.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "instruction.h"


void insertInstruction(InstructionVector *instructionVector, char inst[21], int *InstructionAddr) {
InstructionVector novo;

for(int i = 0, j = 0; i < 20; i++) {
if(i == 4 || i == 12 || i == 16 || i == 20){
j = 0;
}
if(i < 4)
novo.label[j++] = inst[i];
else if(i < 12)
novo.instruction[j++] = inst[i];
else if(i < 16)
novo.param1[j++] = inst[i];
else if(i < 20)
novo.param2[j++] = inst[i];
}
novo.label[4] = '\0';
novo.instruction[8] = '\0';
novo.param1[4] = '\0';
novo.param2[4] = '\0';
instructionVector[(*InstructionAddr)] = novo;
(*InstructionAddr) = *InstructionAddr + 1;
}

int toInt(char *number) {
int result = 0;
for(int i = 0; number[i] >= 48 && number[i] <= 57; i++) {
result += number[i] - 48;
result *= 10;
}
result /= 10;
return result;
}

void printInstructions(InstructionVector *instructions, int size) {
printf("INSTRUCTIONS\n");
for(int i = 0; i < size; i++)
printf("LABEL: %4.4s\t INST: %8.8s\tPARAM1: %4.4s\tPARAM2: %4.4s\n", instructions[i].label, instructions[i].instruction, instructions[i].param1, instructions[i].param2);
printf("\n");
}


void readInstructions(FILE *file, InstructionVector *instructionVector, int *InstructionAddr) {
char instruction[22];
while(!feof(file)){
fgets(instruction, 22, file);
//printf("%s {SIZE = %d}", instruction, strlen(instruction));
insertInstruction(instructionVector, instruction, InstructionAddr);
}
}

// void freeInstructions(InstructionVector **instructionVector) {
// InstructionVector *aux = *instructionVector, *aux2 = NULL;
// while(aux != NULL) {
// aux2 = aux->next;
// free(aux);
// aux = aux2;
// }
// *instructionVector = NULL;
// }
25 changes: 25 additions & 0 deletions VirtualMachine/instruction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef _INSTRUCTION_H_
#define _INSTRUCTION_H_

#include <stdio.h>
#include <stdlib.h>

typedef struct instructionVector {
char label[5];
char instruction[9];
char param1[5];
char param2[5];
}InstructionVector;

void insertInstruction(InstructionVector *instructionVector, char inst[21], int *InstructionAddr);

void printInstructions(InstructionVector *instructions, int size);

// Function which collect all instructions from object to execute
void readInstructions(FILE *file, InstructionVector *instructionVector, int *InstructionAddr);

int toInt(char *number);

void freeInstructions(InstructionVector **instructionVector);

#endif
Loading