Information is Bits
A computer system is a combination of hardware and system software. It runs application programs using these.
#include <stdio.h>
int main()
{
printf("Hello, world");
return 0;
}
This code returns Hello, world
as its output. The file in which it is written is called a text file, containing source code.
A file is a sequence of 0s and 1s - binary digits, or bits - organised in groups of 8 bits called bytes.
All information in a system is represented as a bunch of bits. The only thing differentiating them is the context in which we view them.
Programs Are Translated by Other Programs into Different Forms
A program is a set of instructions asking the computer to perform an action. It is written in a particular programming language by a programmer using a text editor like VSCode, Atom, etc.
A programming language is classified into 2 categories - low-level and high-level.
High-level languages are simple and abstracted from the complexities of the machine.
public class fff
{
public static void main(String[] args)
{
System.out.println("Hello, world");
}
}
Low-level languages deal directly with the machine code.
section .data
hello: db 'Hello, World',10
helloLen: equ $-hello
section .text
global _start
_start:
mov eax,4
mov ebx,1
mov ecx,hello
mov edx,helloLen
int 80h
mov eax,1
mov ebx,0
int 80h;
Abstraction is the process of hiding away the difficult-looking parts of a system from the end-user. Only the features that are needed [at that moment] are shown.
A program is executed by converting the source code into an executable object file [or simply executable] and then executing it. This is done by a compiler in 4 phases :
- Preprocessing : The original program is modified according to the directives in the beginning.
- Compilation : The file is translated into assembly-level instructions.
- Assembly : Assembler translates the assembly file into binary, packs it into a relocatable object program and stores it in an object file.
- Linking : Linker combines the required libraries and other files and forms an executable, which is loaded into main memory and executed by the system.
It Pays to Understand How Compilation Systems Work
It is important to understand how compilation works :
- We can make better decisions if we learn how the compiler handles statements and how it is done in binary. This would enable us to write efficient code [why use 4-5
if-else
ladders whenswitch
exists?] - We can understand linker errors and ensure that they’re prevented.
- We can avoid security issues like buffer overflow vulnerabilities.
Buffer
A buffer is a temporary storage place for data that’s being transported from one place to another.
So, if a video is being buffered, it means the service is downloading the later parts of the video into a buffer, ensuring that your computer doesn’t have to download it instead and cause freezing.
A buffer overflows if the amount of data in it exceeds its storage capacity.
An attacker may feed input to the buffer to overwrite the memory of an application to change its execution path, and either damage files or expose private information, using an overflowed buffer.
Processors Read and Interpret Instructions Stored in Memory
The shell is a command-line interpreter. You type commands in the shell for it perform an action.
If the first word of your command is not a recognised shell command, it assumes it’s an executable file and runs it.
Hardware Organization of a System
Buses
Bus is an electric cable transferring information between components. They are designed to carry fixed-size chunks of bytes called words.
The size of a word varies with system [4 bytes in 32-bit systems; 8 bytes in 64-bit systems].
Input / Output devices
These produce output and receive user input.
I/O devices are connected to the I/O bus via a controller or adapter.
Controllers are chips embedded on the motherboard, while adapters are cards plugged into a slot on the motherboard.
Main memory / Random Access Memory
Main memory [RAM] is a temporary storage device holding a program and the data it manipulates while a processor executes the program.
It is organised as an array of bytes, each element having its own unique address [array index] starting from 0.
It comes in 2 varieties :
- Static RAM : RAM that retains data as long as power is on.
- Dynamic RAM : RAM that needs to be refreshed every few milliseconds.
Processor / CPU
Processor is the engine executing instructions stored in RAM, which is pointed at by a storage device called program counter [PC].
Arithmetic and Logic Unit [ALU]
The ALU computes new data and address values. It also performs arithmetic and logical operations.
Caches Matter
A major goal of designing a system is to make its operations run as fast as possible.
Physical laws state that larger storage devices are cheaper and slower than their smaller, faster and expensive counterparts.