⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 instsystem_c.cpp

📁 单片机宏汇编器的源程序。给一些爱好者作为学习编译原理和 C 程序设计的例子.
💻 CPP
📖 第 1 页 / 共 4 页
字号:
//---------------------------------------------------------------------------
//-------- InstSystem_C.cpp -------------------------------------------------
//---------------------------------------------------------------------------

#include "InstSystem_H.h"
//---------------------------------------------------------------------------

//------ 构造器 -------------------------------------------------------------
InstSystem::InstSystem()
{ for(register Resvs* pt = InstSET; *(pt->name); pt++ ) // 建哈希表
   { insert(GetHashNo(pt->name), pt); } // end for pt
  for(register OPset* it = OPlist; it->len; it++ )
   { OPinsert( GetEntryNo(it->OPnum, it->ODmode[0], it->ODmode[1]),
               (ITset*)it );
   } // end for it
} // end constructor
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// 由保留字串, 到Token的转换。
// 如果找不到,返回Token=NULL,val=0.
//---------------------------------------------------------------------------
int16u InstSystem::InsRecg(const char* word, int16u &val, bool dog)
{ for(register Resvs* pt = hashTbl[GetHashNo(word)]; pt; pt = pt->next )
   { if( !strcmp(word,pt->name) )  // if equal
      { val = pt->typ;
        if( !dog || ( dog && val == Dog_Ctrl) )
         { return pt->tken; } // endif
      } // endif
   } // end for
  // not found
  val = 0; return NULL;
} // end InsRecg
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// 由指令操作码以及操作数寻址方式 到OPset的转换。
// 如果找不到匹配的寻址方式,返回NULL。
//---------------------------------------------------------------------------
OPset* InstSystem::OPsetRecg(int8u OP, int8 OD1, int8 OD2, int8 OD3)
{ register int32u key = PackToKey(OP, OD1, OD2, OD3);
  for( register ITset* it = OPhash[ GetEntryNo(OP, OD1, OD2) ];
          it;        // condition
              it = it->next )
   { if( it->key == key ) { return (OPset*)it; } // endif   found!
   } // end for
   // not found
  return NULL;
} // end OPsetRecg
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
void InstSystem::show()const
{ int8u ct[256];
  for(register int16u i=0; i<256; i++)  { ct[i] = 0; } // end for

  int16u count = 0;
  for(int16u i=0; i<256; i++)
   { printf("\n[%xH]:",i);
     for( Resvs* p = hashTbl[i]; p; p = p->next )
      { printf("  -[%s]",(char*)(p->name));
        ct[i]++;
      } // end for p
     count += ct[i]*ct[i];
   } // end for i

  int8u ac[64];
  for(register int8u i = 0; i<64; i++) { ac[i] = 0; } // end for

  for(register int16u i=0; i<256; i++)
   { ac[ct[i]]++; } // endfor

  printf("\n---------------------------\n");
  for(int8u ii = 0; ii < 64; ii++ )
   { if(ac[ii]) { printf("ac[%d]=%d \t", ii, ac[ii]); } // endif
   } // end for
  printf("\n------  count=[%d] -------", count);

  printf("\n\n--------- Instructions ----------\n");
  for(register int16u i=0; i<256; i++)  { ct[i] = 0; } // end for
  count = 0;
  for(int16u i=0; i<256; i++)
   { printf("\n[%xH]:",i);
     for( ITset* p = OPhash[i]; p; p = p->next )
      { printf("  -[%s(%d)]",TokenToName( ((OPset*)p)->OPnum + 0x0200 ),p->len );
        ct[i]++;
      } // end for p
     count += ct[i]*ct[i];
   } // end for i

  for(register int8u i = 0; i<64; i++) { ac[i] = 0; } // end for

  for(register int16u i=0; i<256; i++)
   { ac[ct[i]]++; } // endfor

  printf("\n---------------------------\n");
  for(int8u ii = 0; ii < 64; ii++ )
   { if(ac[ii]) { printf("\tac[%d]=%d  ", ii, ac[ii]); } // endif
   } // end for
  printf("\n------  count=[%d] -------", count);

} // end show
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// 这里存放的是各指令需要的操作数的个数。
//---------------------------------------------------------------------------
const int8 InstSystem::InstArgus[]={
/* #define  NOP	   0x0200 */	0,
/* #define  MOV	   0x0201 */	2,
/* #define  MOVC   0x0202 */	2,
/* #define  MOVX   0x0203 */	2,
/* #define  PUSH   0x0204 */	1,
/* #define  POP	   0x0205 */	1,
/* #define  XCH	   0x0206 */	2,
/* #define  XCHD   0x0207 */	2,
/* #define  SWAP   0x0208 */	1,
/* #define  ADD	   0x0209 */	2,
/* #define  DJNZ   0x020a */	2,
/* #define  ADDC   0x020b */	2,
/* #define  SUBB   0x020c */	2,
/* #define  INC	   0x020d */	1,
/* #define  DEC	   0x020e */	1,
/* #define  MUL	   0x020f */	1,

/* #define  DIV	   0x0210 */	1,
/* #define  DA	   0x0211 */	1,
/* #define  ANL	   0x0212 */	2,
/* #define  ORL	   0x0213 */	2,
/* #define  XRL	   0x0214 */	2,
/* #define  CLR	   0x0215 */	1,
/* #define  CPL	   0x0216 */	1,
/* #define  RL	   0x0217 */	1,
/* #define  RLC	   0x0218 */	1,
/* #define  RR	   0x0219 */	1,
/* #define  RRC	   0x021a */	1,
/* #define  SETB   0x021b */	1,
/* #define  ACALL  0x021c */	1,
/* #define  LCALL  0x021d */	1,
/* #define  RET	   0x021e */	0,
/* #define  RETI   0x021f */	0,

/* #define  AJMP   0x0220 */	1,
/* #define  LJMP   0x0221 */	1,
/* #define  SJMP   0x0222 */	1,
/* #define  JZ	   0x0223 */	1,
/* #define  JNZ	   0x0224 */	1,
/* #define  JC	   0x0225 */	1,
/* #define  JNC	   0x0226 */	1,
/* #define  JB	   0x0227 */	2,
/* #define  JNB	   0x0228 */	2,
/* #define  JBC	   0x0229 */	2,
/* #define  CJNE   0x022a */	3,
/* #define  JMP    0x022b */	1,
/* #define  CALL   0x022c */	1  };  // end  InstArgus
//---------------------------------------------------------------------------

#define manNo 0x2c
//---------------------------------------------------------------------------
// 由Token查找指令参数个数。返回值为 -1, 0 ~ 127。
//---------------------------------------------------------------------------
int8 InstSystem::InstArgvSum(register int16u tok)
{ tok &= 0x00ff;
 if( tok <= manNo ) { return InstArgus[tok];  } //	endif
 printf("\nInstArgvSum out if run!! ");
 return	-1;	// There is error!
} // end InsRecg
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// 输入保留字的Token,反查保留字的字串。
//---------------------------------------------------------------------------
const char* InstSystem::TokenToName(register int16u Token)
{ for(register int16 no	= 0; no<256; no++)
  { for( register Resvs* pt = hashTbl[no]; pt; pt = pt->next )
     { if(Token == pt->tken) { return pt->name; }	// endif
     } // end for pt
  } // end for no
  return NULL;	   // Not found
} // end InsRecg
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// 输入一个号码,输出该号码对应的结构体指针
// action = -1,操作不存在
// action =  0,空操作
// action =  1,把第一操作数d1的字符串换成十六进制数,然后写入本身位置
// action =  2,把第二操作数d2的字符串换成十六进制数,然后写入本身位置
// action =  3,把第三操作数d3的字符串换成十六进制数,然后写入本身位置
// action =  4,把第一操作数d1的字符串换成十六进制数,然后高位写入本身位置,低位写入下一字节
// action =  5,把第二操作数d2的字符串换成十六进制数,然后高位写入本身位置,低位写入下一字节
// action = 11,把第一操作数d1的字符串换成相对偏移,然后写入本身位置
// action = 12,把第二操作数d2的字符串换成相对偏移,然后写入本身位置
// action = 13,把第三操作数d3的字符串换成相对偏移,然后写入本身位置
// action = 10,把机器码opton按照H3 L8修改
//---------------------------------------------------------------------------
// 查指令表专用函数,输入行号,输出指针  no<=254!!!   默认next=NULL
//---------------------------------------------------------------------------
OPset InstSystem::OPlist[] = {
 //  Opnum OD1   OD2   OD3   OPobj action len
   { SJMP, _slabl, -1, -1,   0x80, Wrel_1, -1,     2 },        // SJMP label  0
   { AJMP, _sdirt, -1, -1,   0x01, H3L8_0, -1,     2 },        // AJMP  H3L8  1
   { AJMP,  _num,  -1, -1,   0x01, H3L8_0, -1,     2 },        // AJMP  H3L8  2
   { LJMP, _ldirt, -1, -1,   0x02, H8L8_1,  0,     3 },        // LJMP  H8L8  3
   { LJMP,  _num,  -1, -1,   0x02, H8L8_1,  0,     3 },        // LJMP  H8L8  4
   { ACALL,_sdirt, -1, -1,   0x11, H3L8_0, -1,     2 },        // LCALL H3L8  5
   { ACALL, _num,  -1, -1,   0x11, H3L8_0, -1,     2 },        // LCALL H3L8  6
   { LCALL,_ldirt, -1, -1,   0x12, H8L8_1,  0,     3 },        // LCALL H8L8  7
   { LCALL, _num,  -1, -1,   0x12, H8L8_1,  0,     3 },        // LCALL H8L8  8

   { JMP,  _ldirt, -1, -1,   0x02, JJudge,  0,     3 },        // JMP  H8L8
   { JMP,  _num,   -1, -1,   0x02, JJudge,  0,     3 },        // JMP  H8L8
   { CALL, _ldirt, -1, -1,   0x12, JJudge,  0,     3 },        // CALL H8L8
   { CALL, _num,   -1, -1,   0x12, JJudge,  0,     3 },        // CALL H8L8
   { SJMP, _num,   -1, -1,   0x80, Wrel_1, -1,     2 },        // SJMP label  0

 // --------------
   { JMP, _aA_DPTR,-1, -1,   0x73, -1,     -1,     1 },        // JMP @A+DPTR
   { NOP,  -1,    -1,  -1,   0x00, -1,     -1,     1 },        // NOP

   {  RR,  _A,    -1,  -1,   0x03, -1,     -1,     1 },        //  RR A

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -