Electrical Engineering › Section X · Processors and integrated circuit technologies › Topic X.2
Operating systems, hardware, and programming
A processor on its own only fetches and executes bits. The operating system turns it into a machine that can run many programs safely, and programming languages let people write those programs without thinking in binary.
Hardware, software and the operating system
Hardware is everything physical in a computer: processor, memory, interface circuits, peripherals and power supply. Software is the programs it runs. Application software does a particular job, such as a spreadsheet or a circuit simulator. System software, chiefly the operating system (OS), sets up the environment applications run in and stands between them and the hardware. Computers mostly run Windows, macOS or Linux, phones run Android or iOS, and many embedded products use a small real-time OS, such as FreeRTOS or Zephyr, or none at all.
The three duties of an operating system
An operating system has three basic duties. First, it schedules and allocates resources: processor time, memory and access to peripherals. Second, it protects processes, the running programs, and the system itself, so that one program cannot corrupt another's code or data or reach hardware it should not touch. Third, it provides services, such as device drivers, files and messages between processes, so each application need not carry its own code for every disk and display. The price is a little speed, since going through the OS takes longer than touching hardware directly.
Multitasking and multiprocessing
Multitasking shares one processor core among several processes by giving each a turn. In preemptive multitasking a timer interrupt hands control to the OS at the end of each time slice, and the OS switches to another process whether or not the current one has finished. In cooperative (non-preemptive) multitasking a process runs until it gives the processor up, so one badly written program can freeze the rest. A context switch saves one process's registers and reloads another's. Multiprocessing uses several processors or cores at once, and each core can itself multitask.
Worked example: sharing one core by time slices
Three processes share one core in turn with 10 ms time slices, and each context switch costs 0.1 ms. 1. One full round takes \(3 \times (10 + 0.1) = 30.3\,\text{ms}\). 2. Each process runs for \(10 \div 30.3\), about 33.0% of the time. 3. Useful work is \(30 \div 30.3\), about 99.0% of the time, so switching wastes about 1.0%. 4. After its slice ends, a process waits for the other two slices and their switches, \(2 \times 10.1 = 20.2\,\text{ms}\), before running again. 5. Shrinking slices to 1 ms makes response quicker but raises the switching overhead to \(0.1 \div 1.1\), about 9.1%: schedulers trade responsiveness against efficiency.
Supervisor state and memory protection
Protection needs help from the hardware. The processor has a supervisor (kernel) state and a user state, set by a status bit. It starts in supervisor state at power-up, runs the OS there, and enters it on every interrupt; before starting an application, the OS switches to user state, where privileged instructions and protected regions are refused. A memory management unit (MMU) checks or translates every address. In the simplest case it compares each address with a permitted range: if a process may use \(4000_{16}\) to \(5\text{FFF}_{16}\), an access to \(6010_{16}\) raises a fault, and the OS takes over, usually ending the process. Real MMUs also translate addresses for virtual memory and control caching.
System calls and cooperating processes
A user-state process cannot call OS code directly. It loads registers with the details of its request and executes a trap, a deliberate software interrupt, which switches the processor into supervisor state and starts the OS handler. The handler performs the service, such as copying a message from one process's memory to another's, puts a result code in a register and returns. Processes that cooperate must be designed so that messages arrive in the right order. Otherwise two processes can each wait for the other to act first, so neither ever proceeds: a deadlock.
Levels of programming language
At the bottom, machine language is the binary code a processor executes. Assembly language replaces each machine instruction with a mnemonic such as ADD or JMP, one for one, so it belongs to one instruction set and is not portable. An assembler translates it into machine code. High-level languages such as C, C++, Java and Python are closer to human language and portable: a compiler for each processor turns the same C source into x86-64 or Arm machine code, while an interpreter translates and runs a program as it goes. Operating system kernels are written mostly in C with small assembly parts, and since about 2022 Linux also accepts some code in Rust.
Kinds of instructions
Instructions fall into similar groups on most processors; x86 names are used here. Data transfer: MOV copies between registers and memory, PUSH and POP use the stack. Arithmetic: ADD, SUB, INC, DEC, and CMP, which subtracts only to set the flags. Bit manipulation: AND, OR, XOR and NOT, shifts, where bits pushed out are lost (the last lands in the carry flag), and rotates, where they re-enter at the other end. Jumps: JMP always, conditional jumps such as JE only when the flags say so. CALL and RET enter and leave subroutines. String instructions move or compare blocks of bytes, and processor-control instructions such as NOP and HLT manage the processor itself.
Worked example: an assembly loop
This x86 assembly (32-bit, NASM syntax) adds a list of 32-bit numbers that ends with a zero; list labels the first number. 1. mov eax, 0 sets the total to zero. 2. mov ebx, list points ebx at the first number. 3. next: mov ecx, [ebx] loads the number ebx points to. 4. cmp ecx, 0 followed by je done jumps out when the zero end marker is reached. 5. add eax, ecx adds the number to the total. 6. add ebx, 4 steps to the next number, 4 bytes along. 7. jmp next repeats from step 3, and the label done follows the loop. Trace the list 7, 12, 5, 0: eax becomes 7, 19, then 24, and the fourth load, 0, ends the loop with 24 in eax.
The same task in C
In C the loop reads: int total = 0; const int *p = list; while (*p != 0) { total += *p; p++; } Here list is an array such as int list[] = {7, 12, 5, 0};, and p is a pointer holding an address, playing the part of ebx, while total plays eax. The expression *p means the value stored where p points. The statement p++ moves p on by one int, 4 bytes on mainstream platforms, so it matches add ebx, 4 without the programmer counting bytes. The C version is shorter and portable to any processor with a C compiler; the assembly shows exactly what the processor does.
Common mistakes to avoid
First, thinking multitasking means running at the same instant: on one core the processes take turns. Second, ignoring switching overhead: very short time slices waste a large share of the processor. Third, assuming assembly runs on any processor: it is tied to one instruction set. Fourth, confusing an assembler, which translates assembly one for one, with a compiler, which translates a whole high-level program. Fifth, forgetting that a pointer step is in bytes at machine level: add ebx, 4, not 1, for 32-bit numbers.
Key takeaways
Hardware is the physical system and software the programs it runs; the operating system allocates resources, protects processes and provides services. Multitasking time-slices one core, preemptively by timer interrupt or cooperatively; multiprocessing uses several cores. Supervisor and user states, an MMU and traps for system calls keep processes apart, and badly coordinated processes can deadlock. Machine code is binary, assembly is its one-for-one mnemonic form, and portable high-level languages are compiled or interpreted.
Flashcards
Flashcard
System software that runs applications and stands between them and the hardware
Test your understanding
Question 1 of 5
In which kind of multitasking does a timer interrupt let the operating system switch processes at the end of each time slice?
Question 2 of 5
Four processes share one core in turn with 20 ms time slices, and each context switch costs 0.5 ms. What percentage of the time does useful work?
Question 3 of 5
A simple MMU lets a process use addresses 2000₁₆ to 3FFF₁₆. Which access raises a fault?
Question 4 of 5
The x86 loop in the reading runs on the list 9, 4, 6, 0. What does eax hold when the loop ends?
Question 5 of 5
Why can one C program run on both an x86-64 PC and an Arm phone, when one assembly program cannot?
