Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include "malloc.h"
- #include "string.h"
- #include "stdlib.h"
- #include "ctype.h"
- enum : unsigned char {
- program_,
- nameprogram,
- data,
- integer_16,
- start_block,
- stop_block,
- label_,
- for_,
- to_,
- minus,
- next_,
- ternary,
- result,
- put,
- get,
- symbol,
- number,
- semicolon,
- koma,
- lparen,
- rparen,
- add_,
- sub_,
- not_,
- and_,
- or_,
- mul_,
- div_,
- mod_,
- assign,
- equal,
- not_equal,
- ng,
- nl,
- eof,
- unknown
- };
- struct Token {
- char name[50];
- int value;
- unsigned char type;
- };
- struct Symbol {
- char name[50];
- int val;
- };
- struct Segment {
- Token tokens[1000];
- int Len;
- Symbol symbols[50];
- int idnum;
- int LTable[50];
- int TopL;
- bool put_is_used;
- bool get_is_used;
- int errors;
- char input_file_name[50];
- char output_file_name[50];
- int expr[200];
- } segment;
- struct StackType {
- int st[200];
- int top;
- };
- class Stack {
- public:
- StackType S;
- void Init(StackType* s) {
- s->top = -1;
- }
- void Push(int i, StackType* s) {
- if(IsFull(s)) {
- puts("Stack error (assign full)");
- exit(0);
- } else {
- ++s->top;
- s->st[s->top] = i;
- }
- }
- int Pop(StackType* s) {
- int i;
- if(IsEmpty(s)) {
- puts("Stack error (assign empty)");
- exit(0);
- } else {
- i = s->st[s->top];
- --s->top;
- }
- return i;
- }
- bool IsEmpty(StackType* s) {
- if(s->top == -1) {
- return true;
- } else {
- return false;
- }
- }
- bool IsFull(StackType* s) {
- if(s->top == 199)return true;
- else return false;
- }
- void prints(StackType s) {
- int i=0;
- for(; i<10; ++i) {
- printf("%d_",s.st[i]);
- }
- }
- } s;
- FILE *input_file, *output_file, *error_file;
- Token next_token(FILE*);
- int analyze(FILE*);
- void print_analyze();
- int create_identifier_table(FILE*);
- void look_for_get_and_put();
- void print_get_instructions(FILE*);
- void print_put_instructions(FILE*);
- void print_title(FILE*, char*);
- void print_segment(FILE*);
- void print_initialization(FILE*);
- void print_ending(FILE*);
- void generate_code(FILE*, char*);
- int infix_to_postfix(int);
- bool is_operation(unsigned char);
- bool priority(unsigned char, StackType);
- void print_code(FILE*);
- void generate_asm_code(const char *, FILE*);
- void print_mod_instructions(FILE*);
- void print_and_instructions(FILE*);
- void print_or_instructions(FILE*);
- void print_not_instructions(FILE*);
- void print_eq_instructions(FILE*);
- void print_nl_instructions(FILE*);
- void print_ng_instructions(FILE*);
- int check_program(FILE*);
- int check_program(FILE* e) {
- char ids[50][50];
- int idnum = 0;
- int er = 0, i = 0, idn = 0;
- int max = 65535;
- int min = -65535;
- int label =0;
- fputs("S12 compiler\nDesigned by Anton Stoyan \nError file:\n",e);
- fputs("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n",e);
- if(segment.tokens[i].type == program_) {
- fputs("program_\n",e);
- ++i;
- } else {
- fprintf(e,"%s (Expected \"program_\")\n",segment.tokens[i].name);
- ++er;
- }
- if(segment.tokens[i].type == nameprogram) {
- fputs("nameprogram\n",e);
- ++i;
- } else {
- fprintf(e,"%s (Expected \"nameprogram_\")\n",segment.tokens[i].name);
- ++er;
- }
- if(segment.tokens[i].type == data) {
- fputs("variable\n",e);
- ++i;
- } else {
- fprintf(e,"%s (Expected \"variable\")\n",segment.tokens[i].name);
- ++er;
- }
- if(segment.tokens[i].type == integer_16) {
- fputs("integer_16\n",e);
- ++i;
- } else {
- fprintf(e,"%s (Expected \"integer_16\")\n",segment.tokens[i].name);
- ++er;
- }
- for(; segment.tokens[i].type != symbol; ++i) {
- fprintf(e,"%s (Expected variable)\n",segment.tokens[i].name);
- ++er;
- if(segment.tokens[i].type == eof) return er;
- if(segment.tokens[i].type == start_block) {
- fputs("start_block\n",e);
- break;
- }
- if((segment.tokens[i].type == for_)||(segment.tokens[i].type == to_)||(segment.tokens[i].type == next_)||
- (is_operation(segment.tokens[i].type))) {
- fputs("%s (Expected \"start_block\"\n)",e);
- ++er;
- }
- }
- for(; segment.tokens[i].type != start_block; ++i) {
- fprintf(e,"%s",segment.tokens[i].name);
- if(segment.tokens[i].type == semicolon) {
- fputs("\n",e);
- continue;
- } else if(segment.tokens[i].type == symbol) {
- strncpy(ids[idnum],segment.tokens[i].name,49);
- ++idnum;
- if(segment.tokens[i+1].type == koma) {
- fputs(",\n",e);
- ++i;
- } else if(segment.tokens[i+1].type == assign) {
- fputs(" >> ",e);
- ++i;
- if(segment.tokens[i+1].type == number) {
- fprintf(e,"%s",segment.tokens[i+1].name);
- if((atoi(segment.tokens[i+1].name) >= max)||(atoi(segment.tokens[i+1].name) <= min)) {
- ++er;
- fputs(" (Segment assign too long)",e);
- }
- ++i;
- if(segment.tokens[i+1].type == koma) {
- fputs(",\n",e);
- ++i;
- } else {
- fputs(" (Expected , or ;)\n",e);
- ++er;
- }
- } else {
- fputs(" (unexpected ::)\n",e);
- ++er;
- }
- }
- } else if(segment.tokens[i].type == eof) {
- fputs(" Unexpected eof\n",e);
- ++er;
- return er;
- } else if(segment.tokens[i].type == unknown) {
- fprintf(e,"%s (Unknown symbol)\n",segment.tokens[i+1].name);
- ++er;
- }
- }
- if(segment.tokens[i].type == start_block) {
- fputs("start_block\n",e);
- ++i;
- } else {
- for(; segment.tokens[i].type != start_block; ++i) {
- fprintf(e,"%s",segment.tokens[i].name);
- ++er;
- if(segment.tokens[i].type == eof)
- return er;
- }
- }
- for(; segment.tokens[i].type != eof; ++i) {
- fprintf(e,"%s",segment.tokens[i].name);
- if(segment.tokens[i].type == semicolon) {
- fputs(";",e);
- continue;
- }
- if(segment.tokens[i].type == stop_block) {
- return er;
- } else if((segment.tokens[i].type == unknown)||(segment.tokens[i].type == koma)) {
- fputs(" (Unexpected)\n",e);
- ++er;
- } else if(segment.tokens[i].type == symbol) {
- bool flag = true;
- int ind=0;
- for(; ind<idnum; ++ind) {
- if((strcmp(ids[ind],segment.tokens[i].name))==0) {
- flag = false;
- break;
- }
- }
- if(flag) {
- fputs(" (Undeclared id)\n",e);
- ++er;
- continue;
- }
- if(segment.tokens[i+1].type == assign) {
- ++i;
- ++i;
- int d=0;
- int f = 0;
- bool flag = true;
- fputs(" >> ",e);
- if(segment.tokens[i+1].type == unknown) {
- er++;
- fputs("unexpected",e);
- }
- for(; segment.tokens[i].type != semicolon; ++i) {
- fprintf(e,"%s",segment.tokens[i].name);
- if(f) {
- continue;
- }
- if(segment.tokens[i].type == lparen) {
- ++d;
- } else if(segment.tokens[i].type == rparen) {
- if(d == 0) {
- ++f;
- continue;
- } else {
- --d;
- }
- } else if(segment.tokens[i].type == symbol) {
- int ind=0;
- for(; ind<idnum; ++ind) {
- if((strncmp(ids[ind],segment.tokens[i].name,49))==0) {
- flag = false;
- break;
- }
- }
- if(f||flag) {
- ++i;
- for(; segment.tokens[i].type != semicolon; ++i) {
- fprintf(e,"%s",segment.tokens[i].name);
- }
- fputs(" (Ill expr)",e);
- continue;
- }
- }
- if(is_operation(segment.tokens[i].type)) {
- if(((segment.tokens[i-1].type != symbol)&&(segment.tokens[i-1].type != rparen)&&
- (segment.tokens[i-1].type != number)&&(segment.tokens[i-1].type != not_))||
- ((segment.tokens[i+1].type != lparen)&&(segment.tokens[i+1].type != symbol)&&
- (segment.tokens[i+1].type != number))) {
- ++i;
- for(; segment.tokens[i].type != semicolon; ++i) {
- fprintf(e,"%s",segment.tokens[i].name);
- }
- ++f;
- }
- }
- }
- if(f||(d != 0)) {
- fputs("; (illegal expression2)\n",e);
- ++er;
- } else fputs(";\n",e);
- }
- }
- if(segment.tokens[i].type == get) {
- if((segment.tokens[i+1].type == lparen)&&(segment.tokens[i+2].type == symbol)&&
- (segment.tokens[i+3].type == rparen)) {
- fprintf(e," ( %s );\n",segment.tokens[i+2].name);
- i+=4;
- } else {
- ++er;
- ++i;
- for(; segment.tokens[i].type != semicolon; ++i) {
- fprintf(e,"%s",segment.tokens[i].name);
- }
- fputs(" (illegal using of get() funtion)\n",e);
- }
- }
- if(segment.tokens[i].type == put) {
- int d=0;
- int f = 0;
- ++i;
- for(; segment.tokens[i].type != semicolon; ++i) {
- fprintf(e," %s",segment.tokens[i].name);
- if(f) {
- continue;
- }
- if(segment.tokens[i].type == lparen) {
- ++d;
- }
- if(segment.tokens[i].type == rparen) {
- if(d == 0) {
- ++f;
- continue;
- } else {
- --d;
- }
- }
- if(segment.tokens[i].type == symbol) {
- int ind=0;
- bool flag = true;
- for(; ind<idnum; ++ind) {
- if((strcmp(ids[ind],segment.tokens[i].name))==0) {
- flag = false;
- break;
- }
- }
- if(flag) {
- for(; segment.tokens[i].type == semicolon; ++i) {
- fprintf(e,"%s",segment.tokens[i].name);
- }
- }
- }
- if(is_operation(segment.tokens[i].type)) {
- if(((segment.tokens[i-1].type != symbol)&&(segment.tokens[i-1].type != rparen)&&
- (segment.tokens[i-1].type != not_)&&(segment.tokens[i-1].type != number))||
- ((segment.tokens[i+1].type != lparen)&&(segment.tokens[i+1].type != symbol)&&
- (segment.tokens[i+1].type != number))) {
- for(; segment.tokens[i].type != semicolon; ++i) {
- fprintf(e,"%s",segment.tokens[i].name);
- }
- ++f;
- }
- }
- }
- if(f||(d != 0)) {
- fputs("; (illegal expression1)\n",e);
- ++er;
- }
- fputs(";\n",e);
- } else if(segment.tokens[i].type == for_) {
- if(segment.tokens[i+1].type != number && segment.tokens[i+1].type != symbol ) {
- fputs(" Must be number or idetuficator\n",e);
- ++er;
- goto S;
- }
- if(segment.tokens[i+2].type == number) {
- fputs(" There assign no to\n",e);
- ++er;
- }
- S:
- int u;
- } else if(segment.tokens[i].type == to_) {
- if(segment.tokens[i-2].type!=for_) {
- fputs(" There assign no for\n",e);
- ++er;
- }
- if(segment.tokens[i+1].type != number && segment.tokens[i+1].type != symbol) {
- fputs(" Must be number or identuficator\n",e);
- ++er;
- goto R;
- }
- if(segment.tokens[i+2].type ==label_ ) {
- fputs(" There assign no next \n",e);
- ++er;
- }
- R:
- int uu;
- } else if(segment.tokens[i].type == next_) {
- if(segment.tokens[i+1].type != label_) {
- fputs(" There assign no label \n",e);
- ++er;
- }
- ++i;
- } else if(segment.tokens[i].type == label_) {
- ++i;
- }
- }
- fputs("stop_block\n",e);
- return er;
- }
- int create_identifier_table(FILE* e) {
- int i;
- int ir;
- ir = segment.Len;
- segment.idnum = 0;
- int ErrorFlag=0;
- for(i=3; i<ir; ++i) {
- sprintf(segment.symbols[segment.idnum].name,"%s",segment.tokens[i].name);
- if((segment.tokens[i+2].type == number)&&(segment.tokens[i+1].type == assign)) {
- segment.symbols[segment.idnum].val = segment.tokens[i+2].value;
- if(segment.tokens[i+3].type == semicolon) {
- return ErrorFlag;
- } else if(segment.tokens[i+3].type == koma) {
- i+=3;
- }
- ++segment.idnum;
- } else if (segment.tokens[i+1].type == semicolon) {
- ++segment.idnum;
- } else if(segment.tokens[i+1].type == koma) {
- ++segment.idnum;
- ++i;
- continue;
- } else if(segment.tokens[i].type == start_block) return ErrorFlag;
- else if(segment.tokens[i].type == eof) return ErrorFlag;
- }
- return ErrorFlag;
- }
- Token next_token(FILE* f) {
- Token token;
- memset(&token, 0, sizeof(Token));
- char buf[50] {0};
- for(;;) {
- char ch = getc(f);
- if(ch == ';') {
- strncpy(token.name, ";",2);
- token.type = semicolon;
- token.value = 0;
- break;
- } else if(ch == EOF) {
- strncpy(token.name,"EOF",4);
- token.type = eof;
- token.value = 0;
- break;
- } else if(isspace(ch)) continue;
- else if(ch == '/') {
- char c = getc(f);
- if(c == '/') {
- while ((c != EOF) && (c != '\n')) {
- c = getc(f);
- }
- ungetc(c, f);
- } else {
- ungetc(c, f);
- strncpy(token.name,"/",2);
- token.type = unknown;
- token.value = 0;
- break;
- }
- } else if(ch=='(') {
- strncpy(token.name,"(",2);
- token.type = lparen;
- token.value = 0;
- break;
- } else if(ch==')') {
- strncpy(token.name,")",2);
- token.type = rparen;
- token.value = 0;
- break;
- } else if(ch==',') {
- strncpy(token.name,",",2);
- token.type = koma;
- token.value = 0;
- break;
- } else if(ch=='?') {
- strncpy(token.name,"?",2);
- token.type = ternary;
- token.value = 0;
- break;
- } else if(ch=='>') {
- char c;
- c = getc(f);
- if(c==ch) {
- strncpy(token.name,">>",3);
- token.type = assign;
- token.value = 0;
- break;
- } else {
- ungetc(c,f);
- strncpy(token.name,">",2);
- token.type = unknown;
- token.value = 0;
- break;
- }
- } else if(ch=='<') {
- char c;
- c = getc(f);
- if(c=='>') {
- strncpy(token.name,"<>",3);
- token.type = not_equal;
- token.value = 0;
- break;
- } else {
- char buferr[6];
- int i=0;
- buferr[0]=ch;
- for (i=1;; i++) {
- buferr[i]=getc(f);
- if ((buferr[i]==' ')||(buferr[i]=='\n')||(buferr[i]=='\t')||(buferr[i]==';')) {
- if (buferr[i]=='\n') {
- break;
- }
- else break;
- }
- }
- ungetc(buferr[i],f);
- buferr[i]='\0';
- strncpy(token.name,buferr,i);
- token.type=nameprogram;
- token.value=0;
- break;
- }
- } else if(ch=='+') {
- char c;
- c = getc(f);
- if(c==ch) {
- strncpy(token.name,"++",3);
- token.type = add_;
- token.value = 0;
- break;
- } else {
- ungetc(c,f);
- strncpy(token.name,"+",2);
- token.type = unknown;
- token.value = 0;
- break;
- }
- } else if(ch=='*') {
- char c;
- c = getc(f);
- if(c==ch) {
- strncpy(token.name,"**",3);
- token.type = mul_;
- token.value = 0;
- break;
- } else {
- ungetc(c,f);
- strncpy(token.name,"*",2);
- token.type = unknown;
- token.value = 0;
- break;
- }
- } else if(ch=='=') {
- strncpy(token.name,"=",3);
- token.type = equal;
- token.value = 0;
- break;
- } else if(isalpha(ch)) {
- // if(ch=='L') {
- // char bufer[10];
- // int j=0;
- // bufer[0]=ch;
- // for (j=1;; j++) {
- // bufer[j]=getc(f);
- // if ((bufer[j]==' ')||(bufer[j]=='\n')||(bufer[j]=='\t')||(bufer[j]==';')) {
- // if (bufer[j]=='\n') {
- // break;
- // }
- // else break;
- // }
- // }
- // ungetc(bufer[j],f);
- // bufer[j]='\0';
- // strncpy(token.name,bufer,j);
- // token.type=label_;
- // token.value=0;
- // break;
- // }
- int i = 0;
- while (isalpha(ch) || isdigit(ch) || (ch == '_')) {
- buf[i++] = ch;
- ch = getc(f);
- }
- ungetc(ch, f);
- buf[i] = 0;
- strncpy(token.name, buf, i);
- if((strcmp(buf,"Get"))== 0) token.type = get;
- else if((strcmp(buf,"Put"))== 0) token.type = put;
- else if((strcmp(buf,"Mod"))== 0) token.type = mod_;
- else if((strcmp(buf,"Div"))== 0) token.type = div_;
- //else if((strcmp(buf,"For"))== 0) token.type = for_;
- // else if((strcmp(buf,"To"))== 0) token.type = to_;
- //else if((strcmp(buf,"Next"))== 0) token.type = next_;
- else if((strcmp(buf,"Variable"))== 0) token.type = data;
- else if((strcmp(buf,"StartBlock"))== 0) token.type = start_block;
- else if((strcmp(buf,"StopBlock"))== 0) token.type = stop_block;
- else if((strcmp(buf,"Integer_16"))== 0) token.type = integer_16;
- else if((strcmp(buf,"Not"))== 0) token.type = not_;
- else if((strcmp(buf,"And"))== 0) token.type = and_;
- else if((strcmp(buf,"Or"))== 0) token.type = or_;
- else if((strcmp(buf,"Lt"))== 0) token.type = nl;
- else if((strcmp(buf,"Gt"))== 0) token.type = ng;
- else if (i <= 8) {
- token.type = symbol;
- token.value = 0;
- } else {
- token.type = unknown;
- token.value = 0;
- }
- break;
- } else if (ch=='#') {
- char bufe[10];
- int ii=0;
- bufe[0]=ch;
- for (ii=1;; ii++) {
- bufe[ii]=getc(f);
- if ((bufe[ii]==' ')||(bufe[ii]=='\n')||(bufe[ii]=='\t')||(bufe[ii]==';')) {
- if (bufe[ii]=='\n') {
- break;
- }
- else break;
- }
- }
- ungetc(bufe[ii],f);
- bufe[ii]='\0';
- if (strcmp(bufe,"#Program")==0) {
- strncpy(token.name,"#Program",9);
- token.type=program_;
- token.value=0;
- break;
- } else {
- strncpy(token.name,bufe,ii);
- token.type = unknown;
- token.value = 0;
- break;
- }
- } else if(ch=='-') {
- char c;
- c = getc(f);
- if(isdigit(c)) {
- int i=2;
- buf[0] = ch;
- buf[1] = c;
- for(; isdigit(ch = getc(f)); ++i) {
- buf[i] = ch;
- }
- ungetc(ch,f);
- buf[i] = '\0';
- strncpy(token.name,buf,i);
- token.type = number;
- token.value = atoi(buf);
- break;
- } else if(c==ch) {
- strncpy(token.name,"-",3);
- token.type = sub_;
- token.value = 0;
- break;
- } else {
- ungetc(c,f);
- strncpy(token.name,"-",2);
- token.type = unknown;
- token.value = 0;
- break;
- }
- } else if(isdigit(ch)) {
- int i=1;
- buf[0] = ch;
- for(; isdigit(ch = getc(f)); ++i) {
- buf[i] = ch;
- }
- ungetc(ch,f);
- buf[i] = '\0';
- strncpy(token.name,buf,i);
- token.type = number;
- token.value = atoi(buf);
- break;
- } else {
- sprintf(token.name,"%s",ch);
- token.type = unknown;
- token.value = 0;
- break;
- }
- }
- return token;
- }
- int analyze(FILE* In) {
- int i = 0;
- while (true) {
- segment.tokens[i] = next_token(In);
- //printf("%s\n", segment.tokens[i].name);
- if (segment.tokens[i++].type == eof) break;
- }
- return i;
- }
- void print_analyze() {
- FILE* f = fopen("LexArray.txt","w");
- puts("Results of lexical analis:");
- puts(" Lex file assign LexArray.txt");
- fputs("S12 compiler\nDesigned by Anton Stoyan \n",f);
- fprintf(f,"Input file: %s\n", segment.input_file_name);
- fputs("Lex file\n",f);
- fputs(" № | Name | Type | Value\n\n",f);
- for(int i = 0; i<segment.Len; ++i) {
- char type[50] {0};
- switch(segment.tokens[i].type) {
- case add_ :
- strncpy(type,"add",4);
- break;
- case and_ :
- strncpy(type,"and",4);
- break;
- case rparen :
- strncpy(type,"rparen",7);
- break;
- case minus:
- strncpy(type,"minus",6);
- break;
- case data :
- strncpy(type,"data",5);
- break;
- case div_ :
- strncpy(type,"div",4);
- break;
- case for_ :
- strncpy(type,"for",4);
- break;
- case to_ :
- strncpy(type,"to",3);
- break;
- case next_ :
- strncpy(type,"next",5);
- break;
- case stop_block :
- strncpy(type,"stop_block",11);
- break;
- case eof :
- strncpy(type,"KeyWord",8);
- break;
- case get :
- strncpy(type,"get",4);
- break;
- case program_ :
- strncpy(type,"program_",9);
- break;
- case mod_ :
- strncpy(type,"mod",4);
- break;
- case mul_ :
- strncpy(type,"mul",4);
- break;
- case not_ :
- strncpy(type,"not",4);
- break;
- case number :
- strncpy(type,"number",7);
- break;
- case nameprogram :
- strncpy(type,"name",5);
- break;
- case lparen :
- strncpy(type,"lparen",7);
- break;
- case or_ :
- strncpy(type,"or",3);
- break;
- case put :
- strncpy(type,"put",4);
- break;
- case semicolon :
- strncpy(type,"semicolon",10);
- break;
- case start_block :
- strncpy(type,"start_block",12);
- break;
- case sub_ :
- strncpy(type,"sub",4);
- break;
- case symbol :
- strncpy(type,"variable",9);
- break;
- case ternary :
- strncpy(type,"ternary",8);
- break;
- case result :
- strncpy(type,"result",7);
- break;
- case koma :
- strncpy(type,"koma",5);
- break;
- case assign :
- strncpy(type,"assign",7);
- break;
- case equal :
- strncpy(type,"equal",6);
- break;
- case not_equal :
- strncpy(type,"not_equal",10);
- break;
- case nl :
- strncpy(type,"less",5);
- break;
- case ng :
- strncpy(type,"greather",9);
- break;
- case unknown :
- strncpy(type,"unknown",15);
- break;
- case integer_16 :
- strncpy(type,"integer_16",10);
- break;
- case label_:
- strncpy(type,"label",6);
- break;
- }
- fprintf(f," %-3d | %-10.10s | %-10.10s | %d\n",i+1,segment.tokens[i].name,type,segment.tokens[i].value);
- }
- fclose(f);
- }
- void look_for_get_and_put() {
- int i=0;
- do {
- ++i;
- } while(segment.tokens[i-1].type != start_block);
- for(; segment.tokens[i].type != eof; ++i) {
- if(segment.tokens[i].type == get) segment.get_is_used = true;
- if(segment.tokens[i].type == put) segment.put_is_used = true;
- if(segment.get_is_used && segment.put_is_used) break;
- }
- }
- void print_get_instructions(FILE* f) {
- fputs("\n;====Input procedure Get()=============\n",f);
- fputs("get PROC\n\n",f);
- fputs("\tpush ax\n",f);
- fputs("\tpush bx\n",f);
- fputs("\tpush cx\n",f);
- fputs("\tpush dx\n",f);
- fputs("\tpush di\n",f);
- fputs("\tpush si\n",f);
- fputs("\n\tlea dx,In_Str\n",f);
- fputs("\tmov ah,09\n",f);
- fputs("\tint 21h\n",f);
- fputs("\tmov di,offset buf\n",f);
- fputs("\tmov MaxLen,10\n",f);
- fputs("\tmov cx,MaxLen\n\n",f);
- fputs("\tmov si,0\n",f);
- fputs("\n In_00:\n",f);
- fputs("\tmov ah,01\n",f);
- fputs("\tint 21h\n",f);
- fputs("\tcmp al,0Dh\n",f);
- fputs("\tje In_1\n",f);
- fputs("\tcmp al,'-'\n",f);
- fputs("\tjne In_0\n",f);
- fputs("\tmov FlagS,1\n",f);
- fputs("\tjmp In_00\n",f);
- fputs("\n In_0:\n",f);
- fputs("\tmov dl,al\n",f);
- fputs("\tcall CHECK_BYTE\n",f);
- fputs("\tmov TStr[si],dl\n",f);
- fputs("\tinc si\n",f);
- fputs("\tloop In_00\n",f);
- fputs("\n In_1:\n",f);
- fputs("\tpush si\n",f);
- fputs("\tdec si\n",f);
- fputs("\tcmp cx,MaxLen\n",f);
- fputs("\tjne In_2\n",f);
- fputs("\tlea dx,erStr1\n",f);
- fputs("\tmov ah,09\n",f);
- fputs("\tint 21h\n",f);
- fputs("\tmov erFlag,1\n",f);
- fputs("\tjmp In_5\n",f);
- fputs("\n In_2:\n",f);
- fputs("\tmov bh,0\n",f);
- fputs("\tmov bl,TStr[si]\n",f);
- fputs("\tMY_MUL Mul10,bx,my_z\n",f);
- fputs("\tadd TBin,ax\n",f);
- fputs("\tadc TBin+2,dx\n",f);
- fputs("\tmov bh,0\n",f);
- fputs("\tmov bl,10\n",f);
- fputs("\tMY_MUL Mul10,bx,my_z\n",f);
- fputs("\tmov Mul10,ax\n",f);
- fputs("\tmov Mul10+2,dx\n",f);
- fputs("\tdec si\n",f);
- fputs("\tcmp si,0\n",f);
- fputs("\tjge In_2\n",f);
- fputs("\tmov ax,TBin\n",f);
- fputs("\tmov dx,TBin+2\n",f);
- fputs("\tpop si\n",f);
- fputs("\tcmp si,MaxLen\n",f);
- fputs("\tjl In_3\n",f);
- fputs("\tcmp MaxLen,10\n",f);
- fputs("\tjl In_2_1\n",f);
- fputs("\tjs In_Err\n",f);
- fputs("\tcmp dx,7FFFh\n",f);
- fputs("\tja In_Err\n",f);
- fputs("\tjmp In_3\n",f);
- fputs("\n In_2_1:\n",f);
- fputs("\tcmp MaxLen,5\n",f);
- fputs("\tjl In_2_2\n",f);
- fputs("\tcmp dx,00\n",f);
- fputs("\tja In_Err\n",f);
- fputs("\tcmp ah,7fh\n",f);
- fputs("\tja In_Err\n",f);
- fputs("\tjmp In_3\n",f);
- fputs("\n In_2_2:\n",f);
- fputs("\tcmp ax,007Fh\n",f);
- fputs("\tjbe In_3\n",f);
- fputs("\n In_Err:\n",f);
- fputs("\tlea dx,erStr3\n",f);
- fputs("\tmov ah,09\n",f);
- fputs("\tint 21h\n",f);
- fputs("\tmov erFlag,1\n",f);
- fputs("\tjmp In_5\n",f);
- fputs("\n In_3:\n",f);
- fputs("\tcmp FlagS,1\n",f);
- fputs("\tjne In_4\n",f);
- fputs("\tmov bx,0\n",f);
- fputs("\tsub bx,ax\n",f);
- fputs("\tmov ax,bx\n",f);
- fputs("\tmov bx,0\n",f);
- fputs("\tsbb bx,dx\n",f);
- fputs("\tmov dx,bx\n",f);
- fputs("\n In_4:\n",f);
- fputs("\tmov [di],ax\n",f);
- fputs("\tmov [di+2],dx\n",f);
- fputs("\tmov TBin,0\n",f);
- fputs("\tmov TBin+2,0\n",f);
- fputs("\tmov Mul10,1\n",f);
- fputs("\tmov Mul10+2,0\n",f);
- fputs("\tmov FlagS,0\n",f);
- fputs("\n In_5:\n\n",f);
- fputs("\tpop si\n",f);
- fputs("\tpop di\n",f);
- fputs("\tpop dx\n",f);
- fputs("\tpop cx\n",f);
- fputs("\tpop bx\n",f);
- fputs("\tpop ax\n\n",f);
- fputs("\tret\n",f);
- fputs("get ENDP\n\n",f);
- fputs("CHECK_BYTE PROC\n",f);
- fputs("\tsub dl,30h\n",f);
- fputs("\tcmp dl,00\n",f);
- fputs("\tjl ErS\n",f);
- fputs("\tcmp dl,0Ah\n",f);
- fputs("\tjl GO\n",f);
- fputs("\n ErS:\n",f);
- fputs("\tlea dx,erStr2\n",f);
- fputs("\tmov ah,09\n",f);
- fputs("\tint 21h\n",f);
- fputs("\n GO:\n",f);
- fputs("\tret\n",f);
- fputs("CHECK_BYTE ENDP\n",f);
- fputs(";======================================\n\n",f);
- }
- void print_put_instructions(FILE* f) {
- fputs("\n;===Output procedure put()=============\n",f);
- fputs("\nput PROC\n\n",f);
- fputs("\tpush ax\n",f);
- fputs("\tpush bx\n",f);
- fputs("\tpush cx\n",f);
- fputs("\tpush dx\n",f);
- fputs("\tpush di\n",f);
- fputs("\tpush si\n\n",f);
- fputs("\tmov cl,byte ptr buf+3\n",f);
- fputs("\tand cl,80h\n",f);
- fputs("\tje m6\n",f);
- fputs("\tfild buf\n",f);
- fputs("\tfchs\n",f);
- fputs("\tfistp buf\n",f);
- fputs("\tmov MSign,\'-\'\n",f);
- fputs("\n M6:\n",f);
- fputs("\tmov cx,10\n",f);
- fputs("\tmov di,0\n",f);
- fputs("\n O_1:\n",f);
- fputs("\tffree st(0)\n",f);
- fputs("\tffree st(1)\n",f);
- fputs("\tfild ten\n",f);
- fputs("\tfild buf\n",f);
- fputs("\tfprem\n",f);
- fputs("\tfistp X1\n",f);
- fputs("\tmov dl,byte ptr X1\n",f);
- fputs("\tadd dl,30h\n",f);
- fputs("\tmov X_Str[di],dl\n",f);
- fputs("\tinc di\n",f);
- fputs("\tfild buf\n",f);
- fputs("\tfxch st(1)\n",f);
- fputs("\tfdiv\n",f);
- fputs("\tfrndint\n",f);
- fputs("\tfistp buf\n",f);
- fputs("\tloop O_1\n\n",f);
- fputs("\tmov dx,offset MX1\n",f);
- fputs("\tmov ah,09\n",f);
- fputs("\tint 21h\n",f);
- fputs("\tmov dl,MSign\n",f);
- fputs("\tmov ah,02\n",f);
- fputs("\tint 21h\n",f);
- fputs("\tinc di\n",f);
- fputs("\tmov cx,12\n",f);
- fputs("\n O_2:\n",f);
- fputs("\tmov dl,X_Str[di]\n",f);
- fputs("\tmov ah,02h\n",f);
- fputs("\tint 21h\n",f);
- fputs("\tdec di\n",f);
- fputs("\tloop O_2\n\n",f);
- fputs("\tmov MSign,\'+\'\n\n",f);
- fputs("\tpop si\n",f);
- fputs("\tpop di\n",f);
- fputs("\tpop dx\n",f);
- fputs("\tpop cx\n",f);
- fputs("\tpop bx\n",f);
- fputs("\tpop ax\n\n",f);
- fputs("\tret\n",f);
- fputs("put ENDP\n",f);
- fputs(";======================================\n\n",f);
- }
- void print_title(FILE* f,char* in) {
- fprintf(f,";Translated from input file %s\n",in);
- fputs("DOSSEG\n",f);
- fputs(".MODEL SMALL\n",f);
- fputs(".STACK 100h\n\n",f);
- }
- void print_segment(FILE* f) {
- if(segment.get_is_used) {
- fputs("MY_MUL MACRO X,Y,Z\n",f);
- fputs("\tmov z,0\n",f);
- fputs("\tmov z+2,0\n",f);
- fputs("\tmov ax,x\n",f);
- fputs("\tmul y\n",f);
- fputs("\tmov z,ax\n",f);
- fputs("\tmov z+2,dx\n",f);
- fputs("\tmov ax,x+2\n",f);
- fputs("\tmul y\n",f);
- fputs("\tadd z+2,ax\n",f);
- fputs("\tmov ax,z\n",f);
- fputs("\tmov dx,z+2\n",f);
- fputs("ENDM\n\n",f);
- }
- int i;
- fputs(".DATA\n",f);
- for(i = 0; i<segment.idnum; ++i) {
- fprintf(f,"\t%s\tlabel word\n\tdd\t0%xh\n",segment.symbols[i].name,segment.symbols[i].val);
- }
- fputs("\n\tbuf\tdd\t0h\n",f);
- fputs("\n\tbuf1\tdw\t0h\n",f);
- fputs("\n\tbuf2\tdw\t0h\n",f);
- fputs("\n\tbuf3\tdw\t0h\n",f);
- fputs("\n\tcond\tdw\t0h\n",f);
- fputs("\n\trez\tdw\t0h\n",f);
- fputs("\n\tFor1\tdw\t0h\n",f);
- fputs("\tRC\tdw\t0h\n",f);
- fputs("\tlb1\tdd\t0h\n",f);
- fputs("\tlb2\tdd\t0h\n\n",f);
- if(segment.get_is_used) {
- fputs(";======Segment for get() functions======\n",f);
- fputs("\terFlag\tdb\t0\n",f);
- fputs("\tTStr\tdb\t10 dup (0)\n",f);
- fputs("\tTBin\tdw\t0,0\n",f);
- fputs("\tMaxLen\tdw\t0\n",f);
- fputs("\tFlagS\tdb\t0\n",f);
- fputs("\tMul10\tdw\t1,0\n",f);
- fputs("\tmy_z\tdw\t0,0\n\n",f);
- fputs("\tIn_Str\tdb\t13,10,\'<< $\'\n",f);
- fputs("\terStr1\tdb\t13,10,\'Segment not input_variable\',13,10,\'$\'\n",f);
- fputs("\terStr2\tdb\t13,10,\'Incorrectly data \',13,10,\'$\'\n",f);
- fputs("\terStr3\tdb\t13,10,\'Segment assign too long \',13,10,\'$\'\n",f);
- fputs(";======================================\n\n",f);
- }
- if(segment.put_is_used) {
- fputs(";=======Segment for put===================\n",f);
- fputs("\tMSign\tdb\t\'+\',\'$\'\n",f);
- fputs("\tX_Str\tdb\t12 dup (0)\n",f);
- fputs("\tten\tdw\t10\n",f);
- fputs("\tX1\tdw\t0h\n",f);
- fputs("\tMX1\tdb\t13,10,\'>> $\'\n",f);
- fputs(";======================================\n\n",f);
- }
- }
- void print_initialization(FILE* f) {
- fputs(".CODE\n",f);
- fputs(";======================================\n",f);
- fputs("\tmov ax,@data\n",f);
- fputs("\tmov ds,ax\n\n",f);
- fputs("\tfinit\n",f);
- fputs("\tfstcw RC\n",f);
- fputs("\tor RC,0c00h\n",f);
- fputs("\tfldcw RC\n",f);
- fputs(";======================================\n\n",f);
- }
- void print_ending(FILE* f) {
- fputs(";======================================\n",f);
- fputs("MOV AH,4Ch\nINT 21h\n",f);
- if(segment.get_is_used) {
- print_get_instructions(f);
- }
- if(segment.put_is_used) {
- print_put_instructions(f);
- }
- print_mod_instructions(f);
- print_and_instructions(f);
- print_or_instructions(f);
- print_not_instructions(f);
- print_eq_instructions(f);
- print_nl_instructions(f);
- print_ng_instructions(f);
- fputs(";======================================\n\n",f);
- fputs("END",f);
- }
- int infix_to_postfix(int i) {
- s.Init(&s.S);
- int n,z;
- n = 0;
- for(; segment.tokens[i+n].type!=semicolon; ++n);
- int k;
- k=i+n;
- for(z=0; i < k; ++i) {
- unsigned char in;
- in = segment.tokens[i].type;
- if((in == symbol)||(in == number)||(in == not_)) {
- segment.expr[z] = i;
- ++z;
- } else if(is_operation(in)) {
- if(s.IsEmpty(&s.S)||priority(in,s.S)) {
- s.Push(i,&s.S);
- } else {
- if(segment.tokens[s.S.st[s.S.top]].type!=lparen) {
- do {
- segment.expr[z] = s.Pop(&s.S);
- ++z;
- } while((!(priority(in,s.S)))&&(!(s.IsEmpty(&s.S)))&&
- (segment.tokens[s.S.st[s.S.top]].type != lparen));
- }
- s.Push(i,&s.S);
- }
- }
- if(in == lparen) {
- s.Push(i,&s.S);
- segment.expr[z] = i;
- ++z;
- }
- if(in == rparen) {
- for(; segment.tokens[s.S.st[s.S.top]].type != lparen;) {
- segment.expr[z] = s.Pop(&s.S);
- ++z;
- }
- s.Pop(&s.S);
- segment.expr[z] = i;
- ++z;
- }
- }
- for(; !(s.IsEmpty(&s.S));) {
- segment.expr[z] = s.Pop(&s.S);
- ++z;
- }
- segment.expr[z] = 3000;
- z++;
- return k;
- }
- bool is_operation(unsigned char t) {
- return ((t==add_)||(t==sub_)||(t==div_)||(t==mul_)||(t==mod_)|| (t==and_)||(t==or_));
- }
- bool priority(unsigned char t,StackType s) {
- return
- ((t==div_)&&(segment.tokens[s.st[s.top]].type==add_))||
- ((t==div_)&&(segment.tokens[s.st[s.top]].type==sub_))||
- ((t==div_)&&(segment.tokens[s.st[s.top]].type==or_))||
- ((t==div_)&&(segment.tokens[s.st[s.top]].type==and_))||
- ((t==mul_)&&(segment.tokens[s.st[s.top]].type==add_))||
- ((t==mul_)&&(segment.tokens[s.st[s.top]].type==sub_))||
- ((t==mul_)&&(segment.tokens[s.st[s.top]].type==or_))||
- ((t==mul_)&&(segment.tokens[s.st[s.top]].type==and_))||
- ((t==mod_)&&(segment.tokens[s.st[s.top]].type==add_))||
- ((t==mod_)&&(segment.tokens[s.st[s.top]].type==sub_))||
- ((t==mod_)&&(segment.tokens[s.st[s.top]].type==or_))||
- ((t==mod_)&&(segment.tokens[s.st[s.top]].type==and_))||
- ((t==and_)&&(segment.tokens[s.st[s.top]].type==or_));
- }
- void print_code(FILE* f) {
- int lab=0;
- Token l;
- int i=0;
- do {
- ++i;
- } while(segment.tokens[i].type != start_block);
- ++i;
- for(;; ++i) {
- l.type = segment.tokens[i].type;
- strncpy(l.name,segment.tokens[i].name,50);;
- l.value = segment.tokens[i].value;
- if(l.type == stop_block) break;
- if(l.type == put) {
- if(segment.tokens[i+3].type==ng) {
- int cond=0;
- fprintf(f,"\tfild %s\n",segment.tokens[i+2].name);
- fprintf(f,"\tfild %s\n",segment.tokens[i+4].name);
- fputs("\tcall ng_\n",f);
- fputs("\tfistp cond\n",f);
- fputs("\tmov ax, word ptr cond\n",f);
- fputs("\tcmp ax,1\n",f);
- fprintf(f,"\tjne Cond%d\n",cond);
- fprintf(f,"\tmov rez, %s\n",segment.tokens[i+8].name);
- fprintf(f,"\tjmp Condit%d\n",cond);
- fprintf(f,"Cond%d:\n",cond);
- fprintf(f,"\tmov rez, %s\n",segment.tokens[i+6].name);
- fprintf(f,"Condit%d:\n",cond);
- fputs("\tfild rez\n",f);
- fputs("\tfistp buf\n",f);
- i=i+10;
- cond++;
- goto stop_block;
- }
- if(segment.tokens[i+3].type==nl) {
- int cond1=0;
- fprintf(f,"\tfild %s\n",segment.tokens[i+2].name);
- fprintf(f,"\tfild %s\n",segment.tokens[i+4].name);
- fputs("\tcall nl_\n",f);
- fputs("\tfistp cond\n",f);
- fputs("\tmov ax, word ptr cond\n",f);
- fputs("\tcmp ax,1\n",f);
- fprintf(f,"\tjne Cond1%d\n",cond1);
- fprintf(f,"\tmov rez, %s\n",segment.tokens[i+8].name);
- fprintf(f,"\tjmp Condit1%d\n",cond1);
- fprintf(f,"Cond1%d:\n",cond1);
- fprintf(f,"\tmov rez, %s\n",segment.tokens[i+6].name);
- fprintf(f,"Condit1%d:\n",cond1);
- fputs("\tfild rez\n",f);
- fputs("\tfistp buf\n",f);
- i=i+10;
- cond1++;
- goto stop_block;
- }
- if(segment.tokens[i+3].type==equal) {
- int cond2 = 0;
- fprintf(f,"\tfild %s\n",segment.tokens[i+2].name);
- fprintf(f,"\tfild %s\n",segment.tokens[i+4].name);
- fputs("\tcall eq_\n",f);
- fputs("\tfistp cond\n",f);
- fputs("\tmov ax, word ptr cond\n",f);
- fputs("\tcmp ax,1\n",f);
- fprintf(f,"\tjne Cond2%d\n",cond2);
- fprintf(f,"\tmov rez, %s\n",segment.tokens[i+6].name);
- fprintf(f,"\tjmp Condit2%d\n",cond2);
- fprintf(f,"Cond2%d:\n",cond2);
- fprintf(f,"\tmov rez, %s\n",segment.tokens[i+8].name);
- fprintf(f,"Condit2%d:\n",cond2);
- fputs("\tfild rez\n",f);
- fputs("\tfistp buf\n",f);
- i=i+10;
- cond2++;
- goto stop_block;
- }
- if(segment.tokens[i+3].type==not_equal) {
- int cond3=0;
- fprintf(f,"\tfild %s\n",segment.tokens[i+2].name);
- fprintf(f,"\tfild %s\n",segment.tokens[i+4].name);
- fputs("\tcall eq_\n",f);
- fputs("\tcall not_\n",f);
- fputs("\tfistp cond\n",f);
- fputs("\tmov ax, word ptr cond\n",f);
- fputs("\tcmp ax,1\n",f);
- fprintf(f,"\tjne Cond3%d\n",cond3);
- fprintf(f,"\tmov rez, %s\n",segment.tokens[i+6].name);
- fprintf(f,"\tjmp Condit3%d\n",cond3);
- fprintf(f,"Cond3%d:\n",cond3);
- fprintf(f,"\tmov rez, %s\n",segment.tokens[i+8].name);
- fprintf(f,"Condit3%d:\n",cond3);
- fputs("\tfild rez\n",f);
- fputs("\tfistp buf\n",f);
- i=i+10;
- cond3++;
- goto stop_block;
- }
- i = infix_to_postfix(i+1);
- generate_asm_code("buf",f);
- stop_block:
- fputs("\tcall put\n",f);
- }
- if(l.type == get) {
- fputs("\tcall get\n",f);
- fprintf(f,"\tfild buf\n");
- fprintf(f,"\tfistp %s\n",segment.tokens[i+2].name);
- i +=4;
- }
- if (l.type== for_) {
- if(segment.tokens[i+1].type==number) {
- char bufHi[6], bufLo[5], buf[10];
- buf[0] = '0';
- sprintf(&buf[1],"%08x",segment.tokens[i+1].value);
- strncpy(bufLo,&buf[5],4);
- bufLo[4] = '\0';
- fprintf(f,"\tmov buf1,0%sh\n",bufLo);
- }
- if(segment.tokens[i+1].type==symbol) {
- fprintf(f,"\tmov ax,%s+2\n",segment.tokens[i+1].name);
- fputs("\tmov buf1,ax\n",f);
- i++;
- }
- }
- if (l.type== to_) {
- if(segment.tokens[i+1].type==number) {
- char bufHi[6], bufLo[5], buf[10];
- buf[0] = '0';
- sprintf(&buf[1],"%08x",segment.tokens[i+1].value);
- strncpy(bufLo,&buf[5],4);
- bufLo[4] = '\0';
- fprintf(f,"\tmov buf2,0%sh\n",bufLo);
- fputs("\tmov ax,buf2\n",f);
- fputs("\tsub ax, buf1\n",f);
- fputs("\tmov For1,ax\n",f);
- fputs("\tmov cx,For1\n",f);
- fprintf(f,"\n Label_%d:\n",lab);
- }
- if(segment.tokens[i+1].type==symbol) {
- fprintf(f,"\tmov ax,%s\n",segment.tokens[i+1].name);
- fputs("\tmov buf2,ax\n",f);
- fputs("\tsub ax, buf1\n",f);
- fputs("\tmov For1,ax\n",f);
- fputs("\tmov cx,For1\n",f);
- fprintf(f,"\n Label_%d:\n",lab);
- i++;
- }
- }
- if(l.type == next_) {
- i++;
- }
- if(l.type == label_) {
- fprintf(f,"\n loop Label_%d\n",lab);
- }
- if(l.type == symbol) {
- if(segment.tokens[i+2].type==minus) {
- fprintf(f,"\tfild %s\n",segment.tokens[i+3].name);
- fputs("\tFLDZ\n",f);
- fputs("\tFSUBR\n",f);
- fprintf(f,"\tfistp %s\n",segment.tokens[i].name);
- i=i+2;
- }
- int bufi;
- bufi = i;
- i = infix_to_postfix(i+2);
- if(i<0) {
- i = -i;
- puts("IE\n");
- continue;
- }
- generate_asm_code(segment.tokens[bufi].name,f);
- }
- }
- }
- void generate_asm_code(const char * str, FILE* f) {
- int n;
- for(n=0; segment.expr[n] != 3000; ++n) {
- s.Init(&s.S);
- if((!is_operation(segment.tokens[segment.expr[n]].type))&&(segment.tokens[segment.expr[n]].type != not_)) {
- if(segment.tokens[segment.expr[n]].type == symbol) {
- fprintf(f,"\tfild %s\n",segment.tokens[segment.expr[n]].name);
- } else if(segment.tokens[segment.expr[n]].type == number) {
- char bufHi[6], bufLo[5], buf[10];
- buf[0] = '0';
- sprintf(&buf[1],"%08x",segment.tokens[segment.expr[n]].value);
- strncpy(bufHi,buf,5);
- bufHi[5] = '\0';
- strncpy(bufLo,&buf[5],4);
- bufLo[4] = '\0';
- fprintf(f,"\tmov word ptr buf,0%sh\n",bufLo);
- fprintf(f,"\tmov word ptr buf+2,0%sh\n",bufHi);
- fputs("\tfild buf\n",f);
- } else if((segment.tokens[segment.expr[n]].type == lparen)||(segment.tokens[segment.expr[n]].type == rparen)) {
- continue;
- }
- } else {
- switch(segment.tokens[segment.expr[n]].type) {
- case add_:
- fputs("\tfadd\n",f);
- break;
- case sub_:
- fputs("\tfsub\n",f);
- break;
- case div_:
- fputs("\tfdiv\n",f);
- break;
- case mod_:
- fputs("\tcall mod_\n",f);
- break;
- case mul_:
- fputs("\tfmul\n",f);
- break;
- case and_:
- fputs("\tcall and_\n",f);
- break;
- case or_:
- fputs("\tcall or_\n",f);
- break;
- case not_:
- fputs("\tcall not_\n",f);
- break;
- case equal:
- fputs("\tcall eq_\n",f);
- break;
- case not_equal:
- fputs("\tcall eq_\n",f);
- fputs("\tcall not_\n",f);
- break;
- case ng:
- fputs("\tcall ng_\n",f);
- break;
- case nl:
- fputs("\tcall nl_\n",f);
- break;
- }
- }
- }
- if(*str=='-') {
- goto f;
- }
- fprintf(f,"\tfistp %s\n",str);
- f:
- ;
- }
- void print_mod_instructions(FILE* f) {
- fputs("\n;===Procedure mod_====================\n",f);
- fputs("\nmod_ PROC\n\n",f);
- fputs("\tpush ax\n",f);
- fputs("\tpush bx\n",f);
- fputs("\tpush cx\n",f);
- fputs("\tpush dx\n",f);
- fputs("\tpushf\n\n",f);
- fputs("\tmov word ptr buf,1h\n",f);
- fputs("\tmov word ptr buf + 2,0h\n",f);
- fputs("\tfistp lb1\n",f);
- fputs("\tfistp lb2\n",f);
- fputs("\tmov ax,word ptr lb2\n",f);
- fputs("\tmov bx,word ptr lb2 + 2\n",f);
- fputs("\tand bh,80h\n",f);
- fputs("\tjz dod1\n",f);
- fputs("\tfild lb2\n",f);
- fputs("\tfchs\n",f);
- fputs("\tfistp lb2\n",f);
- fputs("\tmov word ptr buf,0ffffh\n",f);
- fputs("\tmov word ptr buf + 2,0ffffh\n",f);
- fputs("\n dod1:\n",f);
- fputs("\tmov ax,word ptr lb2\n",f);
- fputs("\tmov bx,word ptr lb2 + 2\n",f);
- fputs("\tmov cx,word ptr lb1\n",f);
- fputs("\tmov dx,word ptr lb1 + 2\n",f);
- fputs("\tand dh,80h\n",f);
- fputs("\tjz dod2\n",f);
- fputs("\tfild lb1\n",f);
- fputs("\tfchs\n",f);
- fputs("\tfistp lb1\n",f);
- fputs("\n dod2:\n",f);
- fputs("\tmov cx,word ptr lb1\n",f);
- fputs("\tmov dx,word ptr lb1 + 2\n",f);
- fputs("\tcmp ax,cx\n",f);
- fputs("\tjne r\n",f);
- fputs("\tcmp bx,dx\n",f);
- fputs("\tjne r\n",f);
- fputs("\tmov word ptr lb1,0h\n",f);
- fputs("\tmov word ptr lb1 + 2,0h\n",f);
- fputs("\tjmp end_mod\n",f);
- fputs("\n r:\n",f);
- fputs("\tcmp bx,dx\n",f);
- fputs("\tjg nb\n",f);
- fputs("\tjl b\n",f);
- fputs("\tcmp ax,cx\n",f);
- fputs("\tjb b\n",f);
- fputs("\n nb:\n",f);
- fputs("\tsub ax,cx\n",f);
- fputs("\tsbb bx,dx\n",f);
- fputs("\tjmp r\n",f);
- fputs("\n b:\n",f);
- fputs("\tmov word ptr lb1,ax\n",f);
- fputs("\tmov word ptr lb1 + 2,bx\n",f);
- fputs("\n end_mod:\n",f);
- fputs("\tfild lb1\n",f);
- fputs("\tfimul buf\n\n",f);
- fputs("\tpopf\n",f);
- fputs("\tpop dx\n",f);
- fputs("\tpop cx\n",f);
- fputs("\tpop bx\n",f);
- fputs("\tpop ax\n",f);
- fputs("\tret\n",f);
- fputs("mod_ ENDP\n",f);
- fputs(";======================================\n\n",f);
- }
- void print_and_instructions(FILE* f) {
- fputs("\n;===Procedure and_====================\n",f);
- fputs("\nand_ PROC\n\n",f);
- fputs("\tpush ax\n",f);
- fputs("\tpush dx\n",f);
- fputs("\tpushf\n",f);
- fputs("\tfistp lb1\n",f);
- fputs("\tfistp lb2\n",f);
- fputs("\tmov ax,word ptr lb1\n",f);
- fputs("\tmov dx,word ptr lb1+2\n",f);
- fputs("\tcmp ax,0\n",f);
- fputs("\tjnz true_and1\n",f);
- fputs("\tcmp dx,0\n",f);
- fputs("\tjz false_and\n",f);
- fputs("\n true_and1:\n",f);
- fputs("\tmov ax,word ptr lb2\n",f);
- fputs("\tmov dx,word ptr lb2+2\n",f);
- fputs("\tcmp ax,0\n",f);
- fputs("\tjnz true_and\n",f);
- fputs("\tcmp dx,0\n",f);
- fputs("\tjnz true_and\n",f);
- fputs("\n false_and:\n",f);
- fputs("\tfldz\n",f);
- fputs("\tjmp l_and\n",f);
- fputs("\n true_and:\n",f);
- fputs("\tfld1\n",f);
- fputs("\n l_and:\n\n",f);
- fputs("\tpopf\n",f);
- fputs("\tpop dx\n",f);
- fputs("\tpop ax\n\n",f);
- fputs("\tret\n",f);
- fputs("and_ ENDP\n",f);
- fputs(";======================================\n\n",f);
- }
- void print_or_instructions(FILE* f) {
- fputs("\n;===Procedure or_======================\n",f);
- fputs("\nor_ PROC\n\n",f);
- fputs("\tpush ax\n",f);
- fputs("\tpush dx\n",f);
- fputs("\tpushf\n",f);
- fputs("\tfistp lb1\n",f);
- fputs("\tfistp lb2\n",f);
- fputs("\tmov ax,word ptr lb1\n",f);
- fputs("\tmov dx,word ptr lb1+2\n",f);
- fputs("\tcmp ax,0\n",f);
- fputs("\tjnz true_or\n",f);
- fputs("\tcmp dx,0\n",f);
- fputs("\tjnz true_or\n",f);
- fputs("\tmov ax,word ptr lb2\n",f);
- fputs("\tmov dx,word ptr lb2+2\n",f);
- fputs("\tcmp ax,0\n",f);
- fputs("\tjnz true_or\n",f);
- fputs("\tcmp dx,0\n",f);
- fputs("\tjnz true_or\n",f);
- fputs("\tfldz\n",f);
- fputs("\tjmp l_or\n",f);
- fputs("\n true_or:\n",f);
- fputs("\tfld1\n",f);
- fputs("\n l_or:\n\n",f);
- fputs("\tpopf\n",f);
- fputs("\tpop dx\n",f);
- fputs("\tpop ax\n\n",f);
- fputs("\tret\n",f);
- fputs("or_ ENDP\n",f);
- fputs(";======================================\n\n",f);
- }
- void print_not_instructions(FILE* f) {
- fputs("\n;===Procedure not_====================\n",f);
- fputs("\nnot_ PROC\n\n",f);
- fputs("\tpush ax\n",f);
- fputs("\tpushf\n",f);
- fputs("\tfistp lb1\n",f);
- fputs("\tmov ax,word ptr lb1\n",f);
- fputs("\tcmp ax,0\n",f);
- fputs("\tjne is_true\n",f);
- fputs("\tmov ax,word ptr lb1+2\n",f);
- fputs("\tcmp ax,0\n",f);
- fputs("\tjne is_true\n",f);
- fputs("\tfld1\n",f);
- fputs("\tjmp l_not\n",f);
- fputs("\n is_true:\n",f);
- fputs("\tfldz\n",f);
- fputs("\n l_not:\n\n",f);
- fputs("\tpopf\n",f);
- fputs("\tpop ax\n\n",f);
- fputs("\tret\n",f);
- fputs("not_ ENDP\n",f);
- fputs(";======================================\n\n",f);
- }
- void print_eq_instructions(FILE* f) {
- fputs("\n;===Procedure eq_======================\n",f);
- fputs("\neq_ PROC\n\n",f);
- fputs("\tpush ax\n",f);
- fputs("\tpush bx\n",f);
- fputs("\tpush cx\n",f);
- fputs("\tpush dx\n",f);
- fputs("\tpushf\n",f);
- fputs("\tfistp lb1\n",f);
- fputs("\tfistp lb2\n",f);
- fputs("\tmov ax,word ptr lb1\n",f);
- fputs("\tmov bx,word ptr lb1+2\n",f);
- fputs("\tmov cx,word ptr lb2\n",f);
- fputs("\tmov dx,word ptr lb2+2\n",f);
- fputs("\tcmp ax,cx\n",f);
- fputs("\tjne not_eq\n",f);
- fputs("\tcmp bx,dx\n",f);
- fputs("\tjne not_eq\n",f);
- fputs("\tfld1\n",f);
- fputs("\tjmp l_eq\n",f);
- fputs("\n not_eq:\n",f);
- fputs("\tfldz\n",f);
- fputs("\n l_eq:\n",f);
- fputs("\tpopf\n",f);
- fputs("\tpop dx\n",f);
- fputs("\tpop cx\n",f);
- fputs("\tpop bx\n",f);
- fputs("\tpop ax\n\n",f);
- fputs("\tret\n",f);
- fputs("eq_ ENDP\n",f);
- fputs(";======================================\n\n",f);
- }
- void print_nl_instructions(FILE* f) {
- fputs("\n;===Procedure nl_======================\n",f);
- fputs("\nnl_ PROC\n\n",f);
- fputs("\tpush ax\n",f);
- fputs("\tpush bx\n",f);
- fputs("\tpush cx\n",f);
- fputs("\tpush dx\n",f);
- fputs("\tpushf\n",f);
- fputs("\tfistp lb1\n",f);
- fputs("\tfistp lb2\n",f);
- fputs("\tmov ax,word ptr lb1\n",f);
- fputs("\tmov bx,word ptr lb1+2\n",f);
- fputs("\tmov cx,word ptr lb2\n",f);
- fputs("\tmov dx,word ptr lb2+2\n",f);
- fputs("\tcmp dx,bx\n",f);
- fputs("\tjl lov\n",f);
- fputs("\tjg gre\n",f);
- fputs("\tcmp cx,ax\n",f);
- fputs("\tjb lov\n",f);
- fputs("\n gre:\n",f);
- fputs("\tfld1\n",f);
- fputs("\tjmp l_nl\n",f);
- fputs("\n lov:\n",f);
- fputs("\tfldz\n",f);
- fputs("\n l_nl:\n",f);
- fputs("\tpopf\n",f);
- fputs("\tpop dx\n",f);
- fputs("\tpop cx\n",f);
- fputs("\tpop bx\n",f);
- fputs("\tpop ax\n\n",f);
- fputs("\tret\n",f);
- fputs("nl_ ENDP\n",f);
- fputs(";======================================\n\n",f);
- }
- void print_ng_instructions(FILE* f) {
- fputs("\n;===Procedure ng_======================\n",f);
- fputs("\nng_ PROC\n\n",f);
- fputs("\tpush ax\n",f);
- fputs("\tpush bx\n",f);
- fputs("\tpush cx\n",f);
- fputs("\tpush dx\n",f);
- fputs("\tpushf\n",f);
- fputs("\tfistp lb1\n",f);
- fputs("\tfistp lb2\n",f);
- fputs("\tmov ax,word ptr lb1\n",f);
- fputs("\tmov bx,word ptr lb1+2\n",f);
- fputs("\tmov cx,word ptr lb2\n",f);
- fputs("\tmov dx,word ptr lb2+2\n",f);
- fputs("\tcmp dx,bx\n",f);
- fputs("\tjg gr\n",f);
- fputs("\tjl lo\n",f);
- fputs("\tcmp cx,ax\n",f);
- fputs("\tja gr\n",f);
- fputs("\n lo:\n",f);
- fputs("\tfld1\n",f);
- fputs("\tjmp l_ng\n",f);
- fputs("\n gr:\n",f);
- fputs("\tfldz\n",f);
- fputs("\n l_ng:\n",f);
- fputs("\tpopf\n",f);
- fputs("\tpop dx\n",f);
- fputs("\tpop cx\n",f);
- fputs("\tpop bx\n",f);
- fputs("\tpop ax\n\n",f);
- fputs("\tret\n",f);
- fputs("ng_ ENDP\n",f);
- fputs(";======================================\n\n",f);
- }
- void generate_code(FILE* f, char* in) {
- print_title(f,in);
- look_for_get_and_put();
- print_segment(f);
- print_initialization(f);
- print_code(f);
- print_ending(f);
- }
- int main(int argc, char* argv[]) {
- // if(argc != 2) {
- // puts("Pomylka!!\nNekorektno vvedeni parametry");
- // puts("\n\tS12 <file_name>.s138");
- // return 0;
- // }
- strncpy(segment.input_file_name, "source.s128",50);
- input_file = fopen(segment.input_file_name,"r");
- if(input_file == NULL) {
- printf("Pomylka!!\nNe Vdalos\' vidkryty fajl %s\n", segment.input_file_name);
- return -1;
- }
- strncpy(segment.output_file_name,segment.input_file_name,50);
- strncpy(strchr(segment.output_file_name,'.'),".asm",50);
- puts("s138 Compiler");
- puts("Designed by Anton Stoyan \n");
- output_file = fopen(segment.output_file_name,"w");
- error_file = fopen("ErrorOut.txt","w");
- segment.Len = analyze(input_file);
- print_analyze();
- segment.errors = check_program(error_file);
- if(segment.errors != 0) {
- printf("Found %d errors\ncan\'t compile input program\nsee errors in ErrorFile.txt\n",segment.errors);
- } else {
- printf("Find no errors.\nOutput file assign: %s\n",segment.output_file_name);
- create_identifier_table(error_file);
- generate_code(output_file, segment.input_file_name);
- }
- fclose(input_file);
- fclose(output_file);
- fclose(error_file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement