CPU Architecture
Von Neumann
- Input device -> (CPU <-> Memory) -> Output device
- Input device used to load programs & data to memory (Stored program concept)
CPU
- Fetch-decode-execute
- Very basic instructions in memory
- Different types of instruction set
- CISC (Desktop/Server CPUs)
- RISC (Smartphones, IoT devices)
- Synchronised by a fast clock measured in hz
System Bus
- Allows communication between components
- Without a bus, every component needs to be directly connected to each other (point-to-point system)
- Multiple lines
- Address lines - memory address
- Data lines - data to be transferred
- Control lines - instruction
- Modern computers have multiple interconnected buses (SATA, PCIe, USB)
- Problem of bus contention as only one thing can be on the bus at once
I/O & Interrupts
- Devices include expansion cards
- Some devices can be built into mobo
- Peripherals are plug-in (eg. keyboard, mouse)
- Polling
- Periodically check device status
- Requires CPU to stop what it's doing (wastes time)
- Interrupts
- Device sends signal to CPU when it's ready
- Invokes an interrupt handler within OS
- Intercepts interrupt and decides when CPU handles it
Internal Memory
- Programs & Data must be converted to binary and loaded into memory before being processed (stored program concept)
- Memory:
- RAM - R/W, volatile, main memory
- ROM - Read only, non-volatile, stores boot code
- bit length - how much memory can be moved and manipulated by CPU in one operation
- 32bit software can run on 64bit machine but not vice versa
Bit Length & Word Size
- Bit length of system is related to word size of variables
- Size of registers
- Width of system bus
- Max unsigned int that can be stored relates to max memory size (modern memory is byte addressable)
- 16bit - word - 64kb
- 32bit - dword - 4gb
- 64bit - qword - 16eb
Registers
- General purpose registers
- Instruction Register (IR) - stores current instruction
- Instruction Pointer (IP) / Program Counter (PC) - stores address of next instruction
- Memory Address Register (MAR) / Memory Buffer Register (MBR) - used by CPU to interface with memory
Architecture
- Control unit (CU) - governs CPU activity
- Arithmetic Logic Unit (ALU) - bit manipulations & numeric operations
- CU provides ALU with operands and tells it what to do
- Registers much faster than memory
- Memory Management Unit (MMU)
Fetch-Execute
- Copy IP -> MAR
Issue read request to MMU
(CPU can do something else in time waiting) - Increment IP
- Instruction arrives from memory to MBR
Copy MBR -> IR - Decode IR
- Fetch operands
- Execute instruction
- Repeat
- Steps 5&6 may cause further memory access
Instruction Sets
Six broad categories of instructions:
- Transfer - moving between memory and registers
- Arithmetic - simple maths operations
- Logic - bit manipulations such as AND, OR, NOT, shift, rotate
- Test - comparing values and setting flags
- Control - jumps & subroutine calls
- Misc - various helper operations
Instruction Format
Each instruction has an opcode
- Number that CPU understands
- eg. 5 for addition
CPU gets operands from registers and/or memory
Instructions can have multiple opcodes depending on how the operands are encoded
eg. adding contents of two registers may have a different opcode to adding a value to a register
A single instruction can be 1 to 15 bytes long
It's assumed here that every instruction takes up 4 bytes of memory
Addressing Modes
The mode tells CPU where operands are located
- Immediate - straight value
- Register - operand in a register
- Direct - memory address to operand in main memory
- Register Indirect - memory address of operand stored in a register
Implicit is used when instruction doesn't need an operand because it always does the same thing
Assembly
- Low level programming
- Mnemonics for each machine code instruction
- Assembler used to compile into machine code
adjust: mov eax, num1 ; commentadjustis labelmov eax, num1moves num1 into eax
Intel x86 Registers
- EAX - accumulator (32bits)
- RAX - accumulator (64bits)
- AX - accumulator (16bits)
- AH - upper 8 bits of AX
- AL - lower 8 bits of AX
- EBX - base register
- ECX - counter register
- EDX - data register
- ESP - stack pointer
- EBP - stack frame base pointer
- EIP - instruction pointer
Status Flags
- CF - carry flag
- ZF - zero flag
- SF - sign flag: positive(0), negative(1)
- OF - overflow flag
Instructions
mov (move from one place in memory to another)
- Must involve a register
add (+)
mul (*)
inc (+1)
dec (-1)
div (/)
sub (-)
cmp (compare)
lea (load effective address)
jmp - unconditional
- Can jump depending on status flags or compare result
loop - uses ECX to count down, decrements ECX by one then jumps if ECX != 0
call (call a subroutine, set EIP to first instruction)
ret (return from subroutine, set EIP back to original call)
push (push value to program stack)
pop (pop top value from stack)
xchg (swap memory locations)
[] brackets can be used around a memory location to get it's address rather than its value
Arrays
- Items stored in consecutive memory locations
- Each integer takes up 4 bytes of memory
lea ebx, arrayloads the address of the first item in array into ebx[ebx]will give the first value in arrayadd ebx, 4,[ebx]will give the second value in array
Program Stack
- Stack grows from top of program down
- ESP - stack pointer
- push decrements ESP
- Then writes data to that location
- pop increments ESP
- Data is still in memory, just forgotten about
- call takes EIP and pushes it to stack, then puts address of subroutine into EIP
- ret pops item from stack and places it into ESP
add esp, 8moves the stack pointer up by 8, removing two items from the stack- Data on the stack can be inspected as an offset to ESP
Parameter Handling
Pass by Value - Values copied into registers
- Depends on caller and callee agreeing on registers to use to parameters and return value
Pass by Reference - Memory addresses of values as parameters - Caller and callee still need to agree on registers to use
The stack can be used for parameters
- Caller pushes parameters before making the call
- Callee pops parameters and uses them
- Stack must be tidied
- Must be agreed on order of parameters and who tidies the stack
Calling Conventions
Four calling conventions in Intel x86 architecture:
- cdecl - parameters pushed to stack in reverse order; caller cleans stack
- stdcall - parameters pushed to stack in reverse order; callee cleans stack
- thiscall - first parameter in ECX, rest reversed on stack; callee cleans
- fastcall - first two parameters in ECX/EDX, rest reversed on stack; callee cleans
fastcall and thiscall are faster if there are less parameters, but they pollute the registers
C library routines expect cdecl to be used
I/O in Assembly
Use printf and scanf
printf:
- Push address of string to print to stack with any extras if format specifiers then call
scanf: - Push address where input should be stored to stack then call
Clean stack after (eg.pop eaxoradd esp, 8)
Push important register values onto stack before calling a subroutine, and pop them back off after to avoid them being corrupted during the subroutine's execution
Format Specifiers
- %d - decimal integer
- %s - string
- %c - character
- %f - floating-point
Types must match, parameters must be correct amount and in correct order
Stack Frames
When a subroutine is called, a new stack frame is created on the stack, holding:
- Parameters
- Return address
- Local variables
ESP always points to the top of the stack
EBP always points to start of current stack frame - Can be used to access parameters and local variables as an offset
- eg. EBP-4 is address of second parameter that was pushed
When a subroutine is called:
- Parameters pushed to stack
- Return address pushed
- Value of EBP pushed
- Local variables reserved on stack (changing ESP)
- Current value of ESP is put into EBP (begin new stack frame)
When a subroutine is ready to return: - Remove local variables from stack
- Pop top value into EBP
- Pop top value into EIP
- Caller needs to clean extra parameters
Nested Calls and Stack Frames
- If a subroutine calls another nested subroutine
- The stack grows as a stack frame is built up
- Parameters
- Return address
- Old base pointer
- Local variables
- Values of EBP and ESP change as the calls happen
- ESP always points to the top of the stack
- EBP changes with each subroutine call
- Stack is cleaned up (gets smaller) as each subroutine returns
Recursion
A recursive subroutine calls itself
Mutual recursion is where two or more subroutines call each other
Two parts to recursive subroutines
- general case - calling itself recursively
- terminating case - causes it to stop
With no terminating case, code will be in infinite loop
Always possible to make iterative version of recursive algorithms
Iterative version simpler, usually when recursive version is tail-recursive
Accessing single char in memory
movzx edx, byte ptr [eax] gets the single character at location in eax, just gets a single byte and zero fills it
Operating Systems
Main purposes:
- Turn hardware components into usable devices
- Make efficient use of resources
Common OSs: - Windows
- Unix
- Linux, MacOS, iOS, Android
Embedded OS found in home appliances, TV boxes, game consoles, etc.
- Linux, MacOS, iOS, Android
Four essential parts of every OS
- Processor Manager
- File Manager
- Device Manager
- Memory Manager
Each manager must perform certain tasks:
- Continuous monitoring of resources
- Enforcement of policies
- Allocation/Deallocation of resources
Program vs Process
Program:
- Code that performs a task
- Source code, Object code
- Programs are static after compiled (don't change)
Process: - Activity that CPU performs when executing a program
- Code loaded from disk to memory
- IP starts at first instruction in memory
- Processes are dynamic (execution branches depending on input)
OS Structure
- central kernel
- permanently in memory
- low-level frequently needed activities
- kernel mode (privileged mode)
- shell
- Provides UI
- allows user to run and use programs
- user mode (restricted access)
- set of processes
- may be created by kernel to carry out its activities
- may be executed when user runs software
- can be privileged or non-privileged depending on how they were started