在这里,我得到了一个考试问题,我已经部分解决了,但不完全理解它为什么在这里使用volatile
?我缺少的表达式必须是switches >>8
。说到翻译,我有一些困难。
八个开关被映射到内存地址0 means 0020,其中最小有效位(索引0)表示开关号1,而带索引7的位表示开关号8。位值1表示开关已打开,0表示开关已关闭。写下缺少的C代码表达式,这样,如果关闭切换开关8,将会退出while循环。
volatile int * switches = (volatile int *) 0xabab0020;
volatile int * leds = (volatile int *) 0xabab0040;
while(/* MISSING C CODE EXPRESSION */){
*leds = (*switches >> 4) & 1;
}
将上面完整的C代码转换为MIPS程序集代码,包括缺少的C代码表达式。您不允许使用伪指令。
发布于 2017-06-30 17:50:57
volatile int * switches = (volatile int *) 0xabab0020;
volatile int * leds = (volatile int *) 0xabab0040;
while((*switches >> 8) & 1){
*leds = (*switches >> 4) & 1;
}
致mips
lui $s0,0xabab #load the upper half
ori $s0,0x0020
lui $s1,0xabab
ori $s1,0x0040
while:
lw $t0,0x20($s0)
srl $t0,$t0,8 #only the 8th bit is important
andi $t0,$t0,1 # clear other bit keep the LSB
beq $t0,$0, done
lw $t1,0x40($s1)
srl $t1,$t1,4
andi $t1,$t1,1
sw $t1,0x40($s1)
j while
done:
sw $t0,0x20($s0)
发布于 2017-06-30 17:56:15
如果没有易失性,编译器可以合法地将您的代码解释为:
int * switches = (volatile int *) 0xabab0020;
int * leds = (volatile int *) 0xabab0040;
*leds = (*switches >> 4) & 1;
while(/* MISSING C CODE EXPRESSION */){
}
发布于 2017-06-30 17:50:01
volatile
限定符是对C
编译器的指示,表明系统中的另一个代理可以更改地址switches
和leds
上的数据。如果没有volatile
限定符,编译器就可以优化对这些变量的引用。
问题描述指出,在设置*switches
的第7位时,循环应该运行,即:while (*switches & 0x80 != 0)
代码的翻译是留给读者的练习。
https://stackoverflow.com/questions/44851766
复制相似问题