• 大小: 55KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-17
  • 语言: C/C++
  • 标签: c  yuyan  masm32  assamble  

资源简介

前段时间自己业余抽空花了三个月编了一个c语言编译器,将c语言编译成masm32。总共4个文件,2000多行的代码,其中sca.c是词法,p.c生成语法树,table.c是符号表,g.c是转化成具体masm32代码,只支持char,short,int,double四种类型数据,其他的有兴趣的可以自己修改增加,由于masm32限制,目前该编译器只支持局部变量在函数开头定义,全局变量不能和局部变量同名。不支持变量强制转变,也不支持switch语句,个人感觉swich没用,#include不支持,只支持“ ”,现在打包给大家分享,包括源代码(感兴趣的可以自己修改或者转发学习,不允许商业用途),里面还有一些例子,想学习编译原理或者masm32的同志可以看看(特别是学习masm32的,可以把写好的c语言直接编译成masm32代码)!遇到问题可以跟我联系,尽量跟你们回复!

资源截图

代码片段和文件信息

#include “table.c“
void error_gencode(TreeNode *);
void printtree(TreeNode *);
void gencode(TreeNode *);
void op_pointer(TreeNode *);
void op_array(TreeNode *);
void op_struct(TreeNode *);
void op_inc(TreeNode *);
void op_para(TreeNode *);
void op_call(TreeNode *);
void cmp_handle(TreeNode *char *int);
void exp_handle(TreeNode *char *int);
void load_var(TreeNode *char *int);
void print_g();
void print_l(TreeNode *);
int sizecal(TreeNode *);
int double_test(TreeNode *);
void getmainarg(TreeNode *);
int get_level(TreeNode *);

void error_gencode(TreeNode *p){
    printf(“GENCODE ERROR:file %s line %d errormsg=%s\n“filenamep->linenoerrormsg);
    exit(1);
}
int get_level(TreeNode *p){
int level=0;
if(p->t==ARRAY){
level=1;
p=p->child[1];
while(p&&p->t==LBRACE){
level++;
p=p->child[1];
}
}
return level;
}
int sizecal(TreeNode *p){
TreeNode *q*t*k;
int offset=0size=0maxsize=0;
q=p->child[0]->child[0]->child[1];
while(q){
t=q->child[0];
while(t){
k=t->child[0];
if(k->t==POINTER){
size=4;
if(offset%size)offset=(offset/size+1)*size;
offset=offset+4;
}else{
size=size_of_type(q->t);
if(offset%size)offset=(offset/size+1)*size;
if(k->t==ID)offset=offset+size;
if(k->t==ARRAY)offset=offset+size*get_array_num(k);
}
if(size>maxsize)maxsize=size;
t=t->child[1];
}
q=q->child[1];
}
//if(offset%maxsize)offset=(offset/maxsize+1)*maxsize;
//printf(“size=%d\n“offset);
return offset;
}
void assign_struct(TreeNode *p){
TreeNode *pl*pr*temp*t;
pl=p->child[0];
pr=p->child[1];
temp=test(pl);
if(temp->t==ARRAY){printtree(pl);printf(“\timul esitype _%s\n\tlea edi[ebx+esi]\n“temp->temp);}
else printf(“\tlea edi_%s\n“pl->id);
if(pr->t==DOT||pr->t==ARROW){
op_struct(pr);
t=test(pr);
if(t->t==ARRAY){op_array(pr);printf(“\timul esitype _%s\n\tlea esi[ebx+esi]\n“temp->temp);}
else printf(“\tlea esi%s\n“pr->id);
}else if(pr->t==ARRAY){op_array(pr);printf(“\timul esitype _%s\n\tlea esi[ebx+esi]\n“temp->temp);}
else printf(“\tlea esi_%s\n“pr->id);
printf(“\tmov ecxtype _%s\n\trep movsb\n“temp->temp);
}
int double_test(TreeNode *p){
    TreeNode *temp;
    if(!p){p=NewNode(ERROR);errormsg=“double test 1“;error_gencode(p);}
    if(p->t==REAL)return 1;
    if(p->t==ID){
        temp=test(p);
        if(temp->t==VAR&&temp->type==DOUBLE)return 1;
    }
    if(p->t==ARRAY){temp=test(p);if(temp->type==DOUBLE)return 1;}
    if(p->t==DOT||p->t==ARROW){temp=struct_test(p);if(temp->type==DOUBLE)return 1;}
    if(p->t==POINTER){
        while(p){
            if(p->t==ID)break;
            p=p->child[0];
        }
        if(!p){p=NewNode(ERROR);errormsg=“double_test 2“;error_gencode(p);}
        temp=test(p);
        if(temp->type==DOUBLE)return 1;
    }
    if(p->t==CAL||p->t==CMP||p->t==ASSIGN){
        int flfr;
        fl=double_test(p->child[0]);
        fr=doub

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      89923  2018-10-15 14:10  bianyi\a.exe

     文件         90  2018-09-04 16:23  bianyi\example\beep.c

     文件       1584  2018-09-18 09:37  bianyi\example\bm.c

     文件       5486  2018-10-11 15:12  bianyi\example\bp.c

     文件       3213  2018-09-14 09:07  bianyi\example\cal.c

     文件       1563  2018-09-11 18:32  bianyi\example\find.c

     文件       1154  2018-10-04 14:41  bianyi\example\kmp.c

     文件       2537  2018-10-15 10:07  bianyi\example\maze.c

     文件       6124  2018-10-03 17:24  bianyi\example\pe.c

     文件        862  2018-09-11 14:08  bianyi\example\q.c

     文件       3951  2018-09-10 11:54  bianyi\example\scan.c

     文件       1417  2018-09-17 08:43  bianyi\example\t.c

     文件        101  2018-10-05 11:46  bianyi\example\win.c

     文件       7576  2018-09-17 14:33  bianyi\example\x.c

     文件      43870  2018-10-12 09:59  bianyi\g.c

     文件      15903  2018-10-12 08:46  bianyi\p.c

     文件       7851  2018-10-08 16:04  bianyi\sca.c

     文件       3358  2018-10-15 10:41  bianyi\stdio.h

     文件      15568  2018-10-12 09:22  bianyi\table.c

     目录          0  2018-10-15 14:14  bianyi\example

     目录          0  2018-10-15 14:10  bianyi

----------- ---------  ---------- -----  ----

               212131                    21


评论

共有 条评论