The first experiment of “deeply understanding computer system” — data Lab

Time：2021-1-20

This paper is the second chapter of the CSAPP supporting experiments, through the use of limited operators to achieve positive, negative, floating-point bit level representation. Through the completion of these 13 functions, we can better understand the computer data encoding.

preparation

go to the official website firstLab AssignmentsGet the experiment related files (also can add my QQ to get the teaching video, PPT and other content) in each experimental file readme describes in detail how to modify the program, compiler and so on. It is recommended to read carefully. If you don’t understand, you can leave a message and reply in time.

My compiling environment: Ubuntu 16.04, GCC 5.4.0.

The following error will be reported at compile time.

Execute the following command to install the 64 bit package.

sudo apt-get purge libc6-dev
sudo apt-get install libc6-dev
sudo apt-get install libc6-dev-i386

Compile again, no error, normal.

subject

bitXor

thinking

De Morgan law, also known as inversion.

code

/*
* bitXor - x^y using only ~ and &
*   Example: bitXor(4, 5) = 1
*   Legal ops: ~ &
*   Max ops: 14
*   Rating: 1
*/
int bitXor(int x, int y) {
return ~(x & y) & ~(~x & ~y);
}

tmin

thinking

The minimum value of complement is 0x80000000

code

/*
* tmin - return minimum two's complement integer
*   Legal ops: ! ~ & ^ | + << >>
*   Max ops: 4
*   Rating: 1
*/
int tmin(void) {
return 1<<31;
}

isTmax

thinking

Judge whether it is the maximum value of complement. The maximum value of 32-bit complement is 0x7fffff, which is XOR,

code

/*
* isTmax - returns 1 if x is the maximum, two's complement number,
*     and 0 otherwise
*   Legal ops: ! ~ & ^ | +
*   Max ops: 10
*   Rating: 2
*/
int isTmax(int x) {
return !(x^0x7fffffff);
}

allOddBits

thinking

This problem is relatively simple, using the mask method to solve. First of all, we need to construct a mask. We use the shift operator to construct a mask with all 1 odd digits. Then we get the odd digits of the input x value, and clear the other bits (Mask & x). Then we do XOR operation with the mask. If it is the same, the final result is 0, and then we return the logical non of its value.

code

/*Method 1
/*Method 2

negate

thinking

The complement is actually an Abelian group. For X, – x is its complement, so – x can be obtained by adding 1 to X

code

/*
* negate - return -x
*   Example: negate(1) = -1.
*   Legal ops: ! ~ & ^ | + << >>
*   Max ops: 5
*   Rating: 2
*/
int negate(int x) {
return ~x+1;
}

isAsciiDigit

thinking

X is different from ‘0’ and ‘9’ respectively, and then judge whether the sign bit is 0 or 1 according to the result of the difference

code

/*

* isAsciiDigit -return 1 if 0x30 <= x <= 0x39 (ASCII codes for characters '0' to '9')

*   Example: isAsciiDigit(0x35) = 1.

*            isAsciiDigit(0x3a) = 0.

*            isAsciiDigit(0x05) = 0.

*   Legal ops: ! ~ & ^ | + << >>

*   Max ops: 15

*   Rating: 3

*/

int isAsciiDigit(int x) {

return(!((x+~48+1)>>31))&!!((x+~58+1)>>31);

}

conditional

thinking

Convert x to all zeros or all ones. Note here that the complement of 0 is 0, and the bits represent all zeros. The complement of 1 is – 1, and bits denote all 1. When x turns to all 0 and all 1, then (X & Y) or (~ x & z), there must be one. What is returned is the value of Y or Z

code

/*

isLessOrEqual

thinking

There are no more than two cases to compare the size of two numbers by bit operation: one is that the sign is different, the positive number is larger, and the other is that the sign is the same, so we can see the difference sign.

code

/*

logicalNeg

thinking

logical not means that not 0 is 1 and not 0 is 0. By using the property of its complement (negative plus one), except 0 and the smallest decimal（The sign bit is 1 and the rest is 0）The other numbers are opposite to each other (sign bit takes place or is 1). The complement of 0 and the least decimal is itself, but the sign bit of 0 and its complement sign bit are either 0 and the least decimal is 1. Use this to get a solution.

code

/*
* logicalNeg - implement the ! operator, using all of
*              the legal operators except !
*   Examples: logicalNeg(3) = 0, logicalNeg(0) = 1
*   Legal ops: ~ & ^ | + << >>
*   Max ops: 12
*   Rating: 4
*/

int logicalNeg(int x) {
return ((x|(~x+1))>>31)+1;
}

howManyBits

thinking

The complement of a positive number: the highest 1 of a positive number is the nth number, plus the sign bit, the result is n + 1.

Complement of negative number: convert to positive number, same as above.

/* howManyBits - return the minimum number of bits required to represent x in

floatScale2

thinking

refer to the figure above for understanding. Do not understand the IEEE standard floating point format backReading notes of deep understanding computer system (CSAPP) — Chapter 2 information representation and processing

According to the input value, it can be divided into three cases

1. Input UF as infinity and nan to return UF directly

2. If UF is 0 or infinitesimal, return 2 * UF + sign

3. If exp + 1 = = 255, return infinity, otherwise return exp + 1. (Exp is the integer part of floating point code, exp + 1 is equivalent to UF * 2.)

code

/*

floatFloat2Int

thinking

1. Denormalization, which means a number very close to 0. It is converted to 0 after int value

2. Normalization, the distribution of numbers becomes more and more sparse from near 0 to infinity. When f does not exceed the range of int type representation, it is converted to int; when f exceeds the range of int type representation, it returns 0x80000000u

3. Special, return 0x8000000u

When the normalized float is converted to an int integer,

If e > = 31, the decimal point is shifted to the right by 31 bits, then the implied 1 and frac occupy 32 bits, and a sign bit is needed, which is beyond the range of int type

If e < 0, it is 0.1frac after the decimal point is shifted 1 bit to the left and 0 after it is converted to int

If 0 < e < 23, if the decimal point is shifted to the left after e, part of the frac should be discarded. At this time, the frac should be shifted to the right by 23-e to erase the decimal part

If 23 < = e < 31, after the decimal point is moved to the right, all frac will be moved to the left of the decimal point, and the frac will be moved to the left by E-23 digits, and the zeros will be filled after it

code

/*

floatPower2

thinking

According to the floating-point number evaluation formula:$$V = {( – 1)^s} \times M \times {2^E}$$

Let m = 1 (frac = 0), xexp bias, exp = x + bias

2. Non standardization

Exp = 0. In frac, let a bit be 1, so that x is smaller.

exp frac M maxE MinE
Denormalization 0 0 * 10 * 0.frac -127 -148
Standardization Not 0 0 1.0 127 -126

Analysis of boundary conditions

1. Non standardization

• When frac = 1 million, M = 0.1b = 0.5, e = 1-bias = – 126, v = 0.5 * 2.0 ^ – 126 = 2.0 ^ – 127
• When frac = 000 0000 0001, M = 0.000 0000 0001 = 2.0 ^ – 22, e = – 126, then v = 2.0 ^ – 22 * 2 ^ – 126 = 2.0 ^ – 148

2. Standardization

• When exp = 0xff, e = exp – bias = 127
• When exp = 1, e = exp – bias = – 126

code

unsigned floatPower2(int x) {

summary

The following topics are still very brain burning. I am at a loss when I get the topic. The main reason is that I don’t understand the concept well. Later, I went to read a book, understood the basic concepts, looked at other people’s solutions, and the topic could be straightened out gradually. Problem solving process code is also recorded, after a period of time back two brush may have a new solution. There are still several experiments waiting for me. Take your time. Welcome to follow my blog and get update notice in time.

finally share a joke on PPT, count sheep ~ ha ha~

Form a habit, praise first, then watch! If you think the writing is good, you are welcome to pay attention, like, collect, thank you!

**If you encounter the problem of typography disorder, you can visit my CSDN through the following link.

Welcome to my official account: embedded and Linux, collect the autumn test written interview interview (HUAWEI millet and other big factory noodles, embedded knowledge points, written questions, resume template, etc.) and 2000G learning materials. **