MIPS汇编器无法处理test.asm文件,请求协助排查修复
MIPS汇编器故障排查求助
输入文件 test.asm 内容
.data .integer 120 .dbyte 8 .integer 121 .text addi $1,$0,10 lw $2,123($4) sw $3,4($1) add $10,$0,$0 sub $11,$0,$0 sll $4,$4,10 lui $5,123 slt $10,$0,$0 ori $11,$0,10 addu $6,$10,$13
编写的MIPS汇编器代码
#include <stdio.h> #include <string.h> #include <stdlib.h> // synthesized missing definitions ... #if 1 #define TEXT_SEGMENT 0x0200 #define DATA_SEGMENT 0x0000 #define MAX_SIZE 1024 typedef struct { char *name; int n; int size; } Directive; Directive directives[1000]; typedef struct { char *name; int opcode; int funct; } Instruction; Instruction instructions[1000]; #endif // forward function declarations #if 1 void assemble(char *filename, int *text_segment, int *data_segment); Instruction *getInstruction(char *instruction); Directive *getDirective(char *directive); int getRegisterNumber(char *regis); int parseInstruction(char *instruction, int *opcode, int *funct); int parseDirective(char *directive, int *type, int *size); #endif int main(int argc, char **argv) { // Checking command line arguments if (argc < 2) { printf("Usage: %s <test.asm> <test.out>\n", argv[0]); return -1; } // Initializing memory int text_segment[MAX_SIZE]; int data_segment[MAX_SIZE]; // NOTE/BUG: compiler warns about third arg being too small #if 0 memset(text_segment, 0, MAX_SIZE); memset(data_segment, 0, MAX_SIZE); #else memset(text_segment, 0, sizeof(text_segment)); memset(data_segment, 0, sizeof(data_segment)); #endif // Writing to output file FILE *fp = fopen(argv[2], "w"); fwrite(text_segment, 1, MAX_SIZE, fp); fwrite(data_segment, 1, MAX_SIZE, fp); fclose(fp); return 0; } // Assembling the input file void assemble(char *filename, int *text_segment, int *data_segment) { // NOTE/BUG: line_number not used #if 0 int line_number = 0; #endif int text_offset = 0; int data_offset = 0; char line[256]; FILE *fp = fopen(filename, "r"); while (fgets(line, 256, fp)) { // Handling blank lines if (strcmp(line, "\n") == 0) { continue; } // Parsing line // NOTE/BUG: compiler warning: label set but not used char *label = NULL; char *instruction = NULL; char *directive = NULL; char *operands = NULL; label = strtok(line, ":"); instruction = strtok(NULL, " \n\t"); directive = strtok(NULL, " \n\t"); operands = strtok(NULL, "\n\t"); // Handling instructions if (instruction) { // Parsing instruction int opcode = 0; int funct = 0; int rs = 0; int rt = 0; int rd = 0; int imm = 0; int add = 0; if (parseInstruction(instruction, &opcode, &funct)) { rs = getRegisterNumber(strtok(operands, ",")); rt = getRegisterNumber(strtok(NULL, ",")); if (opcode == 0) { rd = getRegisterNumber(strtok(NULL, ",")); } else { imm = atoi(strtok(NULL, ",")); } // Generating machine code int machine_code = 0; machine_code |= (opcode << 26); machine_code |= (rs << 21); machine_code |= (rt << 16); machine_code |= (rd << 11); machine_code |= (imm << 0); machine_code |= (add << 0); machine_code |= (funct << 0); // Writing machine code to text segment text_segment[text_offset] = machine_code; text_offset += 1; } // Handling directives if (directive) { // Parsing directive int type = 0; int size = 0; if (parseDirective(directive, &type, &size)) { char *token = strtok(operands, ","); while (token) { int value = 0; sscanf(token, "%d", &value); // Handling .dbyte if (type == 2) { // NOTE/BUG: statement with no effect #if 0 data_segment; #endif // Writing value to data segments data_segment[data_offset] = value; data_offset += 1; } // Handling .integer if (type == 3) { // Writing value to data segment data_segment[data_offset] = value; data_offset += 1; } token = strtok(NULL, ","); } } } } fclose(fp); } } // Getting MIPS instruction Instruction * getInstruction(char *instruction) { int i = 0; while (instructions[i].name) { if (strcmp(instructions[i].name, instruction) == 0) { return &instructions[i]; } i++; } return NULL; } // Getting MIPS directive Directive * getDirective(char *directive) { int i = 0; while (directives[i].name) { if (strcmp(directives[i].name, directive)) { return &directives[i]; } i++; } return NULL; } // Getting register number int getRegisterNumber(char *regis) { int reg = 0; sscanf(regis, "$%d", ®); return reg; } // Parsing instruction int parseInstruction(char *instruction, int *opcode, int *funct) { for (int i = 0; instructions[i].name; i++) { if (strcmp(instruction, instructions[i].name) == 0) { *opcode = instructions[i].opcode; *funct = instructions[i].funct; return 1; } } return 0; } // Parsing directive int parseDirective(char *directive, int *type, int *size) { Directive *dir = getDirective(directive); if (dir) { *type = dir->n; *size = dir->size; return 1; } else { return 0; } }
问题
该汇编器无法正常运行,无法读取上述test.asm输入文件并生成包含指定指令的二进制输出,请求协助排查问题。
内容的提问来源于stack exchange,提问作者Andrew Schelb
相关产品推荐
相关产品推荐

