Language & Control Structures

Chapter 1. Language

Language, regardless of its type, is essentially a set of symbols. English, Chinese, and programming languages alike adhere to this principle. The defining difference, however, lies in whether a specific platform exists to interpret or execute these symbols.

For example, natural languages are brain-dependent, while programming languages are platform-dependent (relying on the specific software, OS, and hardware).

Hardware-Level Dependency (Assembly)

Code is written specially for a CPU's Instruction Set Architecture (ISA).

  • x86/x64: Standard for most desktops and servers (Intel/AMD).
  • ARM: Standard for most mobile devices, Apple Silicon (M*), and modern high-efficiency servers.
  • RISC-V: An open-source architecture common in embedded systems.

The Incompatibility: Assembly code is non-portable. Programs written for x86 will not execute on ARM because their underlying "vocabularies" (instructions) are fundamentally different.

System-Level Dependency (Compiled)

Languages like C, C++, Rust, and Go are compiled into native machine code (binary). They exhibit a dual dependency on both hardware and the operating system.

  • CPU Dependency: The compiler must target a specific CPU architecture.
  • OS Dependency: The languages rely on System Calls to interact with hardware.

Virtual Machine Dependency (Managed)

To achieve cross-platform portability, languages like Java, C#, and Python introduce an abstraction layer between the code and the hardware.

Language Intermediate Layer Strategy
Java JVM Compiled into Bytecode. JVM translates to native.
C# CLR Compiled into IL. CLR handles execution.
Python Interpreter Interpreted line-by-line. Hardware agnostic.

Chapter 2. Layer

In CS, a layer is a mind pattern representing the capability of a thing. It allows us to focus on "what it can do" rather than "how it is implemented".

In the physical world, only the bottom layer is real. The middle layer acts as the method of management. The upper layer emerges as the embodiment of meaning perceived by humans.

OSI Model

Upper Layers:

  • Application: Interface & User Intent (HTTP, DNS).
  • Presentation: Format translation & Encryption (JSON, SSL).
  • Session: Conversation management.

Middle Layers:

  • Transport: Reliability & Segments (TCP, UDP).
  • Network: Routing & Packets (IP).

Bottom Layers:

  • Data Link: Physical Addressing & Frames (MAC, Ethernet).
  • Physical: Raw bit streams (Cables, Signals).

TCP/IP & Hardware Mapping

Application Layer: User Space (RAM). Sending requests via System Calls.

Transport/Internet Layer: Kernel Space (RAM). The TCP/IP stack is code in the OS.

Network Access Layer: The Boundary.

  • Data Link: NIC Controller & Driver. (Software meets Hardware).
  • Physical: PHY Chip & Cables. (Pure Electronics).

The "Relay Race": From Java "Hello" (User RAM) -> OS (Kernel RAM) -> PCIe Bus -> NIC Chip -> PHY Chip -> Wire.

Chapter 3. Process

If we strip away abstractions, a "living" process is just a specific transient electrical state maintained by hardware.

The Body: Electric Charges in RAM

Billions of microscopic capacitors in DRAM.

  • Text Segment: Bytecode instructions.
  • Data Segment: Global/Static variables.
  • Heap: Objects (new Cookie()).
  • Stack: Method call history.

The Soul: Voltage in CPU Registers

The snapshot of the process's state "right now" (Context).

  • PC: Address of next instruction.
  • SP: Stack top pointer.

"The rapid flipping of voltage is the vital activity of the process."

The ID Card: PCB in Kernel RAM

The Process Control Block (PCB). A ledger in OS Kernel Space recording PID, Owner, Status.

Context Switch: Save "The Soul" (Registers) to "The ID Card" (PCB), switch PCB, restore "The Soul" from the new PCB, and Run.