CS485G classnotesRaphael FinkelMay 4, 20151 IntroLecture 1, 1/14/20151. Handout 1 — My names2. Plagiarism — read aloud3. E-mail list: [email protected]
CS485G Spring 2015 10iv. 10002= −8. Try to negate this number.v. 01112= 7.vi. 10012= −7.13 Compilation and disassembly in Linux1. Compiler for C: gcc.
CS485G Spring 2015 114. The microarchitecture describes how the architecture is implemented.It includes the sizes of caches and the frequency at which
CS485G Spring 2015 128. Generated x86 assembler (using -O1)sum:movl 8(%esp),%eaxaddl 4(%esp),%eaxret9. Interpretation: x is at 8(%ebp); y is at 4(%ebp
CS485G Spring 2015 133. Registers32-bit 16-bit 8-bit original purposeeax ax ah/al accumulatorebx bx bh/bl baseecx cx ch/cl counteredx dx dh/dl dataesi
CS485G Spring 2015 14pushl %ebp # save base pointermovl %esp,%ebp # new base pointerpushl %ebx # save old contents of %ebxmovl 8(%ebp), %edx # edx = x
CS485G Spring 2015 15(c) Load Effective Address of src and put it in dest.(d) Purpose: translate p = &x[i](e) Purpose: compute arithmetic expressi
CS485G Spring 2015 161 int arith(int x, int y, int z)2 {3 int t1 = x+y;4 int t2 = z+t1;5 int t3 = x+4;6 int t4 = y*48;7 int t5 = t3 + t4;8 int rval =
CS485G Spring 2015 1711. Result of compilationpushl %ebp # save base pointermovl %esp,%ebp # new base pointermovl 12(%ebp),%eax # $a = yxorl 8(%ebp),%
CS485G Spring 2015 185. The test instruction (testl b, a) also sets the flags; it’s like com-puting a&b without modifying the destination. Usually,
CS485G Spring 2015 191 int absdiff(int x, int y)2 {3 int result;4 if (x > y) {5 result = x-y;6 } else {7 result = y-x;8 }9 return result;10 }absdif
CS485G Spring 2015 23 Abstraction and reality1. Most CS and CE courses emphasize abstraction; it matches how wethink, and it lets us hide implementati
CS485G Spring 2015 20absDiff:pushl %ebp # save base pointermovl %esp,%ebp # new base pointermovl 8(%ebp), %edx # d = xmovl 12(%ebp), %eax # a = ycmpl
CS485G Spring 2015 211 int countOnes(unsigned x)2 {3 int result = 0;4 loop:5 result += x & 0x1;6 x >>= 1;7 if (x)8 goto loop;9 return result
CS485G Spring 2015 22(b) parameters(c) local variables(d) temporary locations (that don’t fit in registers)6. On the x86, the %epb register points to t
CS485G Spring 2015 2311. Linkage at the calling point for swap(&course1, &course2);subl $8,%esp # make room for two parametersmovl $course2, 4
CS485G Spring 2015 24(b) %ebx, %esi, %edi are non-volatile (callee-save) general-purposeregisters.(c) %esp and %ebp are non-volatile (callee-save) spe
CS485G Spring 2015 252. Example1 int add3(int x) {2 int localx = x;3 incrk(&localx, 3);4 return localx;5 }6 void incrk(int*ip, int k) {7*ip += k;8
CS485G Spring 2015 2625 x86 64 registers1. Eight upgraded 64-bit registers, now with names starting with r in-stead of e, such as %rax.2. Eight new 64
CS485G Spring 2015 2727 Arrays1. Lecture 15, 2/23/20152. C declaration: T myArray[L], where T is some type and L is thenumber of elements (the first is
CS485G Spring 2015 288. Loop example1 void zincr(myArrayType z) {2 int i;3 for (i = 0; i < ZLEN; i+=1)4 z[i] += 1;5 }# assume d = zmovl $0, %eax #
CS485G Spring 2015 298. Lecture 16, 2/25/20159. General addressing case: T A[R][C]; defines a two-dimensionalarray of type T with R rows and C columns.
CS485G Spring 2015 3(d) Operating systems need to deal with the intricacies of machinecode as they manipulate processes (keeping track of floatingpoint
CS485G Spring 2015 303. The compiler knows all the offsets.30 Linked lists1. Example:1 void set_val(struct rec*r, int val) {2 while (r) {3 int i = r-&
CS485G Spring 2015 31(b) 2 bytes (short): address ends with 02(c) 4 bytes (int, void *): address ends with 002(d) 8 bytes (double): address ends with
CS485G Spring 2015 3234 Unions1. Snow day: 3/6/20152. Lecture 18, 3/9/20153. A union type is like a struct, but the fields all start at offset 0, sothe
CS485G Spring 2015 332. The limit program shows per-process limitations; by default, for in-stance, the stack is limited to 8MB.36 Buffer overflow1. Un
CS485G Spring 2015 34gcc, use -fstack-protector (adds code to evidently suspi-cious routines) or -fstack-protector-all (adds code to allroutines)80486
CS485G Spring 2015 35(a) Programs define symbols and reference them:1 void swap() {...} // exported global identifier2 extern int myGlobal; // imported
CS485G Spring 2015 36(d) Windows calls shared object files Dynamic Link Libraries (DLLs).7. Lecture 21, 3/23/20158. Relocation(a) The linker combines t
CS485G Spring 2015 374. Global symbols: defined in this module, may be referenced by othermodules. In C: functions (except static) and file-global varia
CS485G Spring 2015 386. Advice:(a) Multiple compilation units should share global declarations viaa .h file.(b) Use static if possible to prevent confli
CS485G Spring 2015 3942 Interpositioning1. Replace standard routine with a special one in order to monitor (forexample, to find memory leaks), profile (
CS485G Spring 2015 4(h) There are tools to help you detect referencing errors (such asvalgrind).5 Binary representation1. Bits are represented by two
CS485G Spring 2015 40(a) Monolithic: many functions, all linked together.(b) Other: object-oriented, layered, dynamically loaded modules(c) Linux: Mos
CS485G Spring 2015 41(d) When the kernel is ready to return from the exception:i. The CPU changes to its previous mode (typically user mode)and its pr
CS485G Spring 2015 4247 System calls invoked by C library routines1. Many C library routines include system calls.(a) printf() calls write() which exe
CS485G Spring 2015 43(f) The only difference is the return value. For A, it is B’s processidentifier (PID). For B, it is 0.9. The exit() system call te
CS485G Spring 2015 442. Lecture 27, 4/6/20153. login starts a shell for each logged-in user.(a) sh: original shell, Stephen Bourne, 1977(b) csh: BSD U
CS485G Spring 2015 45(a) Terminate(b) Ignore(c) Terminate and create a dump file (called core).(d) Temporarily stop the process.(e) Continue the proces
CS485G Spring 2015 46(b) The POSIX standard requires 117 functions, such as write(),to be safe. But printf() is not safe.51 Nonlocal jumps1. Nonlocal
CS485G Spring 2015 47(a) open() and close(): open() returns a file descriptor; thekernel allocates and initializes a data structure. close() letsthe ke
CS485G Spring 2015 48(b) connect() to attach the socket to the server (like “call a num-ber”).5. Server(a) socket() to create a local file descriptor (
CS485G Spring 2015 499. Sample client for ”echo”1 int client(int argc, char**argv) {2 int toserverfd, port;3 char*host, buf[MAXLINE];4 rio_t rio;5 if
CS485G Spring 2015 56. What is the largest signed integer you can store in 16 bits? (Answer:215− 1 = 32767)7. How many bits do you need to store 4893?
CS485G Spring 2015 5010. Implementation of open clientfd1 int open_clientfd(char*hostname, int port) {2 int toserverfd;3 struct hostent*serverHostEntr
CS485G Spring 2015 5111. Sample server for ”echo”1 int server(int argc, char**argv) {2 int listenfd, connfd, listenPort;3 struct sockaddr_in clientAdd
CS485G Spring 2015 5212. Implementation of open listenfd1 int open_listenfd(int port) {2 int listenfd, optval=1;3 struct sockaddr_in serveraddr;45 /*C
CS485G Spring 2015 53(a) Read: reached the end of file, or the end of a line with fgets().(b) Network sockets or pipes: read or write can give a short
CS485G Spring 2015 54closedir(). readdir() returns a struct dirent pointer, whichincludes the name of the file.5. Directories associate file names with
CS485G Spring 2015 55(b) stdio: buffering increases efficiency, and short counts are han-dled automatically. But no way to read metadata, not async-sig
CS485G Spring 2015 56(a) Each process gets a uniform linear address space that does notcontain any addresses belonging to any other process or to thek
CS485G Spring 2015 57(d) Actually, instead of 220entries, the table is in two layers, but weignore that detail for simplicity.(e) Adjacent addresses w
CS485G Spring 2015 58(c) The general idea is to employ the ”recently accessed” bit in thepage table to find a victim that has not been recently accesse
CS485G Spring 2015 59ii. Every free region has tags and also is linked into a list offree regions (the link is in the region itself).iii. To allocate,
CS485G Spring 2015 68. You can also use a program:1 typedef unsigned char*pointer;2 void show_bytes(pointer start, int len){3 int i;4 for (i = 0; i &l
CS485G Spring 2015 60(c) Then mark all allocated regions that are still in use.(d) Then reclaim all allocated regions that are not marked.3. How to kn
CS485G Spring 2015 613. An internetwork is an interconnected set of networks; the Internet isthe best example.4. Lowest level: ethernet segment(a) Hos
CS485G Spring 2015 62(b) Software on hosts and routers agree on a common internet pro-tocol independent of the capacities of routers and links.9. IPv4
CS485G Spring 2015 63(d) Unix sockets can be established for either UDP or TCP.12. Internet components(a) Backbone: collection of routers connected to
CS485G Spring 2015 64(c) Class C: first octet 192-224; second and third octet 0-255. 28ad-dresses in region.(d) Special purpose addresses: first octet 2
CS485G Spring 2015 6559 Client-server1. Clients(a) Examples: ftp, telnet, ssh, browsers like Firefox.(b) Lecture 37, 5/1/2015(c) To find a server: use
CS485G Spring 2015 66(e) The client uses the host:port part to determine the addressand of the server and the port to connect to. It uses the servicep
CS485G Spring 2015 67unix> telnet mail.cs.uky.edu 25Trying 128.163.146.23...Connected to satchmo.cs.uky.edu.Escape character is ’ˆ]’.220 satchmo.cs
CS485G Spring 2015 76. The operating system allocates physical space, which also looks likean array ranging from address 0 to a limit determined by ho
CS485G Spring 2015 86. A buffer is also an array of bytes, typically used to hold data sub-ject to I/O. The bytes hold arbitrary binary values, not ne
CS485G Spring 2015 97. One can use the Boolean operators in C and apply them to any inte-gral data type: char, short, int, long, long long.8. Don’t co
Komentarze do niniejszej Instrukcji