# 台大資工104考題 ###### tags: `雜記` > 僅供參考,有沒有錯都不知道== ![](https://i.imgur.com/mokVHDh.png) ```! On a MIPS machine (Fig. 1) running UNIX, we observed the following binary string stored in memory location x. 0011 1111 0111 0000 0000 0000 0000 0000 The binary could mean many different things, ``` #### a. If this is an integer number, what value is it? $2^{29}+2^{28}+2^{27}+2^{26}+2^{25}+2^{24}+2^{22}+2^{21}+2^{20}$ #### b. If this is a single precision floating point number, what value is it? ``` 0 | 01111110 | 11100000000000000000000 sign | exponent | mantissa ``` > $2^{-1}*1.111=0.1111$ #### c. If whis is an instruction, what instruction is it? ``` 001111|11011|10000|0000000000000000 OPCODE|RS |RT |IMM LUI $16,0 /* $16=0<<16 */ ``` #### d. If this is a C string, what string is it? #### e. If this binary string was observed on your x86 desktop, what would be your answer for(d)? binary string = 0011 1111|0111 0000|0000 0000|0000 0000 = 3f 70 00 00 > big-endian: MIPS > little-endian: x86, most ARM implementations 在 MIPS 上 0x3f700000 的記憶體內容: ``` Low High 3f 70 00 00 ``` 所看到的string是 "?p" 在 x86 上的記憶體內容: ``` Low High 00 00 f7 3f ``` 所看到的string是 "" (null string) 使用以下的code做測試 ```cpp= #include <stdio.h> int main(){ int i, p = 0x3f700000; unsigned char *q = (unsigned char*) &p; for (i=0; i<4;++i){ printf("%02x ", q[i]); } printf("\n"); printf("string=%s\n", (const char *)&p ); } ``` 儲存成a.c,先在host(x86)上編譯進行測試: ```bash ~$ gcc a.c ~$ ./a.out ``` 結果為 ``` 00 00 70 3f string= ``` 接下來在MIPS平台上編譯測試: ```bash ~$ sudo apt install -y binfmt-support qemu-user-static mips-linux-gnu-gcc ~$ mips-linux-gnu-gcc a.c -static ~$ ./a.out ``` 結果為 ``` 3f 70 00 00 string=?p ``` #### f. Suppose we have three variables a. b and c. Give a case where (a+b)+c computes a different value than a+(b+c) on a MIPS microprocessor. 結合律: `(a + b) + c = a + (b + c)` ``` float a=2e38, b=2e38, c=-2e38; (a+b)+c=Inf+c=Inf a+(b+c)=a+0=a ``` ## reference - [Plasma - most MIPS I(TM) opcodes](https://opencores.org/projects/plasma/opcodes) - [Endianness](https://en.wikipedia.org/wiki/Endianness#Little-endian) - [davinais筆記](https://hackmd.io/@Davinais/rJm3Hm4tS) - [你所不知道的C語言:數值系統篇](https://hackmd.io/@sysprog/c-numerics?type=view)