Learn OS with examples — Part I

Bijie Zhu
3 min readAug 23, 2020

--

1.the basic component of a computer

1)CPU: the central part which is doing the calculation

2)Bus: the circuit to transmit data between components.
a. Address Bus: identify a particular location inside the main memory b.Data Bus: transport the actual data

3) Memory: store the data
4)I/O devices: hardware device accepts inputted or outputted data, eg. Keyboard.

2. How CPU interacts with the memory:
CPU internally:

There are three parts inside CPU
1) Arithmetic / Logic Unit : responsible for calculations
2) Registers & CPU cache: store the data
3) Control Unit:
a. Get instructions from the main memory.
b. decide which instruction to run on CPU
c. decide use which registers to store results
There are two registers that decide which instruction to run
I. Program Counter/Instruction Pointer: holds a pointer(memory address) of the next instruction to be executed
II. Instruction Register (IR): Instruction Currently being executed or decoded

This is particularly useful when your C++ program gets crushed. you can check the Program counter before the program crashed.
print the instruction to be executed next with gdb:
x/i $pc

3. How to process instruction stored in the Control Unit
There are two registers in the Control Unit for process switch.
These two registers store:
I. start address of current process code segment
II. start address of current process data segment

4.More about registers

1) general-purpose registers (for data storage)
For the 8086 processor, there are 16 general-purpose registers for data storage:
AX: divided into two 8-bit registers AH & AL (H is high and L is low).
AX is the accumulator, generally used for arithmetic and logical instructions
eg. ADD AX, AX (AX = AX + AX)
BX: BH & BL; Base register, used to store the value of the offset
CX: CH & CL; counter register, used in looping and rotation.
eg. MOV CX, 0005
LOOP
DX : DH & DL; data register. used in multiplication and input/output port addressing
eg. MUL BX (DX, AX = AX * BX)
SP: stack pointer. It points to the topmost item of the call stack.
BP: base pointer. accessing parameters passed by the stack. It is the offset address relative to stack
segment
SI: source Index register. It is used in the pointer addressing of data.
DI: destination index register. It is used in the pointer addressing of data.
2) segment registers (for CPU control Unit)
IP: instruction pointer register, which points to next instruction to be executed in the code
What if we need to switch to a different process?
every process has its own data segment and code segment
CS: code segment register. It points to code in the memory location
DS: data segment register. It points to data in the memory location
SS: Stack segment register. It points to the call stack segment in the memory location.
ES: extra segment register.
CS and DS stores the start address of the code segment and data segment. And the IP stores the offset of the code segment. The data segment offset is stored in the general-purpose register.

--

--