C到MIPS翻译

时间:2017-06-30 17:12:08

标签: mips

在这里,我得到了一个我部分解决但完全不理解的考试题目 为什么在这里使用volatile?和缺少的表达  我必须是switches >>8。 谈到翻译时我有一些困难。

八个开关的内存映射到内存地址0xabab0020,其中 最低有效位(索引0)表示开关编号1和索引为7的位 表示开关编号8.位值1表示开关打开和 0表示它已关闭。写下缺少的C代码表达式,这样就可以了 如果切换开关编号8关闭,则循环退出。

 volatile int * switches = (volatile int *) 0xabab0020;
 volatile int * leds = (volatile int *) 0xabab0040;
 while(/* MISSING C CODE EXPRESSION */){
   *leds = (*switches >> 4) & 1;
}

将上面的完整C代码翻译成MIPS汇编代码,包括缺少的C代码表达式。您不能使用伪指令。

3 个答案:

答案 0 :(得分:2)

没有volatile你的代码可以合法地被编译器解释为:

int * switches = (volatile int *) 0xabab0020;
int * leds = (volatile int *) 0xabab0040;
*leds = (*switches >> 4) & 1;
while(/* MISSING C CODE EXPRESSION */){
}

答案 1 :(得分:1)

switches限定符表示leds编译器可以通过系统中的其他代理更改地址volatile*switches中的数据。如果没有while (*switches & 0x80 != 0)限定符,编译器将被允许优化对这些变量的引用。

问题描述表明循环应在{{1}}的第7位设置时运行,即:{{1}}

翻译代码留给读者练习。

答案 2 :(得分:0)

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)  
相关问题