630 likes | 731 Views
20-755: The Internet Lecture 2: Computer Systems I. David O’Hallaron School of Computer Science and Department of Electrical and Computer Engineering Carnegie Mellon University Institute for eCommerce, Summer 1999. Today’s lecture. Administrative issues (10 minutes) Data (50 min)
E N D
20-755: The InternetLecture 2: Computer Systems I David O’Hallaron School of Computer Science and Department of Electrical and Computer Engineering Carnegie Mellon University Institute for eCommerce, Summer 1999
Today’s lecture • Administrative issues (10 minutes) • Data (50 min) • Break (10 min) • Programs (40 min)
Systems • A system is a collection of interworking parts. • Examples: • the human body • the economy • a car • a stereo • a computer • Systems are often extremely complicated. • How do we understand complex systems? • Abstraction is the key.
Abstraction • Example: a lighting system light bulb switch A A B B
Abstraction • Here’s one way to understand how our lighting system works: “Closing the switch induces a voltage drop between A and B, which causes current to flow through the light bulb, which heats up the filament, which causes the filament to emit light. Opening the switch eliminates the voltage differential, which stops the current, which causes the filament to cool and stop emitting light.”
Abstraction • Here’s another way to understand our lighting system: “Close the switch and light turns on. Open the switch and the light turns off” • This is an example of an abstraction, where we describe the behavior of a system in terms of its inputs (the position of the switch) and its outputs (whether it is emitting light or not).
Abstraction • Abstraction is one of the most powerful weapons in the arsenal of computer science. • Useful because it hides complexity. • High-level languages like C, Java, and Perl provide abstractions for low-level machine instructions. • Operating systems provide abstractions for resources such as the CPU, memory, and I/O devices. • TCP/IP provides an abstraction for collections of interconnected heterogeneous networks. • However, abstractions are most useful if we understand something about how things work.
Typical computer system Keyboard Mouse Modem Printer Processor Interrupt controller Keyboard controller Serial port controller Parallel port controller Local/IO Bus Video adapter Network adapter Memory IDE disk controller SCSI controller SCSI bus disk Network Display disk cdrom
Bits • All computer data (input, output, memory, and even programs) are collections of 1’s and 0’s called bits (binary digits). • “0” and “1” are abstractions for voltage levels. • easy to store with bistable elements and can be reliably transmitted on noisy and innacurate wires. 0 1 0 3.3V 2.8V 0.5V 0.0V
Powers of 10 • Def: 10p = 10 x 10 x ... x 10 (there are p 10’s) • “ten to the power p” or “the pth power of 10” • Examples of powers of 10 100 = 1 101 = 10 102 = 10 x 10 = 100 103 = 10 x 10 x 10 = 1,000
Decimal (base 10) numbers • 10 digits to choose from: 0, 1, ... , 8, 9. • Each digit corresponds to a different power of 10. • Examples: 345 = (3 x 102) + (4 x 101) + (5 x 100) = 300 + 40 + 5 132 = (1 x 102) + (3 x 101) + (2 x 100) = 100 + 30 + 2
Powers of 2 • Def: 2p = 2 x 2 x ... x 2 (there are p 2’s) • “two to the power p” or “the pth power of two” • Examples of powers of 2 20 = 1 21 = 2 22 = 2 x 2 = 4 23 = 2 x 2 x 2 = 8 24 = 2 x 2 x 2 x 2 = 16
Binary (base 2) numbers • 2 digits to choose from: 0, 1. • Each digit corresponds to a different power of 2. • Examples (converting from binary to decimal) 1012 = (1 x 22) + (0 x 21) + (1 x 20) = 4 + 0 + 1 = 5 0112 = (0 x 22) + (1 x 21) + (1 x 20) = 0 + 2 + 1 = 3
Converting from decimal to binary • converting decimal 5 to binary 5 / 2 = 2 rem 1 first (righmost) binary digit is 1 2 / 2 = 1 rem 0 second binary digit is 0 1 / 2 = 0 rem 1 third binary digit is 1 So 5 = 1012 • converting decimal 3 to binary 3 / 2 = 1 rem 1 first (righmost) binary digit is 1 1 / 2 = 0 rem 1 second binary digit is 1 So 3 = 112 = 0112
Counting in binary • With n bits, you can represent 2n numbers:0, 1, ... , 2n - 1. • Example: 1 bit (21 = 2 numbers) 02 (0) 12 (1) • Example: 2 bits (22 = 4 numbers) 002 (0) 012 (1) 102 (2) 112 (3)
Counting in binary (cont.) • Example: 3 bits (23 = 8 numbers) 0002 (0) 0012 (1) 0102 (2) 0112 (3) 1002 (4) 1012 (5) 1102 (6) 1112 (7)
Practice problems Powers of two: (a) 20 = (b) 21 = (c) 25 = Binary to decimal: (a) 00102 = (b) 01112 = (c) 10012 = (d) 11112 = Decimal to binary (a) 8 = (b) 9 = (c) 12 = (d) 14 =
Practice problems: Counting in binary (a) how many numbers can you represent using 4 bits? (b) What is the largest number? (c) write them out in binary, starting from 00002:
Two’s complement representation of signed integers sign bit: 0: positive 1: negative 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 (0) (1) (2) (3) (4) (5) (6) (7) (-8) (-7) (-6) (-5) (-4) (-3) (-2) (-1) positive integers negative integers
Interpreting two’s complement numbers • To negate a two’s complement number: • invert the bits (I.e., change each 0 to 1 and each 1 to 0). • add 1 to the result. • Examples: • To negate 0 = 00002 : 11112+ 1 = 00002 • To negate -2 = 11102 : 00012 + 1 = 00102 • You can use this property to determine the two’s complement representation of a negative number. • Example: -5 = inv(0101) + 1 = 1010 + 1 = 1011, where inv(x) inverts the bits of x. • Example: -12 = ?
Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Hex 0 1 2 3 4 5 6 7 8 9 a b c d e f Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Hex (base-16) representation of binary numbers • Hex is a compact way to represent binary numbers. Each hex digit represents a byte (8 bits) of data: • 011010102 = 6a16 • 10111110111011112 = beef16 • In Perl, use the “0x” prefix to denote a hex number: • x = 0x6a; • y = 0xbeef;
Characters • ASCII characters are represented in 8-bit chunks called bytes. • Two types of characters: • printable characters: characters that can be typed on a keyboard (e.g., ‘d’, ‘%’h) • unprintable control characters (e.g., BEL,BS)
ASCII character set hex char esc 00 NUL '\0' 01 SOH 02 STX 03 ETX 04 EOT 05 ENQ 06 ACK 07 BEL '\a' 08 BS '\b' 09 HT '\t' 0A LF '\n' 0B VT '\v' 0C FF '\f' 0D CR '\r' 0E SO 0F SI 10 DLE 11 DC1 12 DC2 13 DC3 14 DC4 15 NAK 16 SYN 17 ETB 18 CAN 19 EM 1A SUB 1B ESC 1C FS 1D GS 1E RS 1F US 20 SPACE 21 ! 22 " 23 # 24 $ 25 % 26 & 27 ’ 28 ( 29 ) 2A * 2B + 2C , 2D - 2E . 2F / 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7 38 8 39 9 3A : 3B ; 3C < 3D = 3E > 3F ? 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G 48 H 49 I 4A J 4B K 4C L 4D M 4E N 4F O 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W 58 X 59 Y 5A Z 5B [ 5C \ '\\' 5D ] 5E ^ 5F _ 60 ' 61 a 62 b 63 c 64 d 65 e 66 f 67 g 68 h 69 i 6A j 6B k 6C l 6D m 6E n 6F o 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w 78 x 79 y 7A z 7B { 7C | 7D } 7E ~ 7F DEL
Computer memory Keyboard Mouse Modem Printer Processor Interrupt controller Keyboard controller Serial port controller Parallel port controller Local/IO Bus Video adapter Network adapter Memory IDE disk controller SCSI controller SCSI bus disk Network Display disk cdrom
Metrics for space and time • Space • Kilobyte (KB) = 210 • approx. 102 , “a thousand” • Megabyte (MB) = 220 • approx. 106, “a million” • Gigabyte (GB) = 230 • approx. 109, “a billion” • Terabyte (TB) = 240 • approx. 1012, “a trillion” • Time • millisecond (ms) = 10-3 s • .001 s • “a thousandth of a sec” • microsecond (us) = 10-6 s • .000001 s • “a millionth of a second” • nanosecond (ns) = 10-9 s • .000000001 s • “a billionth of a second” • 1 ns is the time it takes light to travel about 12 in!
Computer memory Bytes Addr • Organized as a sequential array of bytes. • Each byte has an integer address (location). • Addresses start at 0. 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015
Words Words Bytes Addr • Machine has “word size” • nominal size of numbers, including addresses. • Numbers, instructions, and addresses typically fractions or multiples of the word size. • Words are addressed by first byte. • PC-class machines are 32 bits • Limits addresses to 4 GB. • Becoming too small! • Newer server-class machines are 64 bits (e.g. DEC Alpha) • Limits addresses to 4 TB. • In 20 years, we’ll be complaining about this too! 0000 0001 0002 Addr = 0000 0003 0004 0005 0006 0007 0008 Addr = 0008 0009 0010 0011 0012 Addr = 0012 0013 0014 0015
Strings Bytes Addr • A string is represented in memory as a sequence of bytes terminated by 0x00 (‘\0’). • Known as a “null-terminated string” • Example (Perl): • $x = “Hi Dave!\n”; 0000 0001 0002 0003 ‘H’ 0004 ‘I’ 0005 SPACE 0006 ‘D’ 0007 ‘a’ 0008 ‘v’ 0009 ‘e’ 0010 ‘\0’ 0011 0012 0013 0014 0015
Data Representation Summary • Key concepts: • It’s all just bits! • everything in a computer is represented as a collection of bits that are interpreted in different ways. • memory is organized as a sequence of bytes • each byte in memory has its own address. • each machine has nominal word size • numbers are fractions or multiples of words • the address of a word is the address of its first byte • floating point representation is used for non-integral numbers: • e.g., 3.14159 • too complex for us to study in this course.
Today’s lecture • Administrative issues (10 minutes) • Data (50 min) • Break (10 min) • Programs (40 min)
Programs Keyboard Mouse Modem Printer Processor Interrupt controller Keyboard controller Serial port controller Parallel port controller Local/IO Bus Video adapter Network adapter Memory IDE disk controller SCSI controller SCSI bus disk Network Display disk cdrom
Memory Object Code Program Data Processor components Processor Addresses P C ALU Register File • PC (Program Counter) • Contains address of the next instruction • ALU (Arithmetic/Logic Unit) • addition, subtraction, etc. • Register File • Small fast internal memory for heavily used program data, typically 16, 32, or 64 locations (called registers), each of which holds 1 word. • Memory • Contains both object code and data Data Instructions Register name Register file R0 R1 R2 ... R30 R31
Object code • Processor simply executes one machine language instruction after another until you unplug it. • A program (or code) is a related set of these instructions. • Key idea: Programs are stored in memory as simply another kind of data • variously called object code, machine code, object program. • Example: • on the DEC Alpha, the word 0x42110401 is the bit pattern for an instruction that adds the contents of two registers and stores the result in a third register.
Assembly Language • machine language instructions are represented in ASCII text form as assembly language instructions. • Assembly • Add the integers in registers 16 and 17 and store the result in register 1. • Object Code • 32-bit pattern • Stored at address 0x1200012d0 addq r16,r17,r1 0x1200012d0: 0x42110401b
Basic processor operation • Fetch • load an instruction from memory, using the address contained in the PC. • Execute • load word from memory to register file • store word from register file to memory • perform an arithmetic operation (e.g., addition) on the contents of two registers and store the results in a register. • Update PC • if Branch instruction • set PC to some new address • if not Branch instruction • increment PC to point to next sequential instruction • ex: PC <-- PC + 4
Memory Object Code Program Data Basic instructions: load • Move a word from memory to a register. • example: ld r1, addr • move word at address addr (Mem[addr]) to register r1. • increment PC with address of next instruction. Processor addr P C ALU Register File Mem[addr] Mem[PC] “ld r1, addr” PC <-- PC + 4
Memory Object Code Program Data Basic instructions: store • Move a word from a register to memory • example: st r1, addr • move contents of register r1 (Reg[r1]) to memory address addr. • increment PC with address of next instruction Processor <addr> P C ALU Register File Reg[r1] Mem[PC] “st r1, addr” PC <-- PC + 4
Memory Object Code Program Data Basic instructions: arithmetic operations • Add the contents of two registers and store the result in a third register • example: add r16,r17,r1 • add contents of r16 and r17 and store the results in r1. • increment PC with address of next instruction Processor P C ALU Register File Mem[PC] “add r16, r17, r1” PC <-- PC + 4
Memory Object Code Program Data Basic instructions: branch • branch to a new location in the program • example: branch addr • set the PC to addr Processor P C ALU Register File Mem[PC] “branch addr” PC <-- addr
Altering the control flow • Changing the default value of the PC (I.e., pointing to the next instruction in memory) is called altering the control flow. • There are two mechanisms for altering the control flow: • executing branch instructions • exceptions • crucial mechanism for modern multitasking operating systems.
Exceptions and interrupts Keyboard Mouse Modem Printer Processor Interrupt controller Keyboard controller Serial port controller Parallel port controller Local/IO Bus Video adapter Network adapter Memory IDE disk controller SCSI controller SCSI bus disk Network Display disk cdrom
Exceptions • An exception is a transfer of control to the OS in response to some event (i.e. change in processor state) User Process Operating System exception processing by exception handler (also called a device driver or an interrupt handler) event exception exception return (optional)
Internal (CPU) exceptions • Internal exceptions occur as a result of events generated by executing instructions. • Execution of a SYSCALL instruction. • allows a program to ask for OS services (e.g., timer updates) • Execution of a BREAK instruction • used by debuggers • Errors during instruction execution • arithmetic overflow, address error, parity error, undefined instruction • Events that require OS intervention • virtual memory page fault
External (I/O) exceptions(or I/O interrupts) • External exceptions occur as a result of events generated by devices external to the processor (managed by interrupt controller). • I/O interrupts • hitting ^C at the keyboard • arrival of a packet from a network • arrival of data from a disk • Hard reset interrupt • hitting the reset button • Soft reset interrupt • hitting ctl-alt-delete on a PC
High-level languages • High-level languages like C/C++, Java, and Perl provide an abstraction for the low-level details of machine-language programs. Corresponding machine/assembly code C Code foo: 0x42100400 addq a0,a0,v0 0x40110400 addq v0,a1,v0 0x4c120400 mulq v0,a2,v0 0x6bfa8001 ret zero,(ra),1 long foo(long a, long b, long c) { long sum = (a+a+b)*c; return(sum); }
Procedures • Procedures (or functions) are named collections of commonly executed instruction sequences. • Crucial abstraction mechanism in every programming language at every level. • take some input, produce some output
Perl procedure example # definition of function say # prints first parameter followed by second sub say { print “$_[0], $_[1]!\n”; } # # main body of Perl program # # first invocation of s say(“hello”,“world”); # prints “hello, world!” # second invocation of say $x = “hi there”; $y = “Dave”; say($x, $y); # prints “hi there, Dave!” From “Learning Perl”, page 95.
Compiled vs interpreted programs • Compiled programs: • translated in a series of steps by a compiler, assembler, and linker from an ASCII source program written in a high level language to a binary executable... • and then loaded into memory and executed by a loader. • Examples: C/C++ compilers, Java Just-in-Time (JIT) compilers • Interpreted programs: • executing interpreter reads the ASCII source program and executes its statements. • Examples: Java virtual machine (JVM), Perl interpreter. • Compiled object code is to processor as interpreted source program is to interpreter.
Compiled programs “source program” ASCII text files C program (p1.c p2.c) Compiler ASCII text files Asm program (p1.s p2.s) Assembler binary files Object program (p1.o p2.o) libraries (.a) Linker binary file “executable program” Executable object program (p) Loader Executing process (p) memory