Compare commits
4 commits
Author | SHA1 | Date | |
---|---|---|---|
0e9455b376 | |||
84c51d9446 | |||
|
2fbae7f7fe | ||
|
6631ca437b |
17 changed files with 101 additions and 4015 deletions
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
*.o
|
||||
lex.yy.*
|
||||
as.tab.*
|
||||
*.asm
|
||||
!input.asm
|
||||
*.txt
|
||||
cross_assembleur
|
||||
|
31
Makefile
31
Makefile
|
@ -1,7 +1,24 @@
|
|||
build :
|
||||
gcc -Wall -c tables.c -o tables.o
|
||||
bison -d -t as.y
|
||||
flex -o lex.yy.c al.lex
|
||||
gcc -Wall -c as.tab.c -o as.tab.o
|
||||
gcc -Wall -c lex.yy.c -o lex.yy.o
|
||||
gcc as.tab.o lex.yy.o tables.o -ll -o rondoudou_cross_assembleur
|
||||
LEX = flex
|
||||
YACC = bison -d
|
||||
CC = gcc
|
||||
|
||||
cross_assembleur: tables.o as.tab.o lex.yy.o
|
||||
$(CC) as.tab.o lex.yy.o tables.o -o cross_assembleur
|
||||
|
||||
tables.o: tables.c tables.h
|
||||
$(CC) -Wall -c tables.c -o tables.o
|
||||
|
||||
as.tab.c: as.y
|
||||
$(YACC) as.y
|
||||
|
||||
lex.yy.c: al.lex
|
||||
$(LEX) al.lex
|
||||
|
||||
as.tab.o: as.tab.c
|
||||
$(CC) -Wall -c as.tab.c -o as.tab.o
|
||||
|
||||
lex.yy.o: lex.yy.c
|
||||
$(CC) -Wall -c lex.yy.c -o lex.yy.o
|
||||
|
||||
clean:
|
||||
rm -f *.o lex.yy.c lex.yy.h as.tab.c cross_assembleur
|
||||
|
|
24
al.lex
24
al.lex
|
@ -13,32 +13,14 @@ yyerror (char const *s)
|
|||
%%
|
||||
|
||||
"ADD" { return tADD ;}
|
||||
"SOU" { return tSUB;}
|
||||
"MUL" { return tMUL; }
|
||||
"DIV" { return tDIV; }
|
||||
"INF" { return tINF; }
|
||||
"SUP" { return tSUP; }
|
||||
"EQU" { return tEQU; }
|
||||
"SOU" { return tSUB;}
|
||||
"DIV" { return tDIV; }
|
||||
|
||||
"AFC" { return tAFC; }
|
||||
"COP" { return tCPY; }
|
||||
"AFCA" { return tAFCA; }
|
||||
"AFC" { return tAFC; }
|
||||
|
||||
|
||||
"READ" { return tREAD; }
|
||||
"WR" { return tWR; }
|
||||
|
||||
"JMP" { return tJMP; }
|
||||
"JMF" { return tJMF; }
|
||||
|
||||
"GET" { return tGET; }
|
||||
"PRI" { return tPRI; }
|
||||
|
||||
"CALL" { return tCALL; }
|
||||
"RET" { return tRET; }
|
||||
|
||||
"STOP" { return tSTOP; }
|
||||
|
||||
[0-9]+ { yylval.nombre = atoi(yytext); return tNB; }
|
||||
|
||||
"\n" {}
|
||||
|
|
93
as.tab.h
93
as.tab.h
|
@ -1,93 +0,0 @@
|
|||
/* A Bison parser, made by GNU Bison 3.0.4. */
|
||||
|
||||
/* Bison interface for Yacc-like parsers in C
|
||||
|
||||
Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
/* As a special exception, you may create a larger work that contains
|
||||
part or all of the Bison parser skeleton and distribute that work
|
||||
under terms of your choice, so long as that work isn't itself a
|
||||
parser generator using the skeleton or a modified version thereof
|
||||
as a parser skeleton. Alternatively, if you modify or redistribute
|
||||
the parser skeleton itself, you may (at your option) remove this
|
||||
special exception, which will cause the skeleton and the resulting
|
||||
Bison output files to be licensed under the GNU General Public
|
||||
License without this special exception.
|
||||
|
||||
This special exception was added by the Free Software Foundation in
|
||||
version 2.2 of Bison. */
|
||||
|
||||
#ifndef YY_YY_AS_TAB_H_INCLUDED
|
||||
# define YY_YY_AS_TAB_H_INCLUDED
|
||||
/* Debug traces. */
|
||||
#ifndef YYDEBUG
|
||||
# define YYDEBUG 1
|
||||
#endif
|
||||
#if YYDEBUG
|
||||
extern int yydebug;
|
||||
#endif
|
||||
|
||||
/* Token type. */
|
||||
#ifndef YYTOKENTYPE
|
||||
# define YYTOKENTYPE
|
||||
enum yytokentype
|
||||
{
|
||||
tMUL = 258,
|
||||
tDIV = 259,
|
||||
tADD = 260,
|
||||
tSUB = 261,
|
||||
tINF = 262,
|
||||
tSUP = 263,
|
||||
tEQU = 264,
|
||||
tAFC = 265,
|
||||
tCPY = 266,
|
||||
tAFCA = 267,
|
||||
tREAD = 268,
|
||||
tWR = 269,
|
||||
tJMP = 270,
|
||||
tJMF = 271,
|
||||
tGET = 272,
|
||||
tPRI = 273,
|
||||
tCALL = 274,
|
||||
tRET = 275,
|
||||
tSTOP = 276,
|
||||
tNB = 277
|
||||
};
|
||||
#endif
|
||||
|
||||
/* Value type. */
|
||||
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
|
||||
|
||||
union YYSTYPE
|
||||
{
|
||||
#line 1 "as.y" /* yacc.c:1909 */
|
||||
|
||||
int nombre;
|
||||
|
||||
#line 81 "as.tab.h" /* yacc.c:1909 */
|
||||
};
|
||||
|
||||
typedef union YYSTYPE YYSTYPE;
|
||||
# define YYSTYPE_IS_TRIVIAL 1
|
||||
# define YYSTYPE_IS_DECLARED 1
|
||||
#endif
|
||||
|
||||
|
||||
extern YYSTYPE yylval;
|
||||
|
||||
int yyparse (void);
|
||||
|
||||
#endif /* !YY_YY_AS_TAB_H_INCLUDED */
|
BIN
as.tab.o
BIN
as.tab.o
Binary file not shown.
122
as.y
122
as.y
|
@ -10,13 +10,8 @@ FILE * file2;
|
|||
|
||||
%}
|
||||
|
||||
%token tMUL tDIV tADD tSUB tINF tSUP tEQU
|
||||
%token tAFC tCPY tAFCA
|
||||
%token tREAD tWR
|
||||
%token tJMP tJMF
|
||||
%token tGET tPRI
|
||||
%token tCALL tRET
|
||||
%token tSTOP
|
||||
%token tMUL tDIV tADD tSUB
|
||||
%token tAFC tCPY
|
||||
%token<nombre> tNB
|
||||
|
||||
%%
|
||||
|
@ -52,28 +47,6 @@ Instruction : tSUB tNB tNB tNB {increment_time();
|
|||
int reg_dest = get_reg_write($2, &added_instruction);
|
||||
add_instruction(SUB, reg_dest, reg_src1, reg_src2);
|
||||
new_instruction(added_instruction + 1);};
|
||||
Instruction : tINF tNB tNB tNB {increment_time();
|
||||
int added_instruction = 0;
|
||||
int reg_src1 = get_reg_read($3, &added_instruction);
|
||||
int reg_src2 = get_reg_read($4, &added_instruction);
|
||||
int reg_dest = get_reg_write($2, &added_instruction);
|
||||
add_instruction(INF, reg_dest, reg_src1, reg_src2);
|
||||
new_instruction(added_instruction + 1);};
|
||||
Instruction : tSUP tNB tNB tNB {increment_time();
|
||||
int added_instruction = 0;
|
||||
int reg_src1 = get_reg_read($3, &added_instruction);
|
||||
int reg_src2 = get_reg_read($4, &added_instruction);
|
||||
int reg_dest = get_reg_write($2, &added_instruction);
|
||||
add_instruction(SUP, reg_dest, reg_src1, reg_src2);
|
||||
new_instruction(added_instruction + 1);};
|
||||
Instruction : tEQU tNB tNB tNB {increment_time();
|
||||
int added_instruction = 0;
|
||||
int reg_src1 = get_reg_read($3, &added_instruction);
|
||||
int reg_src2 = get_reg_read($4, &added_instruction);
|
||||
int reg_dest = get_reg_write($2, &added_instruction);
|
||||
add_instruction(EQU, reg_dest, reg_src1, reg_src2);
|
||||
new_instruction(added_instruction + 1);};
|
||||
|
||||
|
||||
Instruction : tAFC tNB tNB {increment_time();
|
||||
int added_instruction = 0;
|
||||
|
@ -86,73 +59,34 @@ Instruction : tCPY tNB tNB {increment_time();
|
|||
int reg_dest = get_reg_write($2, &added_instruction);
|
||||
add_instruction(CPY, reg_dest, reg_src, 0);
|
||||
new_instruction(added_instruction + 1);};
|
||||
Instruction : tAFCA tNB tNB {increment_time();
|
||||
int added_instruction = 0;
|
||||
int reg_aux = get_reg_write(-1, &added_instruction);
|
||||
add_instruction(AFC, reg_aux, $3, 0);
|
||||
add_instruction(STOREA, $2, reg_aux, 0);
|
||||
unlink($2);
|
||||
new_instruction(added_instruction + 2);};
|
||||
|
||||
|
||||
Instruction : tJMP tNB {increment_time();
|
||||
add_instruction(JMP, $2, 0, 0);
|
||||
new_instruction(1);};
|
||||
Instruction : tJMF tNB tNB {increment_time();
|
||||
int added_instruction = 0;
|
||||
int reg_src = get_reg_read($2, &added_instruction);
|
||||
int reg_aux = get_reg_write(-1, &added_instruction);
|
||||
add_instruction(SUB, reg_aux, reg_aux, reg_src);
|
||||
add_instruction(JMZ, $3, 0, 0);
|
||||
new_instruction(added_instruction + 2);};
|
||||
|
||||
Instruction : tREAD tNB tNB {increment_time();
|
||||
int added_instruction = 0;
|
||||
int reg_addr = get_reg_read($3, &added_instruction);
|
||||
int reg_dest = get_reg_write($2, &added_instruction);
|
||||
add_instruction(LOADI, reg_dest, reg_addr, 0);
|
||||
new_instruction(added_instruction + 1);};
|
||||
Instruction : tWR tNB tNB {increment_time();
|
||||
int added_instruction = 0;
|
||||
int reg_addr = get_reg_read($2, &added_instruction);
|
||||
int reg_value = get_reg_read($3, &added_instruction);
|
||||
add_instruction(STOREI, reg_addr, reg_value, 0);
|
||||
new_instruction(added_instruction + 1);};
|
||||
|
||||
|
||||
Instruction : tGET tNB {increment_time();
|
||||
int added_instruction = 0;
|
||||
int reg_dest = get_reg_write($2, &added_instruction);
|
||||
add_instruction(GET, reg_dest, 0, 0);
|
||||
new_instruction(added_instruction + 1);};
|
||||
Instruction : tPRI tNB {increment_time();
|
||||
int added_instruction = 0;
|
||||
int reg_src = get_reg_read($2, &added_instruction);
|
||||
add_instruction(PRI, reg_src, 0, 0);
|
||||
new_instruction(added_instruction + 1);};
|
||||
|
||||
|
||||
Instruction : tCALL tNB tNB {increment_time();
|
||||
int added_instruction = flush_and_init(file);
|
||||
add_instruction(CALL, $2, $3, 0);
|
||||
new_instruction(added_instruction + 1);};
|
||||
Instruction : tRET {increment_time();
|
||||
int added_instruction = flush_and_init(file);
|
||||
add_instruction(RET, 0, 0, 0);
|
||||
new_instruction(added_instruction + 1);};
|
||||
|
||||
Instruction : tSTOP tNB {increment_time();
|
||||
add_instruction(STOP, $2, 0, 0);
|
||||
new_instruction(1);};
|
||||
|
||||
%%
|
||||
|
||||
int main(void) {
|
||||
file = fopen("output.asm", "w");
|
||||
file2 = fopen("output.bin", "w");
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char *output_filename = "output_cross";
|
||||
|
||||
if (argc >= 2) {
|
||||
output_filename = argv[1];
|
||||
}
|
||||
|
||||
char *asm_filename = malloc(strlen(output_filename) + 5);
|
||||
char *bin_filename = malloc(strlen(output_filename) + 9);
|
||||
strcpy(asm_filename, output_filename);
|
||||
strcpy(bin_filename, output_filename);
|
||||
strcat(asm_filename, ".asm");
|
||||
strcat(bin_filename, "_bin.txt");
|
||||
|
||||
file = fopen(asm_filename, "w");
|
||||
file2 = fopen(bin_filename, "w");
|
||||
init();
|
||||
yyparse();
|
||||
write_asm(file);
|
||||
write_code_machine_compact(file2);
|
||||
return 0;
|
||||
yyparse();
|
||||
flush_and_init();
|
||||
write_asm(file);
|
||||
write_code_machine(file2);
|
||||
|
||||
free(asm_filename);
|
||||
free(bin_filename);
|
||||
return 0;
|
||||
}
|
||||
|
|
9
input.asm
Normal file
9
input.asm
Normal file
|
@ -0,0 +1,9 @@
|
|||
AFC 99 2
|
||||
COP 0 99
|
||||
AFC 98 3
|
||||
AFC 97 3
|
||||
ADD 96 98 97
|
||||
ADD 95 96 0
|
||||
COP 1 95
|
||||
AFC 94 3
|
||||
COP 0 94
|
BIN
lex.yy.o
BIN
lex.yy.o
Binary file not shown.
84
output.asm
84
output.asm
|
@ -1,84 +0,0 @@
|
|||
JMP 48
|
||||
AFC 0 1
|
||||
LOAD 1 0
|
||||
CPY 2 1
|
||||
AFC 3 1
|
||||
MUL 0 3 0
|
||||
ADD 0 2 0
|
||||
LOADI 0 0
|
||||
PRI 0
|
||||
AFC 0 0
|
||||
CPY 2 1
|
||||
AFC 3 1
|
||||
MUL 0 3 0
|
||||
ADD 0 2 0
|
||||
AFC 2 14
|
||||
STOREI 0 2
|
||||
AFC 0 2
|
||||
CPY 1 0
|
||||
STORE 0 1
|
||||
STORE 1 0
|
||||
STORE 2 2
|
||||
STORE 3 3
|
||||
RET
|
||||
AFC 0 0
|
||||
LOAD 1 0
|
||||
CPY 2 1
|
||||
AFC 3 1
|
||||
MUL 0 3 0
|
||||
ADD 0 2 0
|
||||
LOADI 0 0
|
||||
PRI 0
|
||||
AFC 0 1
|
||||
CPY 2 1
|
||||
AFC 3 1
|
||||
MUL 0 3 0
|
||||
ADD 0 2 0
|
||||
AFC 2 10
|
||||
STOREI 0 2
|
||||
CPY 0 1
|
||||
STORE 1 0
|
||||
STORE 2 2
|
||||
STORE 3 3
|
||||
CALL 1 1
|
||||
AFC 0 1
|
||||
CPY 1 0
|
||||
STORE 0 1
|
||||
STORE 1 0
|
||||
RET
|
||||
AFC 0 0
|
||||
AFC 1 0
|
||||
STOREA 3 1
|
||||
AFC 1 1
|
||||
MUL 0 1 0
|
||||
LOAD 2 3
|
||||
ADD 0 2 0
|
||||
AFC 3 1
|
||||
STOREI 0 3
|
||||
AFC 2 1
|
||||
STORE 5 1
|
||||
AFC 1 0
|
||||
STOREA 4 1
|
||||
AFC 1 1
|
||||
MUL 2 1 2
|
||||
STORE 2 0
|
||||
LOAD 0 4
|
||||
ADD 2 0 2
|
||||
AFC 3 12
|
||||
STOREI 2 3
|
||||
STORE 6 1
|
||||
AFC 1 0
|
||||
STOREA 4 1
|
||||
STORE 3 2
|
||||
STORE 5 3
|
||||
CALL 23 4
|
||||
AFC 0 0
|
||||
AFC 1 0
|
||||
STOREA 5 1
|
||||
AFC 1 1
|
||||
MUL 0 1 0
|
||||
LOAD 2 5
|
||||
ADD 0 2 0
|
||||
LOADI 0 0
|
||||
PRI 0
|
||||
STOP 0
|
|
@ -1 +0,0 @@
|
|||
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101010000000000000000000000001000100000000000000000000000001100000000000000000000000000000010000000000000010000000000101000000010000001010000000000010000000000000000100000000010010000000100000001000000000111000000101000000010000000001001000000010000000000000000010010000000000000000000000001001100010111000001000000000001011000001010000001100000000010110000001100000010000000000111000000100000000010000000001001000000010000000000000000010110000011000000001000000000110100000010000000110000000001001000000110000110000000000000010000001000000000000000100101000000000000001000000000001011000000100000000000000000000100000001000000001000000100100100000001000000010000000001110000001000000000100000000010010000000100000000000000000101100000101000000010000000001001000000100000000100000000011010000000000000011000000000100100000011000000010000000000001000000000000001000000000010100000001000000011000000000001000000000000000010000000001001000000010000000100000000011100000001100000001000000000100100000001000000000000000001001000000000000000000000000101000000000000000000000000000101100000001000000000000000001011000000000000000100000000010000000000100000000000000000100100000000000000010000000010011000000010000000100000000010110000001100000011000000000101100000010000000100000000001011000000010000000000000000010000000000000000001000000000110100000000000000100000000001001000000100000101000000000000010000000000000010000000000001000000000000000110000000001001000000110000000100000000010000000001000000001000000000100100000000000000010000000010001000000000000000000000000011000000000000000000000000000000100000000000000100000000000010000000000000001100000000010010000001100000001000000000100000000010000000010000000001010000000010000000000000000010010000000000000000000000001010000000000000000000000000001011000000110000001100000000010110000001000000010000000000101100000001000000000000000001011000000000000000100000000010000000000100000000000000000100100000000000000100000000001101000000000000001000000000010010000001000001110000000000000100000000000000100000000000010000000000000001100000000010010000001100000001000000000100000000010000000010000000001001000000000000000000000000100010000000000000000000000000110000000000000000000000000000001000000000000001000000000000100000000000000011000000000100100000011000000010000000001000000000100000000100000000010100000000100000000000000000100100000000000000010000000001111001100000000000000000000
|
Binary file not shown.
77
tables.c
77
tables.c
|
@ -12,11 +12,12 @@
|
|||
*/
|
||||
|
||||
#include "tables.h"
|
||||
#define NB_REG 4
|
||||
#define MEM_SIZE 16
|
||||
#define NB_INSTRUCTIONS 128
|
||||
#define MEM_INST_SIZE 128
|
||||
#define NB_BITS_INSTRUCTION 5
|
||||
#include <stdlib.h>
|
||||
#define NB_REG 16
|
||||
#define MEM_SIZE 256
|
||||
#define NB_INSTRUCTIONS 256
|
||||
#define MEM_INST_SIZE 256
|
||||
#define NB_BITS_INSTRUCTION 8
|
||||
#define NB_BITS 8
|
||||
|
||||
int traduction_JMP[NB_INSTRUCTIONS];
|
||||
|
@ -42,51 +43,26 @@ void write_asm(FILE * file) {
|
|||
while (i<MEM_INST_SIZE) {
|
||||
if (buffer[i].instruction == ADD) {
|
||||
fprintf(file, "ADD %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
||||
} else if (buffer[i].instruction == NOP) {
|
||||
fprintf(file, "NOP 0 0 0\n");
|
||||
} else if (buffer[i].instruction == SUB) {
|
||||
fprintf(file, "SUB %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
||||
} else if (buffer[i].instruction == MUL) {
|
||||
fprintf(file, "MUL %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
||||
} else if (buffer[i].instruction == DIV) {
|
||||
fprintf(file, "DIV %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
||||
} else if (buffer[i].instruction == INF) {
|
||||
fprintf(file, "INF %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
||||
} else if (buffer[i].instruction == SUP) {
|
||||
fprintf(file, "SUP %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
||||
} else if (buffer[i].instruction == EQU) {
|
||||
fprintf(file, "EQU %d %d %d\n", buffer[i].param1, buffer[i].param2, buffer[i].param3);
|
||||
|
||||
} else if (buffer[i].instruction == AFC) {
|
||||
fprintf(file, "AFC %d %d\n", buffer[i].param1, buffer[i].param2);
|
||||
} else if (buffer[i].instruction == CPY) {
|
||||
fprintf(file, "CPY %d %d\n", buffer[i].param1, buffer[i].param2);
|
||||
|
||||
} else if (buffer[i].instruction == LOAD) {
|
||||
fprintf(file, "LOAD %d %d\n", buffer[i].param1, buffer[i].param2);
|
||||
} else if (buffer[i].instruction == STORE) {
|
||||
fprintf(file, "STORE %d %d\n", buffer[i].param1, buffer[i].param2);
|
||||
} else if (buffer[i].instruction == LOADI) {
|
||||
fprintf(file, "LOADI %d %d\n", buffer[i].param1, buffer[i].param2);
|
||||
} else if (buffer[i].instruction == STOREI) {
|
||||
fprintf(file, "STOREI %d %d\n", buffer[i].param1, buffer[i].param2);
|
||||
} else if (buffer[i].instruction == STOREA) {
|
||||
fprintf(file, "STOREA %d %d\n", buffer[i].param1, buffer[i].param2);
|
||||
|
||||
} else if (buffer[i].instruction == JMP) {
|
||||
fprintf(file, "JMP %d\n", traduction_JMP[buffer[i].param1]);
|
||||
} else if (buffer[i].instruction == JMZ) {
|
||||
fprintf(file, "JMZ %d\n", traduction_JMP[buffer[i].param1]);
|
||||
} else if (buffer[i].instruction == GET) {
|
||||
fprintf(file, "GET %d\n", buffer[i].param1);
|
||||
} else if (buffer[i].instruction == PRI) {
|
||||
fprintf(file, "PRI %d\n", buffer[i].param1);
|
||||
|
||||
} else if (buffer[i].instruction == CALL) {
|
||||
fprintf(file, "CALL %d %d\n", traduction_JMP[buffer[i].param1], buffer[i].param2);
|
||||
} else if (buffer[i].instruction == RET) {
|
||||
fprintf(file, "RET\n");
|
||||
} else if (buffer[i].instruction == STOP) {
|
||||
fprintf(file, "STOP %d\n", buffer[i].param1);
|
||||
}
|
||||
} else {
|
||||
printf("Error: unexpected instruction: %d\n", buffer[i].instruction);
|
||||
exit(1);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
@ -109,43 +85,46 @@ void convert_to_binary_on_N(int value, int N, char * buff) {
|
|||
buff[N] = '\0';
|
||||
}
|
||||
|
||||
void write_instruction_binary(FILE * file, struct str_instruction instr, char compact) {
|
||||
void write_instruction_binary(FILE * file, struct str_instruction instr, int compact, int printSeparator) {
|
||||
char buff1[33];
|
||||
char buff2[33];
|
||||
char buff3[33];
|
||||
char buff4[33];
|
||||
convert_to_binary_on_N(instr.instruction, NB_BITS_INSTRUCTION, buff1);
|
||||
if (instr.instruction == JMP || instr.instruction == JMZ || instr.instruction == CALL) {
|
||||
convert_to_binary_on_N(traduction_JMP[instr.param1], NB_BITS, buff2);
|
||||
} else {
|
||||
convert_to_binary_on_N(instr.param1, NB_BITS, buff2);
|
||||
}
|
||||
|
||||
convert_to_binary_on_N(instr.param1, NB_BITS, buff2);
|
||||
|
||||
convert_to_binary_on_N(instr.param2, NB_BITS, buff3);
|
||||
convert_to_binary_on_N(instr.param3, NB_BITS, buff4);
|
||||
if (compact) {
|
||||
fprintf(file, "%s%s%s%s", buff1, buff2, buff3, buff4);
|
||||
} else {
|
||||
fprintf(file, "\"%s%s%s%s\" & ", buff1, buff2, buff3, buff4);
|
||||
fprintf(file, "\"%s%s%s%s\"", buff1, buff2, buff3, buff4);
|
||||
if (printSeparator) {
|
||||
fprintf(file, ", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void write_code_machine(FILE * file) {
|
||||
int i = MEM_INST_SIZE - 1;
|
||||
while (i>=0) {
|
||||
write_instruction_binary(file, buffer[i], 0);
|
||||
fprintf(file, "signal memory: MEMORY_TYPE := (");
|
||||
while (i >= 0) {
|
||||
write_instruction_binary(file, buffer[i], 0, i != 0);
|
||||
i--;
|
||||
}
|
||||
fprintf(file, ");");
|
||||
}
|
||||
|
||||
void write_code_machine_compact(FILE * file) {
|
||||
printf(file, "\"");
|
||||
fprintf(file, "\"");
|
||||
int i = MEM_INST_SIZE - 1;
|
||||
while (i>=0) {
|
||||
write_instruction_binary(file, buffer[i], 1);
|
||||
while (i >= 0) {
|
||||
write_instruction_binary(file, buffer[i], 1, 0);
|
||||
i--;
|
||||
}
|
||||
printf(file, "\"\n");
|
||||
fprintf(file, "\"\n");
|
||||
}
|
||||
|
||||
|
||||
|
|
2
tables.h
2
tables.h
|
@ -18,7 +18,7 @@
|
|||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
enum instruction_t {NOP, ADD, MUL, SUB, DIV, INF, SUP, EQU, CPY, AFC, LOAD, STORE, LOADI, STOREI, STOREA, JMP, JMZ, PRI, GET, CALL, RET, STOP};
|
||||
enum instruction_t {NOP, ADD, MUL, SUB, DIV, CPY, AFC, LOAD, STORE};
|
||||
|
||||
void init(void);
|
||||
void print();
|
||||
|
|
BIN
tables.o
BIN
tables.o
Binary file not shown.
62
toto.asm
62
toto.asm
|
@ -1,62 +0,0 @@
|
|||
JMP 37
|
||||
AFC 1 1
|
||||
COP 2 0
|
||||
AFC 3 1
|
||||
MUL 1 3 1
|
||||
ADD 1 2 1
|
||||
READ 1 1
|
||||
PRI 1
|
||||
AFC 1 0
|
||||
COP 2 0
|
||||
AFC 3 1
|
||||
MUL 1 3 1
|
||||
ADD 1 2 1
|
||||
AFC 2 14
|
||||
WR 1 2
|
||||
AFC 1 2
|
||||
COP 0 1
|
||||
RET
|
||||
AFC 1 0
|
||||
COP 2 0
|
||||
AFC 3 1
|
||||
MUL 1 3 1
|
||||
ADD 1 2 1
|
||||
READ 1 1
|
||||
PRI 1
|
||||
AFC 1 1
|
||||
COP 2 0
|
||||
AFC 3 1
|
||||
MUL 1 3 1
|
||||
ADD 1 2 1
|
||||
AFC 2 10
|
||||
WR 1 2
|
||||
COP 1 0
|
||||
CALL 1 1
|
||||
AFC 1 1
|
||||
COP 0 1
|
||||
RET
|
||||
AFC 2 0
|
||||
AFCA 3 0
|
||||
AFC 5 1
|
||||
MUL 2 5 2
|
||||
ADD 2 3 2
|
||||
AFC 4 1
|
||||
WR 2 4
|
||||
AFC 3 1
|
||||
AFCA 4 0
|
||||
AFC 6 1
|
||||
MUL 3 6 3
|
||||
ADD 3 4 3
|
||||
AFC 5 12
|
||||
WR 3 5
|
||||
AFCA 4 0
|
||||
CALL 18 4
|
||||
AFC 4 0
|
||||
AFCA 5 0
|
||||
AFC 6 1
|
||||
MUL 4 6 4
|
||||
ADD 4 5 4
|
||||
READ 4 4
|
||||
PRI 4
|
||||
STOP 0
|
||||
|
Loading…
Reference in a new issue