LC2K C语言代码,求写剩下的几个函数

dragonir 2015-10-30 11:33:39

#include "../inc/TestHarness.h"
#include <assert.h>
#include "Assemb.h"
#include <iostream>

//Complete the following implementation

bool submission = false; // set to true if you want to submit
LPCTSTR m_szUserName =
LPCTSTR m_szFtpUser =
LPCTSTR m_szFtpPassword =
LPCTSTR m_szFtpName = "
INTERNET_PORT m_nPort =
LPCTSTR m_szFtpDirectory =

Assemb::~Assemb()
{

}

Assemb::Assemb()
{
numInst = 0;
}
Assemb::Assemb(FILE *in, FILE *out):inf(in), outf(out)
{
//....
numInst = 0;
}

Assemb::Assemb(char *in, char *out)
{
inf = fopen(in, "r");
outf = fopen(out, "w");
//....
numInst = 0;
}

int Assemb::encoding(Inst ins)
{
int ans = 0;
int reg0, reg1, reg2, reg3;

sscanf(ins.arg0, "%d", ®0);
sscanf(ins.arg1, "%d", ®1);
sscanf(ins.arg2, "%d", ®2);
reg3 = (reg2 >= 0) ? (reg2) : ((1 << 16) + reg2);

if(strcmp(ins.opcode, "noop") == 0)
ans = (1<<22) + (1<<23) + (1<<24);
if(strcmp(ins.opcode, "halt") == 0)
ans = (1<<23) + (1<<24);
if(strcmp(ins.opcode, "jalr") == 0)
ans = (reg0 << 19) + (reg1 << 16);
if(strcmp(ins.opcode, ".fill") == 0)
ans = reg0;
if(strcmp(ins.opcode, "add") == 0)
ans = (reg0 << 19) + (reg1 << 16) + reg2;
if(strcmp(ins.opcode, "nand") == 0)
ans = (1 << 22) + (reg0 << 19) + (reg1 << 16) + reg2;
if(strcmp(ins.opcode, "lw") == 0)
ans = (2 << 22) + (reg0 << 19) + (reg1 << 16) + reg3;
if(strcmp(ins.opcode, "sw") == 0)
ans = (3 << 22) + (reg0 << 19) + (reg1 << 16) + reg3;
if(strcmp(ins.opcode, "beq") == 0)
ans = (4 << 22) + (reg0 << 19) + (reg1 << 16) + reg3;
return ans;
}
int Assemb::readinAllInst() //read in all instructions from source file
{
char line[MAXLINELENGTH];
char *ptr = line;
ins.label[0] = ins.opcode[0] = ins.arg0[0] = ins.arg1[0] = ins.arg2[0] = '\0';
if (fgets(line, MAXLINELENGTH, inf) == NULL) {
return(0);
}
if (strchr(line, '\n') == NULL) {
printf("error: line too long\n");
return(0);
}
ptr = line;
if (sscanf(ptr, "%[^\t\n ]", ins.label)) {
ptr += strlen(ins.label);
}
sscanf(ptr, "%*[\t\n ]%[^\t\n ]%*[\t\n ]%[^\t\n ]%*[\t\n ]%[^\t\n ]%*[\t\n ]%[^\t\n ]",
ins.opcode, ins.arg0, ins.arg1, ins.arg2);
return(1);
}

int Assemb::calculateLabels() //
{ //encoding(Inst ins)
;
return 0;
}

int Assemb::translateIntoMachineCode()
{
int ins;
encoding(ins)
return 0;
}

int Assemb::printOutMachineCode()
{
return 0;
}

int Assemb::readinInst(Inst& ins)
{
char line[MAXLINELENGTH];
char *ptr = line;

/* delete prior values */
ins.label[0] = ins.opcode[0] = ins.arg0[0] = ins.arg1[0] = ins.arg2[0] = '\0';

/* read the line from the assembly-language file */
if (fgets(line, MAXLINELENGTH, inf) == NULL) {
/* reached end of file */
return(0);
}

/* check for line too long (by looking for a \n) */
if (strchr(line, '\n') == NULL) {
/* line too long */
printf("error: line too long\n");
return(0);
}

/* is there a label? */
ptr = line;
if (sscanf(ptr, "%[^\t\n ]", ins.label)) {
/* successfully read label; advance pointer over the label */
ptr += strlen(ins.label);
}

/*
* Parse the rest of the line. Would be nice to have real regular
* expressions, but scanf will suffice.
*/
sscanf(ptr, "%*[\t\n ]%[^\t\n ]%*[\t\n ]%[^\t\n ]%*[\t\n ]%[^\t\n ]%*[\t\n ]%[^\t\n ]",
ins.opcode, ins.arg0, ins.arg1, ins.arg2);

///////////////////////////////////////////////////////////////////////

return(1);
}

...全文
306 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧