CSAPP - HsuJv/Note GitHub Wiki

A Tour of Computer Systems

Information Is Bits + Context

  • Files
    • Files that consist exclusively of ASCII characters are known as text files.
    • All other files are known as binary files
    • p.s. According to Wikipedia, "Text File" refers to a type of container , while plain text refers to a type of content. Text files contain plain text, but they are not limit to such.
  • All information in a system is represented as a bunch of bits.

Programs Are translated by Other Programs into Different Forms

  • On a Unix System, the translation from C source file to object file is performed by a compiler driver:

    The compilation system
    Input Processor Output
    Source program (*.c/text) Preprocessor (cpp) Modified source program (*.i/text)
    Modified source program (*.i/text) Compiler (ccl) Assembly program (*.s/text)
    Assembly program (*.s/text) Assembler (as) Relocatable object programs (*.o/binary)
    Relocatable object programs (*.o/binary) Linker (ld) Executable object program (*/binary)
    • Preprocessor phase: The preprocessor modifies the original C program according to directives that begin with "#". The result is another C program, typically with the .i suffix.
    • Compilation phase: The compiler translates the text *.i into the text file *.s, which contains an assembly-language program. Assembly language provides a common output language for different compilers for different high-level languages.
    • Assembly phase: The assembler translates *.s into machine-language instructions packages them in a form known as a relocatable object program, and stores the result in the object file *.o
    • Linking phase: The linker handles the merge between the standard C library and source file. The result is an executable object file that is ready to be loaded into memory and executed by the system.

Processors Read and Interpret Instructions Stored in Memory

  • Hardware Organization of a System
    • HardwareOrganization
    • Bus: Running throughout the system is a collection of electrical conduits called buses that carry bytes of information back and forth between the components.
    • I/O Devices: Input/output devices are the system's connection to the external world. Each I/O device is connected to the I/O bus by either a controller or an adapter
      • Controllers are chip sets in the device itself or on the system's main printed circuit board(or motherboard)
      • Adapter is a card that plugs into a slot on the motherboard.
      • Both of each is used to transfer information back and forth between the I/O bus and an I/O device.
    • Main Memory: The main memory is a temporary storage device that holds both a program and the data it manipulates while the processor is executing program.
    • Processor: The central processing unit (CPU), is the engine that interprets instructions stored in main memory.
      • At its core is a word-sized storage device called the program counter(PC)
      • At any point in time, the PC points the instruction which is to be executed next time in main memory.
    • Register file: The register file is a small storage device that consists of a collection of word-sized registers.
    • ALU: the arithmetic/logic unit (ALU) computes new data and address values.
  • Running the Program
    • Initially, the shell program is executing its instructions, waiting for us to type a command;
    • When we hit the enter key on the keyboard, the shell knows that we have finished typing the command.
    • The shell then loads the executable file, coping the code and data in the object file from disk to main memory.
    • Using a technique know as direct memory access (DMA), the data travels directly from disk to main memory, without passing through the process.
    • Once the code and data are loaded into memory, the processor begins executing the machine-language instructions in the program's main routine.

Caches Matter

  • Large storage devices are slower than smaller storage devices, fast devices are more expensive to build than their slower counterparts.
  • Similarly, while the register files access to the CPU 100 times faster than main memory, they can storage only millionth of the main memory. And this processor-memory gap increases over time.
  • To deal with this, cache memories (or simply caches) is designed, which serve as temporary staging areas for information that the processor expects to access in the near future.
  • Cache is a multi-layer structure, the closer to the processor the smaller but the faster. The idea behind caching is that a system can get effect of both a very large memory and a very fast one by exploiting locality.

Storage Devices From a Hierarchy

  • MemoryHierarchy
  • The main idea of a memory hierarchy is that at one level serves as a cache for storage at the next lower level.

The Operating System Manages the Hardware

  • Operating System
    • Operating system is a layer of software interposed between the application program and the hardware.
    • This protects the hardware from misuse by runaway applications
    • and provides applications with simple and uniform mechanisms for manipulating complicated and often wildly different low-level hardware devices.
    • The operating system achieves both goals via the fundamental abstractions:
      • files are abstractions for I/O devices
      • virtual memory is an abstraction for both the main memory and disk I/O devices
      • processes are abstractions for the processor,main memory,and I/O devices
  • Process
    • the operating system's abstraction for a running program
    • each process appears to have exclusive use of the hardware.
  • a single CPU can appear to execute multiple processes concurrently by having the processor switch among them.
    • With the mechanism know as context switching, OS performs this interleaving
    • OS keeps track of all the state, which is known as content, information that the process needs in order to run.
    • The interleaving performs as figure following: Interleaving
  • Threads
    • Modern systems a process can actually consist of multiple execution units, called threads, each running in the context of the process and sharing the same code and global data.
  • Virtual Memory
    • Virtual memory is an abstraction that provides each process with the illusion that it has exclusive use of the main memory.
    • Each process has the same uniform view of memory which is known as its virtual address space.
    • The virtual address space consists of:
      • Program code and data: Code begins at the same fixed address for all processes, followed by data locations that correspond to global variables. This area are initialized directly from the contents of an executable object file.
      • Heap: Ahead of the code and data areas. Heap expands and contracts dynamically at run time as a result of any call of memory allocating function, such as malloc and free.
      • Shared libraries: Near the middle of the address space that hold the code and data for shared libraries.
      • Stack: At the end of user's virtual address space. It is used by the compiler to make function calls. The user stack expands and contracts dynamically during the execution of the program.
      • Kernel virtual memory: It is the part of the operating system that is always resident in memory. At the end of address space is a region reserved for the kernel, which application programs have no read or write access.
  • Files
    • A file is a sequence of bytes, nothing more and nothing less.
    • Every I/O device is modeled as a file in Unix Systems.
    • This provides applications with a uniform view of all of the varied I/O devices.

Systems Communicate With Other Systems Using Networks

  • Modern system are linked to other systems by networks. The network can be viewed as just another I/O device.

Important Themes

  • Concurrency and Parallelism
    • concurrency: refer to the general concept of a system with multiple, simultaneous activities.
    • parallelism: refer to the use of concurrency to make a system run faster.
    • Parallelism can be exploited at multiple levels of abstraction in a computer system.
      • Thread-Level Concurrency: multi-core and hyperthreading.
      • Instruction-Level Parallelism: pipelining
      • Single-Instruction, Multiple-Data (SIMD) Parallelism
  • Abstractions
    • The use of abstractions is one of the most important concepts in CS.
    • We use abstractions so that we don't need to be concerned with how the entities below the abstraction layer work.
    • e.g. while a processor execute an instruction, we do focus on the result of this execution, not on the underlying hardware work.

Representing and Manipulating Information

  • Computers encode information as bits, generally organized as sequence of bytes.

  • Most machines use two's-complement encoding of integers and IEEE encoding of floating point.

  • The implicit casting of C gives results that many programmers do not anticipate, often leading program bugs.

  • Due to the finite lengths of the encodings, computer arithmetic has properties quite different from conventional integer and real arithmetic

    • The finite length can cause numbers to overflow
    • Floating-point values can also underflow, when they are so close to 0.0 that they are changed to zero.
  • Note: IEEE 754 floating-point standard (32-bits):

    Values Represented by Bit Patterns in IEEE Single Format
    Single-Format Bit Pattern Value
    0 < e < 255 (-1)s �� 2e-127 �� 1.f (normal numbers)
    e = 0; f �� 0 (at least one bit in f is nonzero) (-1)s �� 2-126 �� 0.f (subnormal numbers)
    e = 0; f = 0 (all bits in f are zero) (-1)s �� 0.0 (signed zero)
    s = 0; e = 255; f = 0
    (all bits in f are zero)
    +INF (positive infinity)
    s = 1; e = 255; f = 0
    (all bits in f are zero)
    -INF (negative infinity)
    s = u; e = 255; f �� 0 (at least one bit in f is nonzero) NaN (Not-a-Number)

Machine-Level Representation of Programs

Program Encodings

  • Machine-Level Code:
    • Two important abstractions:
      • instruction set architecture (ISA).
      • Virtual addresses.
    • IA32 machine code contains visible the processor state:
      • The program counter (called %eip) indicates the address in memory of the next instruction to be executed.
      • THe integer register file contains eight named locations storing 32-bit value.
      • The condition code registers (also flag register) hold status information about the most recently executed arithmetic or logical instruction.
      • A set of floating-point registers store floating-point data.
    • Whereas there are various data type in C, machine code views the memory as simply a large, byte-addressable array.
    • A single machine instruction performs only a very elementary operation.
  • Code Examples
    • code:
    int accum = 0;
    
    int sum(int x, int y){
        int t = x + y;
        accum += t;
        return t;
    }
    
    • after running the command gcc -O1 -S -m32 code.c, we get a file named code.s, which contains the assembly-code:
    sum:
        .cfi_startproc
        movl    8(%esp), %eax
        addl    4(%esp), %eax
        addl    %eax, accum
        ret
        .cfi_endproc
    
    • if uses the '-c' command-line option, gcc will both compile and assemble the code (code.s and code.o will be generated)
    • Embedded within the 924 bytes of the file code.o is a 15-byte sequence having hexadecimal representation:8b 44 24 08 03 44 24 04 01 05 00 00 00 00 c3
    • then calls the disassembler whose name is objdump to get the machine code generate assembly-code: objdump -d code.o:
    00000000 <sum>:
       0:   8b 44 24 08             mov    0x8(%esp),%eax
       4:   03 44 24 04             add    0x4(%esp),%eax
       8:   01 05 00 00 00 00       add    %eax,0x0
       e:   c3                      ret    
    
    • Suppose there is a file main.c which contains:
    int main(){
        return sum(1, 3);
    }
    
    • run the command gcc -o1 -m32 -c -o main.o main.c and ld -m elf_i386 -o prog code.o main.o and objdump -d prog, we get an assembly-code that begins with:
    08048094 <sum>:
     8048094:   8b 44 24 08             mov    0x8(%esp),%eax
     8048098:   03 44 24 04             add    0x4(%esp),%eax
     804809c:   01 05 24 91 04 08       add    %eax,0x8049124
     80480a2:   c3                      ret    
    
    • The code differs from the previous one in two aspects:
      • The addresses listed along the left -- the linker has shifted the location
      • The add %eax, 0x8049124 instruction -- the address of accum has been determined.

Accessing Information

  • An IA32 CPU contains a set of eight registers storing 32-bit values.
    • IA32Registers
    • For the most part, the first six registers can be considered general-purpose registers with no restrictions placed on their use.
    • The final two registers contain pointers to important places in the program stack.
  • Operand Specifiers
    • OperandForms
  • Data Movement Instructions
    • The AT&T Assembly-code Grammar is similar to the Intel Assembly-code Grammar, but the position of the former's destination operands and source operands are exactly the opposite of the latter.
    • DataMovementInstructions

Arithmetic and Logical Operations

  • IntegerArithmeticOperations
  • Load Effective Address
    • The load effective address (leal) instruction is a variant of the movl.
    • Instead of reading from the designated location, the instruction copies the effective address to the destination.
  • Shift Operations
    • The shift amount is encoded as a single byte
    • Only the low-order 5 bits of the shift amount are considered
  • Special Arithmetic Operations
    • SpecialArithmeticOperations

Control

  • Condition Codes
    • A set of single-bit condition code registers is used to be tested to perform conditional branches
    • The most useful are:
      • CF: Carry Flag
      • ZF: Zero Flag
      • SF: Sign Flag
      • OF: Overflow Flag
  • Accessing the Condition Codes
    • There are three common ways of using the condition codes:
      1. Set a single byte to 0 or 1 depending on some combination of the condition codes
      2. Conditionally jump to some other part of the program
      3. Conditionally transfer data
    • The set instructions (case a)
      Instruction Synonym Effect Set condition
      sete D setz D �� ZF Equal/zero
      setne D setnz D �� ~ZF Not equal/not zero
      sets D D �� SF Negative
      setns D D �� ~SF Nonnegative
      setg D setnle D �� ~(SF ^ OF) & ~ZF Greater(signed >)
      setge D setnl D �� ~(SF ^ OF) Greater or equal (signed >=)
      setl D setnge D �� SF ^ OF Less(signed <)
      setle D setng D �� (SF ^ OF) | ZF Less or equal(signed <=)
      seta D setnbe D �� ~CF & ~ZF Above(unsigned >)
      setae D setnb D �� ~CF Above or equal(unsigned >=)
      setb D setnae D �� CF Below(unsigned <)
      setbe D setna D �� CF | ZF Below or equal(signed <=)
⚠️ **GitHub.com Fallback** ⚠️