2016-10-05 2 views
1

간단한 CPU를 모방 한 학교용 코드를 작성 중입니다. 세분화 오류가 무엇인지는 알고 있지만 내 코드가 무엇이 잘못되었는지는 알 수 없습니다. 머리글 또는 주요 아무 문제가 없습니다접합 오류 (코어 덤프)

, 내가 여기

referrence 그것을 포함하고있어 내 헤더입니다 :

#define NUM_BYTES (16 * 1024) 
#define BYTES_PER_WORD 2 
#define WORDS_PER_INSTRUCTION 2 
#define NUM_WORDS (NUM_BYTES/BYTES_PER_WORD) 
#define NUM_INSTRUCTIONS (NUM_WORDS/WORDS_PER_INSTRUCTION) 
#define R0 0 
#define R1 1 
#define R2 2 
#define R3 3 
#define R4 4 
#define R5 5 

typedef unsigned short Machine_word; 

typedef enum { PLUS, MINUS, TIMES, DIV, NEG, AND, OR, NOT, LI, LW, SW, 
       MOVE, CMP, READ, WRITE, HALT } Opcode; 

typedef enum { LT, LE, GT, GE, EQ, NE } Comparison; 

typedef struct { 
    Opcode opcode; 
    Comparison comparison; 
    unsigned short reg1; 
    unsigned short reg2; 
    unsigned short reg3; 
    unsigned short addr_or_imm; 
} Instruction; 

void print_instruction(Instruction instr); 
int disassemble(const Machine_word memory[], int starting_addr, int num_instrs, 
       Instruction instrs[], int *const valid_instrs); 
int valid_instruction(Machine_word instr_word1, Machine_word instr_word2); 
int assemble(unsigned short opcode, unsigned short comparison, 
      unsigned short reg1, unsigned short reg2, unsigned short reg3, 
      unsigned short addr_or_imm, Machine_word *const word1, 
      Machine_word *const word2); 

내 주요 :

#include<stdio.h> 
#include "header.h" 

#define PROGRAM_SIZE 10 


int main() { 
    Machine_word words[NUM_WORDS]= {0x10a5,  0, /* 2 words of 1st instr. */ 
            0x80c0, 0x03ff, /* 2 words of 2nd instr. */ 
            0xa040, 0x00d8, /* etc. */ 
            0x5008,  0, 
            0x7080,  0, 
            0xc528, 0x21f8, 
            0x9080, 0x2718, 
            0xb058,  0, 
            0xe100,  0, 
            0xf000,  0}; 
    /* double braces because it's an array of structures */ 
    Instruction instrs[NUM_INSTRUCTIONS]= {{0}}; 
    int i, num_valid_instrs= 0; 

    disassemble(words, 0, PROGRAM_SIZE, instrs, &num_valid_instrs); 

    for (i= 0; i < num_valid_instrs; i++) { 
    print_instruction(instrs[i]); 
    printf("\n"); 
    } 

내 기능 : (첫 번째 기능 ha 아무 문제!)

#include<stdio.h> 
#include "machine.h" 


void print_instruction(Instruction instr) { 

    if (instr.opcode == HALT) { 
    printf("halt"); 
    } else if (instr.opcode == PLUS) { 
    printf("plus\tR%hu\tR%hu\tR%hu", instr.reg1, instr.reg2, instr.reg3); 
    } else if (instr.opcode == MINUS) { 
    printf("minus\tR%hu\tR%hu\tR%hu", instr.reg1, instr.reg2, instr.reg3); 
    } else if (instr.opcode == TIMES) { 
    printf("times\tR%hu\tR%hu\tR%hu", instr.reg1, instr.reg2, instr.reg3); 
    } else if (instr.opcode == DIV) { 
    printf("div\tR%hu\tR%hu\tR%hu", instr.reg1, instr.reg2, instr.reg3); 
    } else if (instr.opcode == NEG) { 
    printf("neg\tR%hu\tR%hu", instr.reg1, instr.reg2); 
    } else if (instr.opcode == AND) { 
    printf("and\tR%hu\tR%hu\tR%hu", instr.reg1, instr.reg2, instr.reg3); 
    } else if (instr.opcode == OR) { 
    printf("or\tR%hu\tR%hu\tR%hu", instr.reg1, instr.reg2, instr.reg3); 
    } else if (instr.opcode == NOT) { 
    printf("not\tR%hu\tR%hu", instr.reg1, instr.reg2); 
    } else if(instr.opcode == LI) { 
    printf("li\tR%hu\t%05hu", instr.reg1, instr.addr_or_imm); 
    }else if (instr.opcode == LW) { 
    printf("lw\tR%hu\t%05hu", instr.reg1, instr.addr_or_imm); 
    } else if (instr.opcode == SW) { 
    printf("sw\tR%hu\t%05hu", instr.reg1, instr.addr_or_imm); 
    } else if (instr.opcode == MOVE) { 
    printf("move\tR%hu\tR%hu", instr.reg1, instr.reg2); 
    } else if (instr.opcode == CMP) { 
    printf("cmp %d\tR%hu\tR%hu\t%05hu", (int)instr.comparison, instr.reg1, instr.reg2, instr.addr_or_imm); 
    } else if (instr.opcode == READ) { 
    printf("read\tR%hu", instr.reg1); 
    } else if (instr.opcode == WRITE) { 
    printf("write\tR%hu", instr.reg1); 
    } 
} 


int disassemble(const Machine_word memory[], int starting_addr, int num_instrs, Instruction instrs[], int *const valid_instrs) { 
    int i, count= 0, index= 0; 

    if(starting_addr % 4 != 0) 
    return 0; 
    if(starting_addr < 0 || starting_addr > 16384) 
    return 0; 
    if(starting_addr + num_instrs*4 > 16384) 
    return 0; 
    if(memory == NULL || instrs == NULL) 
    return 0; 

    for(i = starting_addr/2; i < i + num_instrs*2; i += 2){ 
    /*read instructions*/ 
    Machine_word opcode = memory[i]; 
    Machine_word comparison = memory[i]; 
    Machine_word reg1 = memory[i]; 
    Machine_word reg2 = memory[i]; 
    Machine_word reg3 = memory[i]; 
    Machine_word mem_addr = memory[i + 1]; 

    opcode = (opcode & 0xf000) >> 12; 
    comparison = (comparison & 0x0e00) >> 9; 
    reg1 = (reg1 & 0x01c0) >> 6; 
    reg2 = (reg2 & 0x0038) >> 3; 
    reg3 = (reg3 & 0x0007); 

    if(opcode == 0) { 
     instrs[index].opcode = PLUS; 
     instrs[index].reg1 = reg1; 
     instrs[index].reg2 = reg2; 
     instrs[index].reg3 = reg3; 
    } else if(opcode == 1) { 
     instrs[index].opcode = MINUS; 
     instrs[index].reg1 = reg1; 
     instrs[index].reg2 = reg2; 
     instrs[index].reg3 = reg3; 
    } else if(opcode == 2) { 
     instrs[index].opcode = TIMES; 
     instrs[index].reg1 = reg1; 
     instrs[index].reg2 = reg2; 
     instrs[index].reg3 = reg3; 
    } else if(opcode == 3) { 
     instrs[index].opcode = DIV; 
     instrs[index].reg1 = reg1; 
     instrs[index].reg2 = reg2; 
     instrs[index].reg3 = reg3; 
    } else if(opcode == 4) { 
     instrs[index].opcode = NEG; 
     instrs[index].reg1 = reg1; 
     instrs[index].reg2 = reg2; 
    } else if(opcode == 5) { 
     instrs[index].opcode = AND; 
     instrs[index].reg1 = reg1; 
     instrs[index].reg2 = reg2; 
     instrs[index].reg3 = reg3; 
    } else if(opcode == 6) { 
     instrs[index].opcode = OR; 
     instrs[index].reg1 = reg1; 
     instrs[index].reg2 = reg2; 
     instrs[index].reg3 = reg3; 
    } else if(opcode == 7) { 
     instrs[index].opcode = NOT; 
     instrs[index].reg1 = reg1; 
     instrs[index].reg2 = reg2; 
    } else if(opcode == 8){ 
     instrs[index].opcode = LI; 
     instrs[index].reg1 = reg1; 
     instrs[index].addr_or_imm = mem_addr; 
    } else if(opcode == 9) { 
     instrs[index].opcode = LW; 
     instrs[index].reg1 = reg1; 
     instrs[index].addr_or_imm = mem_addr; 
    } else if(opcode == 10) { 
     instrs[index].opcode = SW; 
     instrs[index].reg1 = reg1; 
     instrs[index].addr_or_imm = mem_addr; 
    } else if(opcode == 11) { 
     instrs[index].opcode = MOVE; 
     instrs[index].reg1 = reg1; 
     instrs[index].reg2 = reg2; 
    } else if(opcode == 12) { 
     instrs[index].opcode = CMP; 
     instrs[index].comparison = (Comparison)comparison; 
     instrs[index].reg1 = reg1; 
     instrs[index].reg2 = reg2; 
    } else if(opcode == 13) { 
     instrs[index].opcode = READ; 
     instrs[index].reg1 = reg1; 
    } else if(opcode == 14) { 
     instrs[index].opcode = WRITE; 
     instrs[index].reg1 = reg1; 
    } else if(opcode == 15) { 
     instrs[index].opcode = HALT; 
    } 

    index++; 
    count++; 

    } 

    *valid_instrs = count; 
    return 1; 
} 

만 두 번째 기능, 비트 조작을 수행, 문제

너무 긴 것에 대해 사과이 없습니다.

+2

로 변경하십시오. 디버거를 사용하면 확실히 문제의 원인을 찾을 수 있습니다. –

+2
+0

@BLUEPIXY가 그것을 못 박았다고 생각합니다. 'i

답변

2

위의 의견에서 말한대로 오류는 i < i + num_instrs*2 상태입니다. 'i'값은 i + num_instrs*2에 frizzed가 아니며 각 단계에서 증가합니다. i. 이자형. 조건은 0 < 0 +10*2, 2 < 2 +10*2, 4 < 4 +10*2과 같습니다. 그래서 당신은 상태에서 초기 값 starting_addr/2를 사용해야합니다 :

우리는 당신의 문제를 재현 할 수 없기 때문에 우리가 대답 할 수없는

for(i = starting_addr/2; i < i + num_instrs*2; i += 2){ 

for(i = starting_addr/2; i < starting_addr/2 + num_instrs*2; i += 2){