【CSAPP】HW1 | 位向量的应用 Application of bit vectors | Adressing and Byte Ordering

一个 w-bit 向量可表示为  \left \{ {0, . . . , w-1} \right \} 。用 8-bit 二进制代码表示每个给定的两个无符号整数,也就是一个8-bit 的向量,然后计算这两个集合的交集、并集和对称差。这个集合的元素是 \left \{ {0, . . , 7} \right \} . 每个元素从左边的位子开始按升序对应。

a)   Let DEC 28 = BIN 00011110 is A

      Let DEC 64 = BIN 01000000 is B

      Then, translated by bit vector, we can get  A = \left \{ 2,3,4 \right \}    B = \left \{ 6 \right \} ,so that

      A\cup B = \left \{ 2,3,4,6 \right \}      (intersection)

      A\cap B = \O       (union)

      A\oplus B = \left \{ 2,3,4,6 \right \}        (symmetric difference)

b)  Same Way,  Let DEC 63 = BIN 00111111 is A

      Let DEC 112 = BIN 01110000 is B

      Then,  A = \left \{ 0,1,2,3,4,5,6 \right \}    B = \left \{ 6 \right \} ,so that

      A\cup B = \left \{ 0,1,2,3,4,5,6 \right \}     (intersection)

      A\cap B = \left \{ 4,5 \right \}      (union)

      A\oplus B = \left \{ 0,1,2,3,6 \right \}     (symmetric difference)

 

假设变量 x 和 y 的类型分别为 int 和 short。x 的地址为 0x100,y 的地址为 0x200。x 的四个字节将被存储在内存位置 0x100、0x101、0x102 和 0x103处。y 的两个字节将被存储在内存位置 0x200 和 0x201 处。x 的值是 27066166,y 的值是 41797。如果我们将 x 和 y 分别存储在小端和大端中,那么在给定的内存范围内存储的是什么值?(用十六进制表示)。

x_{DEC} = 27066166,x_{HEX} = 19C FF 36

y_{DEC} = 41797,y_{HEX} = A3 45

Little endian:36 FF 9C 01  ...  45 A3

Big endian  : 01 9C FF 36  ...  A3 45

a)

1. The preprocessor modifies the original C program according to directives that begin with the # character, and deletes the comments. The result is another C program with the .i suffix.

2. The compiler translates the text file hello.i into the text file hello.s, which contains an assembly-language program. Each statement in an assembly-language program exactly describes one low-level machine-language instruction.

3. The assembler translates hello.s into machine-language instructions, packages them in a form known as a relocatable object program. The result is stored as object file hello.o.

4. A program can call a function resides in a separate precompiled object file. The linker handles this merging, and the executable object file hello is the final output.
 

b) 

High-level programming languages and compilers provide an abstraction for application developers without the need to know internally complex machine-level code

a) Registers are a type of computer memory used to quickly accept, store, and transfer data and instructions that are being used immediately by the CPU.

b) The main memory is a temporary storage device that holds both a program and the data it manipulates while the processor is executing the program.

c) The L1 and L2 caches serve as temporary staging areas for information that the processor is likely to need in the near future. So, the processor can read data faster.

将下列的十进制数分别转换成 8-bit 无符号二进制数和对应的十六进制数,如无法转换,记为X。

a) 0        b) 255        c) 256        d) 100

a)  00000000, 00

b)  255 = 2^7+2^6+...+2^0 , 11111111 , FF

c)  256 = 2^8 ,  X

d)  100 = 2^6 + 2^5+2^2 ,  01100100 , 64

一个给定的32位二进制代码代表字符串或整数(使用二进制补码)。如果每个字节的十六进制表示法都在下面的转换表中,给定的代码是一个字符串;否则就是一个整数。写下每个二进制代码的字符串或整数。(从左到右) 例子。

a) 45 53 4E 47, ESNG

b) 74 21 33 2e, t!3.

c) FF FF FF D4, -44

d) 00 01 57 A6, 87974

a)   211 + 240 = 451,384 + 247 = 631

451<2^x-1<631x=9

b)for unsigned integer representation,0\leq 3x\leq 2^4-10\leq x\leq 5

two's complement encoding integer representation,-2^3 \leq 3x\leq 2^3-1-2\leq x\leq 2


猜你喜欢

转载自blog.csdn.net/weixin_50502862/article/details/124082971