5
int load_byte(int memory[],int address) {
//This function works by finding the appropriate word then masking
//to obtain the relevant byte
int index,section;

    switch (address>>28) {
        case 0:
            index = address - START_ADDRESS;
            printf("index = %d",index);
        case 1:
            index = address - START_DATA_ADDRESS;
        case 7:
            index = address - START_STACK_ADDRESS;
   }

    section = index%4;
    index = index/4;
    switch (section) {
    case 0:
        return memory[index]&0x000000ff;
    case 1:
        return (memory[index]&0x0000ff00)>>8;
    case 2:
        return (memory[index]&0x00ff0000)>>16;
    case 3:
        return (memory[index]&0xff000000)>>24;
    }

}

START_ADDRESS 的值为 0x00400000,我使用的示例地址是 0x00400002,只是这个函数一直给我一个段错误,不太明白为什么,因为有问题的数组的大小为 1000。提前致谢。

4

2 回答 2

8

您的第一个开关看起来很奇怪,只处理了 0、1 和 7。并且每个案例的结尾都没有break;声明。

于 2012-10-26T09:16:00.787 回答
1

编码:

switch (address>>28) {
    case 0:
        index = address - START_ADDRESS;
        printf("index = %d",index);
    case 1:
        index = address - START_DATA_ADDRESS;
    case 7:
        index = address - START_STACK_ADDRESS;
    }

由于addressis 0x00400002,所以switch将从 开始执行,并且每个中都case 0没有任何代码,所有代码都将运行。也就是说,最终意志等于。breakcase Xindexaddress - START_STACK_ADDRESS

也许这就是原因。

尝试在breaks 之间添加cases。

switch (address>>28) {
    case 0:
        index = address - START_ADDRESS;
        printf("index = %d",index);
        break;
    case 1:
        index = address - START_DATA_ADDRESS;
        break;
    case 7:
        index = address - START_STACK_ADDRESS;
}
于 2012-10-26T09:19:01.097 回答