Nature & Wildlife

CAS CS Computer Systems Spring PDF

CAS CS Computer Systems Spring 2016 Solution to Second Examination Closed Books and Notes March 31, 2016 YOUR Please write neatly and clearly. Make sure you have 5 questions over 8 pages. You have
of 8
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
CAS CS Computer Systems Spring 2016 Solution to Second Examination Closed Books and Notes March 31, 2016 YOUR Please write neatly and clearly. Make sure you have 5 questions over 8 pages. You have 75 minutes to answer all questions. You are allowed to only have a copy of the Intel x86-64 reference sheets. No calculators nor electronic devices are allowed. Good luck! Question Score #1 10 #2 10 #3 10 #4 10 #5 10 Total 50 1 2 1) Consider the following C functions and Intel x86-64 assembly code: int fun1(int a, int b) if (a b) urn a; else urn b; int fun2(int a, int b) if (a = b) urn b; else urn a; cmp %esi,%edi # edi - esi (a - b) jl.l5 # if (a b) urn a mov %esi,%eax # else urn b q.l5: mov %edi,%eax q int fun3(unsigned int a, unsigned int b) if (a b) urn a; else urn b; Which of the functions compiled into the assembly code shown? fun2 3 2) Consider the following Intel x86-64 assembly code for a C for loop: loop:.l3:.l2: movl $0, %eax jmp.l2 subl $1, %edi addl $1, %esi addl $1, %eax cmpl %esi, %edi jg.l3 addl $1, %eax Based on the assembly code above, fill in the blanks below in its corresponding C source code. (Note: you may only use the symbolic variables x, y, and result in your expressions below do not use register names.) (2 points for each completed expression) int loop(int x, int y) { int result; for (result = 0; x y; result++) { x--; y++; result++; urn result; 4 3) Consider the source code below, where M and N are constants declared with #define. int mat1[m][n]; int mat2[n][m]; int sum_element(int i, int j) { urn mat1[i][j] + mat2[i][j]; Suppose the above code generates the following Intel x86-64 assembly code: sum_element: movslq %esi, %rsi movslq %edi, %rdi leaq (%rsi,%rdi,8), %rdx # rdx = rsi+8*rdi = j + 8i subq %rdi, %rdx # rdx = rdx - rdi = j + 8i - i = j + 7i leaq (%rdi,%rdi,4), %rax # rax = rdi + rdi*4 = 5i addq %rax, %rsi # rsi = rsi + rax = j + 5i movl mat2(,%rsi,4), %eax # eax = mat2 + 4*rsi = mat2 + 4*(j + 5i) addl mat1(,%rdx,4), %eax # eax = eax + mat1 + 4*rdx = eax + mat1 + 4*(j + 7i) What are the values of M and N? (5 points for each) M = 5 N = 7 5 4) Consider the following C code: /* copy string x to buf */ void foo(char *x) { char buf[4]; strcpy(buf, x); void callfoo() { foo( ); Here is the corresponding machine code on a Linux/x86-64 machine: foo : : ec 18 sub $0x18,%rsp : fe mov %rdi,%rsi : e7 mov %rsp,%rdi 40053a: e8 d1 fe ff ff callq f: c4 18 add $0x18,%rsp : c3 q callfoo : : ec 08 sub $0x8,%rsp : bf mov $0x400600,%edi 40054d: e8 de ff ff ff callq foo : c4 08 add $0x8,%rsp : c3 q 6 4) (Continued) This problem tests your understanding of the stack discipline and byte ordering. Here are some notes to help you work the problem: strcpy(char *dst, char *src) copies the string at address src (including the terminating \0 character) to address dst. It does not check the size of the destination buffer. Recall that Linux/x86 machines are Little Endian. You will need to know the hex (ascii) values of the following characters: 0-9 are the contiguous values from 0x30 to 0x39 inclusively and null is 0x00. Now consider what happens on a Linux/x86 machine when callfoo calls foo with the input string (a) Immediately after the add instruction at address 0x f executes, what is the 8 byte value at the top of the stack? (3 points) VALUE AT THE TOP OF THE STACK = 0x0x [The low-order byte of the urn address to callfoo(), 0x , is overwritten by the null character since the input string is 25 bytes whereas the space allocated by foo() on the stack is 24 bytes.] (b) Immediately after the q instruction at address 0x executes, what is the value of the program counter register %rip? (3 points) %rip = 0x0x (c) What will a printf( buf = %s\n , buf); function print if it were inserted immediately after strcpy urns to foo. (4 points) buf (string): 7 5) Consider the following C declaration: struct Node{ char c; double value; struct Node* next; int flag; ; typedef struct Node* pnode; /* NodeList is an array of N pointers to Node structs */ pnode NodeList[N]; (a) Using the template below (allowing a maximum of 32 bytes), indicate the allocation of data for a Node struct. Mark off and label the areas for each individual element (there are 4 of them). Cross hatch the parts that are allocated, but not used (to satisfy alignment). And leave blank the parts that are not allocated. Assume the Linux alignment rules on an Intel x86-64 machine discussed in class. Clearly indicate the right hand boundary of the data structure with a vertical line. (4 points) Answer: c XXXXXXXXXXXXXXXXXXXXXXXXX d d d d d d d d p p p p p p p p i i i i XXXXXXXXXXXXXX c value next flag 8 5) (Continued) (b) For each of the two C references below, please indicate which assembly code section (labeled A D) places the value of that C reference into register %eax. Recall that if r is a pointer to a struct, the C reference r- e refers to the value of element e in that struct. The initial register-to-variable mapping for each assembly code section is: %edi = integer index i in C reference (3 points for each) =============================================== C References: 1. C NodeList[i]- flag 2. B NodeList[i]- next- next- flag ================================================ Linux/Intel x86-64 Assembly: A: movslq %edi, %rdi movq NodeList(,%rdi,8), %rax movq 16(%rax), %rax movl 24(%rax), %eax ============================ B: movslq %edi, %rdi movq NodeList(,%rdi,8), %rax movq 16(%rax), %rax movq 16(%rax), %rax movl 24(%rax), %eax # eax = M[M [ M[ M[NodeList+8*i] +16 ] + 16]+24] ============================ C: movslq %edi, %rdi movq NodeList(,%rdi,8), %rax movl 24(%rax), %eax # eax = M[ M[NodeList+8*i] + 24 ] ============================ D: movslq %edi, %rdi movq NodeList(,%rdi,8), %rax movq 16(%rax), %rax movsbl (%rax), %eax
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks