資源簡(jiǎn)介
c語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的匯編器,通過(guò)匯編器譯碼并保存譯碼結(jié)果到文件中,然后通過(guò)模擬器來(lái)實(shí)現(xiàn)對(duì)計(jì)算機(jī)的簡(jiǎn)單模擬
賓夕法尼亞大學(xué)的c語(yǔ)言課設(shè)

代碼片段和文件信息
#include?
#include?
#include?
#include?
#define?MAX_LEN?200
#define?INSTRS_COUNT?(sizeof(g_instrs_name)/sizeof(g_instrs_name[0]))
#define?INSTR_SYM?{“HLT““JMP““CJMP““OJMP““CALL““RET““PUSH““POP“\
“LOADB““LOADW““STOREB““STOREW““LOADI““NOP““IN““OUT““ADD““ADDI“\
????“SUB““SUBI““MUL““DIV““AND““OR““NOR““NOTB““SAL““SAR““EQU“\
????“LT““LTE““NOTC“}
typedef?struct?var
{
????char?var_name[10];
????int?pianyi1;
????int?changdu;
????unsigned?char?*length;
????struct?var?*next;
}VAR;??//用于表示變量的偏移量,變量的類型,變量所需要的內(nèi)存大小
typedef?struct?label
{
????char?label_name[10];
????int?pianyi2;
????struct?label?*next;
}LABEL;?????//用于表示標(biāo)號(hào)的名字,及標(biāo)號(hào)的地址
VAR?*var_head=NULL;
LABEL?*label_head=NULL;
const?char?*g_instrs_name[]=INSTR_SYM;?????????/*定義存放指令記號(hào)的數(shù)組*/
const?char?instr_format[33]=“12222133444451667575777778778881“;/*定義存放指令格式的數(shù)組,下標(biāo)對(duì)應(yīng)指令碼,7個(gè)數(shù)字字符代表七種指令格式*/
int?GetInstrCode(const?char?*op_sym);??????????/*由指令助記符得到指令代碼*/
unsigned?long?TransToCode(char?*instr_lineint?instr_num);/*將指令翻譯成目標(biāo)代碼,instr_num指令操作碼*/
int?GetRegNum(char?*instr_linechar?*reg_name);/*由寄存器名對(duì)應(yīng)到編碼*/
void?CreatList(VAR**vhdLABEL**lhdFILE*pfIn);/*創(chuàng)建鏈表*/
int?shuzu(char[10]);/*通過(guò)含有[]的字符串取出數(shù)組的大小*/
int?fuzhi1(char?s[MAX_LEN]unsigned?char?*pc);
int?fuzhi2(char?s[MAX_LEN]unsigned?char?*pc);/*數(shù)組元素賦值*/
LABEL*?Findlabel(LABEL*label_hdchar?s[20]);
VAR*?Findvar(VAR*var_hdchar?s[20]);
int?main(int?argcchar?*?*argv)
{
????char?a_line[MAX_LEN];???//用于存儲(chǔ)一行的字符
????char?op_sym[8];?????????//用于存儲(chǔ)助記符用于存儲(chǔ)變量名字
????int??op_num;????????????//用于表示助記符對(duì)應(yīng)的操作碼
????int?ilength_all=0;??????????????????//pianyi用于標(biāo)記偏移量
????char?*pcPos;????????????//用于保存注釋符的位置
????VAR?*pv=NULL;
????unsigned?long?addr=0ul*shuju;
????unsigned?char?*all;
????FILE?*pfIn*pfOut;??????//源文件,目標(biāo)文件
????int?n;
????if(argc<3)
????{???????????????????????????????/*檢查命令行參數(shù)的數(shù)目*/
????????printf(“ERROR:no?enough?command?line?arguments!\n“);
????????return?0;
????}
????if((pfIn=fopen(argv[1]“r“))==NULL){??????/*打開(kāi)源代碼文件*/
????????printf(“ERROR:cannot?open?file?%s?for?reading!\n“argv[1]);
????????return?0;
????}
????if((pfOut=fopen(argv[2]“w“))==NULL){?????/*打開(kāi)目標(biāo)代碼文件*/
????????printf(“ERROR:cannot?open?file?%s?for?writing!\n“argv[2]);
????????return?0;
????}
????CreatList(&var_head&label_headpfIn);/*第一次循環(huán)處理偽指令和標(biāo)號(hào),創(chuàng)建鏈表*/
????pv=(VAR*)malloc(sizeof(VAR));
????pv=var_head;
????pfIn=fopen(argv[1]“r“);
????while(!feof(pfIn))//第二次循環(huán)處理指令
????{
????????fgets(a_lineMAX_LENpfIn);
????????if((pcPos=strchr(a_line‘#‘))!=NULL)
????????{
????????????*pcPos=‘\0‘;
????????}
????????if((pcPos=strchr(a_line‘\n‘))!=NULL)
????????{
????????????*pcPos=‘\0‘;
????????}
????????if((pcPos=strstr(a_line“WORD“))!=NULL)
????????{
????????????continue;
????????}
????????else?if((pcPos=strstr(a_line“BYTE“))!=NULL)
????????{
????????????continue;
????????}
????????else?if((pcPos=strchr(a_line‘:‘))!=NULL)
????????{
???????????
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????1041??2014-03-16?21:37??copy.txt
?????文件????????9733??2014-03-16?21:37??queen.txt
?????文件?????????199??2014-03-30?18:45??readme.txt
?????目錄???????????0??2014-03-30?18:41??sas\
?????目錄???????????0??2014-03-16?21:37??sas\bin\
?????目錄???????????0??2014-03-30?18:41??sas\bin\Debug\
?????文件???????41970??2014-03-16?21:37??sas\bin\Debug\sas.exe
?????文件?????????168??2014-03-16?21:37??sas\copy.dat
?????文件????????1041??2014-03-16?21:37??sas\copy.txt
?????文件???????15429??2014-03-16?21:37??sas\main.c
?????目錄???????????0??2014-03-30?18:39??sas\obj\
?????目錄???????????0??2014-03-30?18:41??sas\obj\Debug\
?????文件???????14381??2014-03-30?18:39??sas\obj\Debug\main.o
?????文件????????9733??2014-03-16?21:37??sas\queen.txt
?????文件????????1111??2014-03-17?00:34??sas\sas.cbp
?????文件?????????445??2014-03-16?21:40??sas\sas.depend
?????文件?????????321??2014-03-17?00:34??sas\sas.layout
?????目錄???????????0??2014-03-30?18:41??ssim\
?????目錄???????????0??2014-03-16?21:37??ssim\bin\
?????目錄???????????0??2014-03-30?18:41??ssim\bin\Debug\
?????文件???????37773??2014-03-16?21:37??ssim\bin\Debug\ssim.exe
?????文件?????????360??2014-03-16?21:37??ssim\copy.dat
?????文件????????8287??2014-03-16?21:37??ssim\main.c
?????目錄???????????0??2014-03-16?21:37??ssim\obj\
?????目錄???????????0??2014-03-30?18:41??ssim\obj\Debug\
?????文件???????16666??2014-03-16?21:37??ssim\obj\Debug\main.o
?????文件????????1872??2014-03-16?21:37??ssim\queen.dat
?????文件????????1088??2014-03-16?21:37??ssim\ssim.cbp
?????文件?????????428??2014-03-16?21:37??ssim\ssim.depend
?????文件?????????241??2014-03-16?21:37??ssim\ssim.layout
評(píng)論
共有 條評(píng)論