What is the book about

The book is about what happens to the system and why when the system executes a program

With hello. C as an example

#include <sutdio.h>
int main(a)
{
    printf("hello world\n")
    return 0;
}
Copy the code

Computer Roaming – An overview of computer systems

How does a computer system work in general

How is information represented in a computer

All the information in the system – files on disk, programs in memory, user data in memory, and data on the network – is represented by a string of bits. The only way to distinguish between different data objects is the context in which we read them. For example, an identical sequence of bytes might represent an integer, floating point number, string, or machine instruction in a different context

How is the program compiled

Preprocessor, compiler, assembler, linker together constitute the compilation system

A program compiled by a high-level language into a low-level language (machine code) that can be read by a machine.

What are the benefits of knowing a build system

  • Optimize program performance. Like how expensive a function call is
  • Error in understanding link. For example, what’s the difference between a static variable and a global variable
  • Avoid security holes. The first step in safe programming is to understand the consequences of the way data and control information is stored on the program stack

How does the processor read and interpret instructions stored in memory

For example, on Unix systems, if you type./hello in the shell, how does “Hello world” appear on the screen

linux> ./hello
hello world
linux>
Copy the code

To understand how this works, you need to know the hardware of a computer

1) the bus

An electronic channel through the system that transmits blocks of fixed length. The word length in byte is a system parameter, which varies from system to system, and is now typically 4 bytes (32-bit), 8 bytes (64-bit)

2) I/O devices

I/O (input/output) devices are the channels through which a system communicates with the outside world. The keyboard, mouse, monitor, and disk all input I/O devices and are connected to the I/O bus through a controller or adapter

3) the main memory

Main memory is a temporary storage device that temporarily stores programs and data during the processing of a processor program. Physically, it is composed of a set of dynamic random access memory (DRAM) chips. Logically, it stores a linear array of bytes, each with a unique address (array index).

4) Processor

The central processing unit (CPU), or processor for short, is the engine that interprets or executes instructions stored in main memory.

Presentation and processing of information

Computers store and process information in binary numbers, also known as bits.

What is a floating point number

Floating point numbers are used to represent very large numbers, very small numbers close to zero, and approximations of real numbers

Floating point number in C

They are represented by float and double

Machine-level representation of a program

Computers execute machine code, which uses sequences of bytes to encode low-level operations, including processing data, managing memory, reading and writing data on storage devices, and network communications.

How to use assembly code

An intermediate product of the C compilation process, assembly, is easier to understand than binary instructions, which can be disassembled into assembly

How to express it in data format

Since it is extended from 16 bits (bits) to 32 bits, Intel uses “word” for 16 bits and “double word” for 32 bits. The standard int is 32 bits, as shown below

How does a computer access information

Cpus use registers to store integer data and Pointers. For example, x86-64 cpus contain a set of 16 general-purpose registers that store 64 bits. The 16 registers perform various functions, the most special of which is the stack pointer % RSP, which indicates where the stack ends at runtime. More importantly, there is a standard set of programming specifications that govern how registers are used to manage stacks, pass function parameters, return values of functions, and store local or temporary variables.

What is an operand indicator

Most instructions have one or more “operands” that indicate where the source data in an operation is being performed and where the result is being placed. There are three types of operands

  • Immediate number, used to represent constant values
  • Register, used to represent the contents of a register
  • A memory reference that accesses something at a calculated address

How to transmit data

By instruction, data transfer between register and memory can be realized

In C, “pointer” refers to an address, and an indirect reference to a pointer is to put the pointer into a register and then use that register in a memory reference

How is stack data pushed in and out

Since the stack and the data are placed in the same memory, the memory addressing method can be used to access any location in the stack. The stack grows towards the address direction, so the pressing is to reduce the stack pointer (the value of register % RSP) and store the data in memory, and the unpressing is to read the data in memory and increase the value of the stack pointer

How to perform arithmetic and logical operations

Also through instructions, most operations are divided into instruction classes. Instruction classes have various variations with operands of different sizes. For example, instruction class ADD consists of four addition instructions: ADDb, ADDW, ADDL and ADDQ, which are byte addition, word addition, double word addition and four-word addition respectively. Each instruction class corresponds to four operations, namely

  • Load valid address
  • One yuan operation
  • The binary operation
  • shift

Unary operations have one operand and binary operations have two operands

What is “loading a valid address”

Loading effective address instruction leaq is actually a variation of MOVQ instruction. Its instruction form reads data from memory to register, but this instruction does not read data from memory location, but writes the effective address to the destination operand.

What are unitary and binary operations

  1. Unary operations have only one operand, which is both source and destination. It can be a register or a memory location, such as I ++ and I —
  2. Binary operation, the second number is both source and destination, such as x-=y, the first number is the source operand, the second number is the destination operand

Shift operation

Including arithmetic and logical right shift