Lec 1B
Lec 1B
• Linux Systems Programming, R. Love, Second Edn, 2013, O'Rielly Media, ISBN-13:
978-1449339531.
• Linux Kernel Development, R. Love, Third Edn., ISBN-13: 978-0672329463.
• Contact email: [email protected]
• Office Hours: Fri. 1500hrs – 1600hrs (Or by appointment).
• TA office hours: TBA
Exactly What is a Computer
Most Basic Computer aka Turing Machine
• Proposed by Alan Turing (circa. 1936)
• Model consists of infinite tape divided into cells which contain input
symbols.
• Head reads the top of the symbol and changes the internal state of
the machine when the symbol is something that is not unexpected.
• The machine thus moves to the next state until it reaches a terminal
state which makes that the input is invalid…
Evolution of Computers
• Abacus – Chinese, Roman and Russian – ancient form of calculator.
• Relies on a series of beads and their movements to perform calculations.
Babbage Analytical Machine
• Mechanical form of the Abacus used for calculating polynomial
functions.
• Separation of storage and processing…
20th Century – Enigma Machine
- Designed by Arthur Scherbius (Axis Germany) towards end of
WW1 to send encrypted messages
Input
Output
(via
Glowlamps)
Collosus Machine
- Designed by Britain (Allied) to break encrypted messages of
enigma...
Post WW2 – Electronic Numerical Integrator and Computer (ENIAC)
Operation time samples: 10-digit num x 10-digit num took 2800 µsec,
https://www.youtube.com/watch?v=rO2SScF6rrM
Post WW2 – General Purpose Computers Post 1960s
• Evolutionary design
• Backwards compatible up until 8086, introduced in 1978
• Added more features as time goes on
Home computing:
- IBM PC – Intel 8088 microprocessor
- 8 bit microprocessor, booted using floppy disks…
- 5MB HDDs with 640KB of RAM
- PC-DOS operating system.
- Apple Lisa and Macintosh – GUI based interface.
- Motorola 68000 microprocessor with about 128 KB of RAM (upgraded to 512 KB of RAM)
- Newer faster processors evolved post 1990s – Intel 80386/486/Pentium processor – 32-bit
- RAM sizes increased to 16MB 🡪 32 MB 🡪 64 MB
Cellular Communication Married to Computers – Smart Phones
- Computers
Age of wearables and implantables…
- Microprocessors are now fitted into wearable devices (e.g. watches)
- Watches + smartphones = smart-watches
- Temperature and vital statistics monitoring
- Implantables….
- Often specific purpose (not so generic…)
Components of Computer System
Layered Architecture
User Programs
P6 P5 P4 P3 P2 P1
Interrupt handlers
Hardware
Interrupts and Interrupt Handling (Heart of the OS)
• Interrupts:
- ALL devices send a signal to the CPU whenever they require to ``communicate”
with the CPU.
- CPU is switched to Interrupt Handler program (part of the OS/supplied
externally).
- The Interrupt Handler ``saves’’ various information with respect to the running
program and it is jumped to a fixed location in memory – the IH routine.
- The IH routing – binaries (programs) that involve instructions and commands to
communicate with the device.
- The IH routine may either handle the device interrupt and jump back to executing
the saved task
- In case of task scheduler timer clock interrupt the CPU scheduler handles the
tasks/process and handles it.
Processes/Tasks/Threads
• Running program binary
• The OS loads up the binary
• Runtime linker supplies the necessary address bindings – the address of memory
where local variables are stored – MMU Used.
• Saves ``state’’ of the running program in Process Control Block (PCB) for each
process.
• Loads the ``state’’ of the new program and starts executing it.
• Every so often a timer interrupt ``interrupts’’ the CPU to switch the
process/task.
• Timer interrupt handler 🡪 invokes 🡪 process scheduler.
Multi-Processing (Why you exactly need an OS)
• Multiple processes running in tandem giving the illusion that multiple
programs are running.
• Process executions synchronized with the help of the process/task
scheduler.
• Scheduler is invoked periodically by the timer interrupt handler which saves the
state of a task and loads up another.
• Gives illusion of multi-processing.
• Task state –
• Every running program has a task / process state which includes:
• Process ID (PID)
• CPU registers
• Memory addresses for code, stack, data, heap and BSS
• I/O operations in progress.
• Interrupts that are unhandled.
• Previous process running and next process that needs to be run (in the process queue).
Processes
P6 P5 P4 P3 P2 P1
Application Layer
Real Time
Clock
Timer
Interrupt
Handler
Intel’s 64-Bit
• Intel Attempted Radical Shift from IA32 to IA64
• Totally different architecture (Itanium)
• Executes IA32 code only as legacy
• Performance disappointing
• AMD Stepped in with Evolutionary Solution
• x86-64 (now called “AMD64”)
• Intel Felt Obligated to Focus on IA64
• Hard to admit mistake or that AMD is better
• 2004: Intel Announces EM64T extension to IA32
• Extended Memory 64-bit Technology
• Almost identical to x86-64!
• All but low-end x86 processors support x86-64
• But, lots of code still runs in 32-bit mode
Assembly Programmer’s View
CPU Memory
Addresses
Registers
Data Code
PC Data
Condition Instructions Stack
Codes
Programmer-Visible State
• PC: Program counter • Memory
• Address of next instruction • Byte addressable array
• Called “EIP” (IA32) or “RIP” (x86-64) • Code and user data
• Register file • Stack to support procedures
• Heavily used program data
• Condition codes
• Store status information about most
recent arithmetic operation
• Used for conditional branching
What are registers?
1. Computer main memory latency : 80 nanoseconds.
2. A 3.3 GHz CPU takes ~ 0.3 nsecs per cycle memory latency is about 240
cycles.
3. The Core i7 has 3 levels of cache with different latencies.
I. Level 3 ~ 48 nsec (~ 150 cycles).
II. Level 2 ~ 10 nsec (~ 39 cycles).
III. Level 1 ~ 4 nsec (~12 cycles).
• Transfer control
• Unconditional jumps to/from procedures
• Conditional branches
Object Code • Assembler
Code for sum • Translates .s into .o
0x401040 <sum>: • Binary encoding of each instruction
0x55
• Nearly-complete image of executable
0x89 code
0xe5 • Missing linkages between code in
0x8b
different files
0x45
0x0c • Linker
0x03
0x45 • Resolves references between files
0x08 • Total of 11 bytes • Combines with static run-time
0x5d
• Each instruction libraries
0xc3
1, 2, or 3 bytes • E.g., code for malloc, printf
• Starts at address • Some libraries are dynamically linked
0x401040 • Linking occurs when program begins
execution
Machine Instruction Example
• C Code
int t = x+y;
• Add two signed integers
• Assembly
addl 8(%ebp),%eax • Add 2 4-byte integers
• “Long” words in GCC parlance
Similar to expression: • Same instruction whether signed
x += y or unsigned
More precisely: • Operands:
int eax; x: Register %eax
int *ebp; y: Memory M[%ebp+8]
eax += ebp[2] t: Register %eax
• Return function value in %eax
0x80483ca: 03 45 08
• Object Code
• 3-byte instruction
• Stored at address
0x80483ca
Disassembling Object Code
Disassembled
080483c4 <sum>:
80483c4: 55 push %ebp
80483c5: 89 e5 mov %esp,%ebp
80483c7: 8b 45 0c mov 0xc(%ebp),%eax
80483ca: 03 45 08 add 0x8(%ebp),%eax
80483cd: 5d pop %ebp
80483ce: c3 ret
• Disassembler
objdump -d p
• Useful tool for examining object code
• Analyzes bit pattern of series of instructions
• Produces approximate rendition of assembly code
• Can be run on either a.out (complete executable) or .o file
Alternate Disassembly
Object Disassembled
0x401040:
0x55 Dump of assembler code for function sum:
0x89 0x080483c4 <sum+0>: push %ebp
0xe5 0x080483c5 <sum+1>: mov %esp,%ebp
0x8b 0x080483c7 <sum+3>: mov 0xc(%ebp),%eax
0x45 0x080483ca <sum+6>: add 0x8(%ebp),%eax
0x0c 0x080483cd <sum+9>: pop %ebp
0x03 0x080483ce <sum+10>: ret
0x45
0x08
0x5d • Within gdb Debugger
0xc3 gdb p
disassemble sum
• Disassemble procedure
x/11xb sum
• Examine the 11 bytes starting at sum
What Can be Disassembled?
% objdump -d WINWORD.EXE
No symbols in "WINWORD.EXE".
Disassembly of section .text:
30001000 <.text>:
30001000: 55 push %ebp
30001001: 8b ec mov %esp,%ebp
30001003: 6a ff push $0xffffffff
30001005: 68 90 10 00 30 push $0x30001090
3000100a: 68 91 dc 4c 30 push $0x304cdc91
source
%esi %si index
destination
%edi %di index
stack
%esp %sp
pointer
base
%ebp %bp
pointer
movl (%ecx),%eax
movl 8(%ebp),%edx
Move with sign extend or zero extend
1. If you move a double word into a double word register, the upper half is zeroed out.
2. If you move a 32 bit immediate into a 64 bit register it is sign extended.
3. Load a smaller value from memory and fill the rest of the register with zeroes. Or sign
extend a small value from memory.
popl %ebx
popl %ebp Finish
ret
Using Simple Addressing Modes
swap:
pushl %ebp
void swap(int *xp, int *yp) Set
movl %esp,%ebp Up
{
pushl %ebx
int t0 = *xp;
int t1 = *yp;
*xp = t1; movl 8(%ebp),
*yp = t0; %edx
} movl 12(%ebp), Body
%ecx
movl (%edx),
%ebx
movl (%ecx),
%eax
movl %eax, Finish
(%edx)
movl %ebx,
(%ecx)
popl %ebx
Understanding Swap
•
•
void swap(int *xp, int *yp)
{
• Stack
Offset
int t0 = *xp; (in memory)
int t1 = *yp; 12 yp
*xp = t1; 8 xp
*yp = t0;
} 4 Rtn adr
0 Old %ebp %eb
-4 Old %ebx p
%esp
Register Value
movl 8(%ebp), %edx # edx = xp
%edx xp
movl 12(%ebp), %ecx # ecx =
%ecx yp
yp
%ebx t0 movl (%edx), %ebx # ebx = *xp
%eax t1 (t0)
movl (%ecx), %eax # eax = *yp
(t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
Address
Understanding Swap 123 0x124
456 0x120
0x11c
%eax
0x118
%edx Offset
0x114
%ecx yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
4 Rtn adr 0x108
%esi
%eb 0 0x104
%edi p -4
0x100
%esp movl 8(%ebp), %edx # edx = xp
movl 12(%ebp), %ecx # ecx =
%ebp 0x104 yp
movl (%edx), %ebx # ebx = *xp
(t0)
movl (%ecx), %eax # eax = *yp
(t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
Address
Understanding Swap 123 0x124
456 0x120
0x11c
%eax 0x118
%edx 0x124 Offset
0x114
%ecx yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
4 Rtn adr 0x108
%esi
%eb 0 0x104
%edi p -4
0x100
%esp
movl 8(%ebp), %edx # edx = xp
%ebp 0x104 movl 12(%ebp), %ecx# ecx = yp
movl (%edx), %ebx # ebx = *xp (t0)
movl (%ecx), %eax # eax = *yp (t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
Address
Understanding Swap 123 0x124
456 0x120
0x11c
%eax
0x118
%edx 0x124 Offset
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
4 Rtn adr 0x108
%esi
%eb 0 0x104
%edi p -4
0x100
%esp
movl 8(%ebp), %edx # edx = xp
%ebp 0x104 movl 12(%ebp), %ecx# ecx = yp
movl (%edx), %ebx # ebx = *xp (t0)
movl (%ecx), %eax # eax = *yp (t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
Address
Understanding Swap 123 0x124
456 0x120
0x11c
%eax
%eax 0x118
%edx
%edx 0x124 Offset
0x114
%ecx
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
%ebx 123
4 Rtn adr 0x108
%esi
%esi
%eb 0 0x104
%edi
%edi p -4
0x100
%esp
%esp
movl 8(%ebp), %edx # edx = xp
%ebp
%ebp 0x104 movl 12(%ebp), %ecx# ecx = yp
movl (%edx), %ebx # ebx = *xp (t0)
movl (%ecx), %eax # eax = *yp (t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
Address
Understanding Swap 123 0x124
456 0x120
0x11c
%eax 456 0x118
%edx 0x124 Offset
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx 123
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 8(%ebp), %edx # edx = xp
%ebp 0x104 movl 12(%ebp), %ecx# ecx = yp
movl (%edx), %ebx # ebx = *xp (t0)
movl (%ecx), %eax # eax = *yp (t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
Address
Understanding Swap 456 0x124
456 0x120
0x11c
%eax
%eax 456
456 0x118
%edx
%edx 0x124 Offset
0x114
%ecx
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
%ebx 123
4 Rtn adr 0x108
%esi
%esi
%ebp 0 0x104
%edi
%edi -4
0x100
%esp
%esp
movl 8(%ebp), %edx # edx = xp
%ebp 0x104 movl 12(%ebp), %ecx# ecx = yp
movl (%edx), %ebx # ebx = *xp (t0)
movl (%ecx), %eax # eax = *yp (t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
Address
Understanding Swap 456 0x124
123 0x120
0x11c
%eax 456 0x118
%edx 0x124 Offset
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx 123
%es 4 Rtn adr 0x108
i %ebp 0 0x104
%edi -4
0x100
%esp
movl 8(%ebp), %edx # edx = xp
%ebp 0x104 movl 12(%ebp), %ecx # ecx =
yp
movl (%edx), %ebx # ebx = *xp
(t0)
movl (%ecx), %eax # eax = *yp
(t1)
movl %eax, (%edx) # *xp = t1
Complete Memory Addressing Modes
• Most General Form
D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D]
• D: Constant “displacement” 1, 2, or 4 bytes
• Rb: Base register: Any of 8 integer registers
• Ri: Index register: Any, except for %esp
• Unlikely you’d use %ebp, either
• S: Scale: 1, 2, 4, or 8 (why these numbers?)
• Special Cases
(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]]
D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D]
(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]]
Data Representations: IA32 + x86-64
• Sizes of C Objects (in Bytes)
C Data Type Generic 32-bit Intel IA32 x86-64
• unsigned 4 4 4
• int 4 4 4
• long int 4 4 8
• char 1 1 1
• short 2 2 2
• float 4 4 4
• double 8 8 8
• long double 8 10/12 10/16
• char * 4 4 8
• Or any other pointer
64-bits)
32-bits
Registers
16-bits
accumulator
rbx ebx bx bh bl
general purpose
accumulator
rsi esi si
rdi edi accumulator
di
rsp esp sp stack pointer
r8 r8d accumulator
• New instructions:
• movl ➙ movq
• addl ➙ addq
• sall ➙ salq
• etc.
popl %ebx
popl %ebp Finish
ret
64-bit code for swap
swap: Set
void swap(int *xp, int *yp) Up
{
int t0 = *xp; movl (%rdi), %edx
int t1 = *yp; movl (%rsi), %eax Body
*xp = t1; movl %eax, (%rdi)
*yp = t0;
movl %edx, (%rsi)
}
Finish
ret
• Operands passed in registers (why useful?)
• First (xp) in %rdi, second (yp) in %rsi
• 64-bit pointers
• No stack operations required
• 32-bit data
• Data held in registers %eax and %edx
• movl operation
64-bit code for long int swap
swap_l: