📄 decode.c
字号:
/*
* Copyright 2002 by Texas Instruments Incorporated.
* All rights reserved. Property of Texas Instruments Incorporated.
* Restricted rights to use, duplicate or disclose this code are
* granted through contract.
*
*/
#include "decode.h"
#include "instr.h"
#include "utils.h"
/* Decode the input program */
void decode(FILE * in_file){
/* Read the file one string at a time */
char instr[MAX_CHAR];
char dest[MAX_CHAR];
char src[MAX_CHAR];
char line[1024];
char temp[100];
unsigned long line_no = 1;
printf("Started Decoding...\n");
while (fscanf(in_file,"%[^;\n]",line) != EOF){
if (is_space(line)){ /* Comment */
/* Read till end of line */
fscanf(in_file,"%[^\n]",temp);
/* Read the \n charater */
fgetc(in_file);
line_no++;
strcpy(line,"");
continue;
}
printf("%s\n",line);
/* Read the instruction */
sscanf(line,"%s",instr);
if (!strcmp(instr,"READ") || !strcmp(instr,"WRITE")){
/* Read the operand */
sscanf(line,"%s %s",instr, src);
if (strlen(src) != 1){
printf("Error: Line %d. Expecting operand...Aborting\n",line_no,instr);
return;
}
if (!strcmp(instr,"READ")){
read_src(src);
}
else if (!strcmp(instr,"WRITE")){
write_dest(src);
}
/* Read till end of line */
fscanf(in_file,"%[^\n]",temp);
/* Read the new line character */
fgetc(in_file);
line_no++;
strcpy(line,"");
continue;
}
/* Assume two operand instructions */
/* Read the operands */
sscanf(line,"%s %s %s",instr,dest,src);
if (strlen(dest) != 1){
printf("Error: Line %d. Expecting operand %s...Aborting\n",line_no,line);
return;
}
if ( (strlen(src) != 1) && !(isnum(src)) ){
printf("Error: Line %d. Expecting operand...Aborting\n",line_no,instr);
return;
}
if (!strcmp(instr,"ADD")){
add(dest, src);
}
else if (!strcmp(instr,"ADDI")){
if (!isnum(src)){
printf("Error: Line %d. Illegal operand...Aborting\n",line_no,src);
return;
}
addi(dest, src);
}
else if (!strcmp(instr,"SUB")){
sub(dest, src);
}
else if (!strcmp(instr,"SUBI")){
if (!isnum(src)){
printf("Error: Line %d. Illegal operand...Aborting\n",line_no,src);
return;
}
subi(dest, src);
}
else if (!strcmp(instr,"MULT")){
mult(dest, src);
}
else if (!strcmp(instr, "MULTI")){
if (!isnum(src)){
printf("Error: Line %d. Illegal operand...Aborting\n",line_no,src);
return;
}
multi(dest, src);
}
else if (!strcmp(instr,"DIV")){
my_div(dest, src);
}
else if (!strcmp(instr,"DIVI")){
if (!isnum(src)){
printf("Error: Line %d. Illegal operand...Aborting\n",line_no,src);
return;
}
divi(dest, src);
}
else if (!strcmp(instr,"MOV")){
mov(dest, src);
}
else if (!strcmp(instr,"MOVI")){
if (!isnum(src)){
printf("Error: Line %d. Illegal operand...Aborting\n",line_no,src);
return;
}
movi(dest, src);
}
else{
printf("Illegal instruction %s at line %d\n",instr, line_no);
}
/* Read till end of line */
fscanf(in_file,"%[^\n]",temp);
/* Read the new line character*/
fgetc(in_file);
strcpy(line,"");
line_no++;
}
printf("Stopped Decoding...\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -