What to learn in week five

Source: Internet
Author: User

In- depth understanding of computer System Chapter II study Summary

Our exploration of computer systems begins with learning the computer itself, and it is made up of processors and
Memory subsystem. In the core, we need methods to represent the basic data types,
Approximate values such as Integer and real number operations. We then consider how the machine-level instructions operate such data, and how the compiler translates the C program into such instructions. Next, we study several practical
The current processor approach helps us to better understand how hardware resources are being used to execute instructions. Once we understand the compiler and machine-level code, we can write the highest-performing C program,
To analyze how to maximize the performance of your program.

The history of the binary

Information stored and processed by modern computers is represented by a two-value signal. These insignificant binary numbers, or bits, lay the foundation of the digital revolution. The more than 1000-year-old decimal (based on 10), which was known and used in India, was developed by Arab mathematicians in 12th century and was brought to the West in 13th century by Italian mathematician Leonardo Pisano (A.D. 11701250, the more commonly known name is Fibonacci). Using decimal notation is a natural thing for humans with 10 fingers, but binary values work better when constructing machines that store and process information. A binary signal can be easily represented, stored, and transmitted, for example, by a hole or hole in a punched card, a voltage or a low voltage on a wire, or a magnetic field clockwise or counterclockwise. The electronic circuitry that stores and performs calculations on a two-value signal is simple and reliable, allowing manufacturers to integrate millions of or even billions of of these circuits on a single silicon wafer.

Three types of digital representations

unsigned (unsigned) encoding is based on the traditional binary notation, which represents a number greater than or equal to zero. The complement (s-complement) encoding is the most common way to represent signed integers, which are numbers that can be positive or negative. The floating-point (? oating-point) encoding is a two-base version of the scientific notation for real numbers.

Examples of overflow (OVER?OW):

Most computers now (using 32 bits to represent data type int), the calculation expression 200*300*400*500 will result in-884 901 888. This violates the attributes of integer operations, and calculating the product of a set of positive numbers should not produce a negative result.

The computer operation of integers satisfies the law of real integer arithmetic known to people.

Using the combination law of multiplication and the commutative law to calculate any of the following C-expressions, the results will be 884 901 888: (500*400) * (300*200) ((500*400) *300) *200 ((200*500) *300) *400 400* (200* ( 300*500))
The computer may not produce the desired results, but at least the results are consistent!

the evolution of C programming language

As mentioned earlier, the C programming language was first developed by Dennis Ritchie of Bell Labs, and is intended to be used with UNIX operating systems (Unix is also developed by Bell Labs). At that time, most system programs, such as operating systems, must be written in a large number of assembly codes in order to access low-level representations of different data types. For example, functions such as memory allocations provided by the MALLOC library function cannot be written in other high-level languages at that time.

Information Store

Most computers use a 8-bit block, or byte, as the smallest addressable memory unit, rather than accessing individual bits in memory. Machine-level programs treat memory as a very large array of bytes, called virtual memory. Each byte of the memory is identified by a unique number, called its address, and the collection of all possible addresses is called the virtual address space. As the name implies, this virtual address space is just a conceptual image of a machine-level program. The actual implementation (see Chapter 9th) combines random access memory (RAM), disk storage, special hardware, and operating system software to provide a seemingly uniform byte array for the program.

Hexadecimal notation

A byte is made up of 8 bits. In binary notation, its range is 000000002 to 111111112, and if it is represented by a decimal integer, its range is 010 ~ 25510. Both representations are not very convenient for describing bit patterns. Binary notation is too verbose, and the conversion of decimal notation to bit mode is cumbersome. The alternative is to represent bit patterns in 16 or hexadecimal (hexadecimal) numbers. Hex ("hex") uses the number ' 0 ' ~ ' 9 ', and the character ' A ' ~ ' F ' to represent 16 possible values. Figure 2-2 shows the decimal values and binary values for the 16 hexadecimal digits. In hexadecimal notation, the value of a byte is 0016 ~ FF16.

For example, suppose you give you a number 0x173a4c, you can convert it to binary format by expanding each hexadecimal number, as follows: Hex 1 7 3 A 4 C binary 0001 0111 0011 1010 0100 1100 So you get the binary The system represents 000101110011101001001100. Conversely, given a binary number 1111001010110110110011, you can first divide it into a group of 4 bits and then convert it to 16 binary.  Note, however, that if the total number of bits is not a multiple of 4, the leftmost group can be less than 4 bits, preceded by 0, and then each 4-bit group is converted to the corresponding hexadecimal number: binary 11 1100 1010 1101 1011 116 binary 3 C A D B 3

Conversions between decimal and hexadecimal representations

Convert a decimal number x to 16, you can repeatedly use 16 in addition to X, get a quotient Q and a remainder R, that is, x = qx16 + R. We then use R as the lowest number in hexadecimal notation, and we get the remaining number by repeating the process on Q. For example, consider a conversion of decimal 314156:314156 = 19634x16 + (C) 19634 = 1227x16 + 2 (2) 1227 = 76x16 + one (B) = 4x16 + (C) 4 = 0x16 + 4 (4) From here, we can read the hexadecimal representation as 0X4CB2C. In turn, convert a hexadecimal number to a decimal number, and we can multiply each hexadecimal number by the power of the corresponding 16. For example, given a number 0X7AF, we calculate that it corresponds to a decimal value of 7x162 + 10x16 + 15=7x256 + 10x16+15=1792+160+15=1967.

Word

Each computer has a word size, which indicates the nominal size of the integer and pointer data (nominal size). Because the virtual address is encoded in one of these words, the most important system parameter that the word length determines is the maximum size of the virtual address space. That is, for a machine with a word length of W, the virtual address ranges from 0 to 2w-1, and the program accesses up to 2w bytes.

Data size

Computers and compilers support many different ways of encoding numeric formats, such as integers and floating-point numbers, and other lengths of digits. For example, many machines have instructions for handling a single byte, as well as instructions for processing a 2-byte, 4-byte, or 8-byte integer, and some instructions support a floating-point number expressed as 4 bytes and 8 bytes. The C language supports multiple data formats for integers and floating-point numbers. The data type of C char represents a separate byte. Although "Char" is named for the fact that it is used to store a single character in a text string, it can also be used to store integer values. C's data type int can also be preceded by a qualifier short, a long, and the nearest long long to provide an integer representation of various sizes

Addressing and byte order

for program objects that span multiple bytes, we must establish two rules: what is the address of this object, and how these bytes are arranged in memory. On almost all machines, multibyte objects are stored as contiguous sequence of bytes, with the address of the object being the smallest address in the byte used. For example, suppose that the address of a variable x of type int is 0x100, that is, the value of the address expression &x is 0x100. Then, the 4 bytes of x will be stored in the 0x100, 0x101, 0x102, and 0x103 locations of the memory. Some machines choose to store objects in memory in the order in which they are from the least significant byte to the most significant byte, while others are stored in the order from the most significant byte to the lowest valid byte. The previous rule-the least significant byte in the front of the way, called the Small-end method (little endian). This rule is used by most Intel compatible machines. The latter rule-the most significant byte in the front way, called the big endian. most IBM and Sun Microsystems machines use this rule. Note that we are talking about "most". These rules are not strictly defined by the boundaries of the enterprise. For example, IBM and Sun-made personal computers use an Intel-compatible processor, so the small-end method is used. Many newer microprocessors use the double-ended method (Bi-endian), which means that they can be configured to run as big or small-endian machines.

The third case where byte order becomes visible is when writing programs that circumvent the normal type system. In the C language, you can use coercion type conversion (CAST) to allow referencing an object in a data type that differs from the data type defined when the object was created. Most application programming strongly does not recommend this coding technique, but they are very useful and even necessary for system-level programming.

For C language Beginners: Use typedef to name data types:

typedef declarations in the C language provide a way to name the data type. This can greatly improve the readability of your code because deep nested type declarations are difficult to read.

For C language Beginners: Use printf to format the output:

The printf function (and its similar fprintf and sprintf) provides a way to print information that has considerable control over the formatting details. The first argument is the format string, and the remaining parameters are the values to be printed. In the format string, each sequence of characters starting with '% ' indicates how the next parameter is formatted. Typical examples are: '%d ' is the output of a decimal integer, '%f ' is the output of a floating-point number, and '%c ' is the output of a character whose encoding is given by the parameter.

For C language beginners: pointers and arrays

In the function show_bytes (Figure 2-4), we see a close connection between the pointer and the array, which is described in detail in section 3.8. This function has a parameter of type byte_pointer (defined as a pointer to unsigned char) start, but we see the array reference Start[i] on line 8th. In the C language, we can use array notation to refer to pointers, and we can also use pointer notation to refer to array elements. In this example, reference start[i] means that we want to read the byte at the first position where start points to.

For C language beginners: pointer creation and indirect referencing

We see the use of two unique operations in C and C + +. C's "Fetch address" operator & Create a pointer. In these three rows, the expression &x creates a pointer to the position where the variable x is saved. The type of the pointer depends on the type of x, so the three pointers are of type int*,? oat*, and void**, respectively. (Data type void* is a special type of pointer that has no associated type information.) Coercion type conversion operators can convert one data type to another. Therefore, coercion of type conversion (Byte_ pointer) &x indicates that, regardless of the previous type of pointer &x, it is now a pointer to a data type of unsigned char. These forced type conversions given here do not change the actual pointers, they simply tell the compiler to look at the data being directed at the new data type.

Linux: Intel IA32 processor running Linux

windows: Intel IA32 running Windows

Sun: Sun Microsystems SPARC processor running Solaris

Linux: Intel x86-64 processor running Linux

Introduction to Boolean algebra

Binary value is the core of computer coding, storing and manipulating information, so the study of numerical 0 and 1 has evolved a rich mathematical knowledge system. This originates from the work of George Boole (George boole,1815-1864) around 1850, and is therefore also known as Boolean algebra (bool algebra). Boolean notes that by encoding the logical value TRUE (TRUE) and False (false) to a binary value of 1 and 0, it is possible to design an algebra to study the basic principles of logical reasoning. The simplest Boolean algebra is defined on the basis of the two-tuple set {0,1}. Figure 2-7 defines several operations in this Boolean algebra. The symbols we use to denote these operations match those used by the bit-level operations in C, which are discussed later. Boolean operations ~ correspond to logical operations not, denoted by symbols in propositional logic. In other words, when P is not true, we say that "p is true and vice versa. Correspondingly, when P equals 0 o'clock, ~p equals 1, and vice versa. Boolean operations & correspond to logical operations and, represented in propositional logic with symbolic ∧. When P and q are true, we say that P∧q is true. Accordingly, P & Q is equal to 1 only if P = 1 and q = 1 o'clock. Boolean operation | Corresponds to a logical operation or, expressed as a symbolic ∨ in propositional logic. When P or Q is true, we say P∨Q is established. Correspondingly, when P =1 or q = 1 o'clock, P|q is equal to 1. The Boolean operation ^ corresponds to the logical operation XOR, which is represented in the propositional logic with the symbol σ. When P or Q is true but not at the same time, we say PΣQ is established. Correspondingly, when p = 1 and q = 0, or p = 0 and q = 1 o'clock, P^q equals 1.

Bit-level arithmetic in C language

A useful feature of the C language is that it supports bitwise Boolean operations. In fact, the symbols that we use in Boolean operations are used by the C language: | is the or (or),& is and (and), ~ is not (negate), and ^ is exclusive-or (XOR). These operations can be applied to any "integer" data type, that is, data types declared as char or int, regardless of whether they have qualifiers such as short, long, long, or unsigned.

Integer representation

In this section, we describe two different ways to encode integers with bits: one can only represent non-negative numbers, and the other can represent negative numbers, 0, and positive numbers. Later we will see their mathematical properties closely associated with machine-level implementations. We'll also look at extending or shrinking an encoded integer to fit the effect of a different length representation.

What to learn in week five

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.