CS-220 Spring 2018 Final Exam Version Practice May 10, Name:

Similar documents
CS-220 Spring 2018 Test 2 Version Practice Apr. 23, Name:

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

Download the tarball for this session. It will include the following files:

CSE 351 Midterm - Winter 2015 Solutions

CSE 351 Midterm - Winter 2015

Download the tarball for this session. It will include the following files:

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor

1 Number Representation(10 points)

CSCI 2021: x86-64 Control Flow

15-213/18-243, Spring 2011 Exam 1

CS356: Discussion #8 Buffer-Overflow Attacks. Marco Paolieri

Lab 7 Linux Debugging. EECS 448: Software Engineering I Mark Calnon October 17, 2011

CS356: Discussion #7 Buffer Overflows. Marco Paolieri

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

Computer Systems C S Cynthia Lee

CSE 351 Midterm Exam

CSE 351 Spring 2017 Midterm Exam (8 May 2017)

Do not turn the page until 5:10.

15-213/18-243, Fall 2010 Exam 1 - Version A

CS 107 Lecture 10: Assembly Part I

Question 1: Number Representation

University of Washington

Buffer Overflow Attack (AskCypert CLaaS)

How Software Executes

Computer Systems C S Cynthia Lee

18-600: Recitation #4 Exploits

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-64 assembly code is produced:

CSE 351 Midterm - Winter 2017

Procedures and the Call Stack

15-213/18-213, Fall 2011 Exam 1

CS , Fall 2007 Exam 1

CS377P Programming for Performance Single Thread Performance In-order Pipelines

CS377P Programming for Performance Leveraging the Compiler for Performance

Machine/Assembler Language Putting It All Together

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Buffer Overflow. An Introduction

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Areas for growth: I love feedback

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

Generation. representation to the machine

CSE 351 Midterm Exam Spring 2016 May 2, 2015

Do not turn the page until 5:10.

Do not turn the page until 12:30.

Do not turn the page until 5:10.

Machine Program: Procedure. Zhaoguo Wang

18-600: Recitation #4 Exploits (Attack Lab)

Machine Language CS 3330 Samira Khan

University of Washington Computer Science & Engineering Winter 2018 Instructor: Mark Wyse March 14, CSE 351 Final Exam. Last Name: First Name:

Binghamton University. CS-220 Spring x86 Assembler. Computer Systems: Sections

Changelog. Assembly (part 1) logistics note: lab due times. last time: C hodgepodge

15-213, Fall 2007 Midterm Exam

Corrections made in this version not seen in first lecture:

Question F5: Caching [10 pts]

How Software Executes

Do not turn the page until 5:10.

CS 33. Linkers. CS33 Intro to Computer Systems XXV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.


CSE351 Spring 2018, Midterm Exam April 27, 2018

Do not turn the page until 12:30.

CS 107. Lecture 13: Assembly Part III. Friday, November 10, Stack "bottom".. Earlier Frames. Frame for calling function P. Increasing address

CSCI 356 Fall 2017 : Practice Exam I DO NOT OPEN EXAM PACKET UNTIL INSTRUCTED TO DO SO YOU MAY FILL IN INFORMATION ON THE FRONT NOW

Machine Programming 3: Procedures

Do not turn the page until 11:30.

CS-220 Spring 2018 Test 1 Version A Feb. 28, Name:

CS , Spring 2002 Exam 2

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

Do not turn the page until 12:30.

University of Washington Computer Science & Engineering Winter 2018 Instructor: Mark Wyse March 14, CSE 351 Final Exam. Last Name: First Name:

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

Changelog. Brief Assembly Refresher. a logistics note. last time

UW CSE 351, Winter 2013 Midterm Exam

15-213/18-243, Summer 2011 Exam 1 Tuesday, June 28, 2011

L14: Structs and Alignment. Structs and Alignment. CSE 351 Spring Instructor: Ruth Anderson

CS356: Discussion #15 Review for Final Exam. Marco Paolieri Illustrations from CS:APP3e textbook

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

Processor state. Information about currently executing program. %rax, %rdi, ... %r14 %r15. %rsp. %rip CF ZF SF OF. Temporary data

Functions. Ray Seyfarth. August 4, Bit Intel Assembly Language c 2011 Ray Seyfarth

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code.

CS527 Software Security

Do not turn the page until 12:30.

Introduction Presentation A

Winter 2017 CS107 Midterm #2 Examination (Practice #2) Problem Points Score Grader TOTAL 30

Representation of Information

Machine-Level Programming V: Unions and Memory layout

CPS104 Recitation: Assembly Programming

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

Instruction Set Architectures

Review addressing modes

Instructions and Instruction Set Architecture

void P() {... y = Q(x); print(y); return; } ... int Q(int t) { int v[10];... return v[t]; } Computer Systems: A Programmer s Perspective

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Buffer overflows (a security interlude) Address space layout the stack discipline + C's lack of bounds-checking HUGE PROBLEM

Brief Assembly Refresher

Question Points Score Total: 100

CS Bootcamp x86-64 Autumn 2015

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

x86-64 Programming II

Machine-Level Programming V: Advanced Topics

Transcription:

CS-220 Spring 2018 Final Exam Version Practice May 10, 2018 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : One of the advantages of using the make command is that if the Makefile is written correctly, make guarantees that all of the pre-requisite steps in a build process are completed before executing a final step. (b) T F : The UNIX command./mypgm <test.txt >msgs.txt connects the standard input stream to file test.txt, and connects the standard error stream to msgs.txt. (c) T F : In an assignment statement, C will convert the value of the expression on the right hand side of the equals sign to the type of the left hand side, even if this causes the value of the expression to change. (d) T F : One of the most powerful concepts of the C programming language is the ability to treat a complex function as a simple abstract mechanism which reads arguments, and returns a value. (e) T F : When you invoke a C function, the values of the argument expressions are copied to the parameters of the function. (f) T F : An if/else statement is an example of sequential control flow in the C language. (g) T F : The same bits in memory are always interpreted to be the same value, no matter what type is associated with those bits. (h) T F : It is important that an older version of an ISA support everyting in a newer version of the same ISA so that new software can be run on both old and new hardware. (i) T F : In X86-64, the stack grows by making the value in the %rsp register smaller, which moves the push/pop end of the stack towards a lower address in memory. (j) T F : One of the disadvantages of a direct map cache is that it is difficult to find a victim cache line when there is a cache miss. For the following questions, check the best answer. 2. (5 points) A typedef statement is useful in C for all of the following except: Making it clear to the programmer that a parameter to a function is a special kind of (for instance) integer. Simplifying the declaration of variables of a complicated type. Modifying the behavior of the C operators such as + or *. Enable library code to change the type of a class of variables with a single edit and recompile. Hiding the details of a complicated type to allow a programmer to deal with that type as an abstraction. Page 1 of 10

3. (5 points) What will get printed by the following code snippet? int x ; int sum=0; for ( x=0;x<10;x++) { int x= 2; sum +=x ; p r i n t f ( x=%d sum=%d\n, x, sum ) ; x=11 sum=52 Compiler Error x=-2 sum=-20 x=10 sum=-20 x=-2 sum=52 4. (5 points) If a floating point number is represented by the bits 0x8000003f, then the floating point value of that number is: Less than -2 to the -127 Greater than -2 to the -127, but less than 0 Zero Greater than zero, but less than 2 to the -127. Greater than 2 to the -127 5. (5 points) The assignment on line 13 of Listing 1 on page 7 is implement by instructions at what range of offsets in Listing 2 on page 8? (Hint, a question mark has an ASCII code of 0x3f.) 701-705 72a-72e 731-735 759-75c 76f-773 None of the above 6. (5 points) The for statement on line 27 of Listing 1 on page 7 is implement by instructions at which ranges of offsets in Listing 3 on page 8? Check all that apply. (Hint, a lowercase a has an ASCII code of 0x61, and z has a code of 0x7a.) 7ba-7be 7c5-7c9 7cb-7cf 7fe-805 808-80c None of the above 7. (5 points) In Listing 4 on page 9, the countletters function creates its own stack frame in instructions at 814 and 815, but does not subtract from %rsp to create extra room for local variables, even though it uses memory at locations like -0x18(%rbp) and -0x1c(%rbp). Check all of the reasons below why this is valid. The operating system and all other processes are not allowed to change memory in the red zone. Data in the heap (obtained by malloc) cannot overlap with the stack area in memory. The topletter function never pushes or pops on the stack. The countletters function does not call any lower level functions. It is a leaf function, and therefore no function creates a stack frame below countletter s stack frame. If this were not valid, it would cause a segmentation violation, and the compiler never allows code to be generated that would cause a segmentation violation. None of the above Page 2 of 10

8. (5 points) Figure 1 on page 10 is a snapshot of the stack memory when running the code in Listing 1 on page 7. Using this stack information and the x86 assembler code in Listing 2 on page 8, Listing 3 on page 8 and Listing 4 on page 9, check all of the true statements below. The current stack frame contains a single eight byte word that points to its caller s %rbp. By looking at the address at %rbp+8, we can find the return address for the current function. The caller of the current function is the main function. The caller of the current function is the topletter function. The call to the current function was generated from line 28 in Listing 1 on page 7. The call to the current function was generated from line 30 in Listing 1 on page 7. 9. (5 points) Check all of the true statements below that represent benefits of using cache memory. Computers with caches are cheaper than computers without caches. Computers with caches are faster that computers without caches because the cache almost always supplies data to the ALU at high speed. Caches make understanding how the insides of a computer work much simpler. Caches allow an abstract view of memory as a large indexable array of bytes, even though the concrete implementation of memory is more complicated. Even though caches almost always make computers run much faster, there are some programs that cause a low cache hit rate, and run very slowly on a computer with cache. There is only one way to implement cache - using the set bits in an address to index into a single cache line. 10. (5 points) An ELF executable file is used to load binary data into memory. Check all the true statements below about which sections of an ELF executable file are loaded into memory. The.code section which contains the man-readable X86 assembler code for the program. The.text section, which contains the binary machine instructions to execute. The.wodata section which contains all the variables in the program that can be written, but not read from memory. The.data section which contains initial values for all initialize global variables in the program. The.scope section which contains information about the scope of each variable in the code. The.rodata section which contains program data that cannot change, such as constants and literal data. Page 3 of 10

Answer the following questions by filling in the blanks. 11. (5 points) What is does the following code snippet print to stdout? int mat [ 2 ] [ 3 ] [ 4 ] ; int s l i c e ; int row ; int c o l ; for ( s l i c e =0; s l i c e <2; s l i c e ++) { for ( row=0;row <3;row++) { for ( c o l =0; col <4; c o l++) { mat [ s l i c e ] [ row ] [ c o l ] = s l i c e 20 + row 5 + c o l ; p r i n t f ( mat [ 1 ] [ 2 ] [ 3 ] = % d\n, mat [ 1 ] [ 2 ] [ 3 ] ) ; 12. Given the following snippet of code: int i ; for ( i =0; i <100; i ++) { i f (0== i %2) continue ; i f (0== i %4) p r i n t f ( You won $100! \ n ) ; i f ( i >15) break ; p r i n t f ( The f i n a l value o f i i s %d\n, i ) ; (a) (2 points) Will the code snippet above print out You won $100!? (b) (3 points) The code snippet above will print out The final value of i is 13. (5 points) If the program in Listing 1 on page 7 is compiled int maxletter, and invoked as./maxletter testing good letters, what will get printed to stdout? 14. (5 points) Given the C program in Listing 1 on page 7, which the compiler translated to X86 assembler code in Listing 3 on page 8, and given the stack data from this program in figure 1 on page 10, what is the value of the maxusage variable in the stack frame associated with the topletter function? Page 4 of 10

15. And finally, an exercise in writing and reading code. Consider the following program: #include <s t d i o. h> #include <s t d l i b. h> #include <s t r i n g. h> #include <time. h> char randchar ( ) ; int main ( int argc, char argv ) { t i m e t t ; srand ( ( unsigned ) time(&t ) ) ; int n=a t o i ( argv [ 1 ] ) ; int cn =1; char c ; while (EOF!=( c=getchar ( ) ) ) { int i ; for ( i =0; i<cn ; i ++) putchar ( i ==0?c : randchar ( ) ) ; cn++; i f ( cn>n ) cn =1; return 0 ; char randchar ( ) { char l t r s []= abcdefghijklmnopqrstuvwxyz ; return l t r s [ rand ()% s t r l e n ( l t r s ) ] ; If this code is compiled as executable file encode, then the command echo "This is a test"./encode 3... would (possibly) write the following string to stdout... Thbilps jidgs rajx tceuyste (a) (25 points) Write a C program (including the main function) which takes a single number as the command line argument, and decodes the encoded result as specified by the encode function above. For instance, the command... echo "This is a test"./encode 3./decode 3... would write the string This is a test to stdout. Page 5 of 10

(b) (10 points (bonus)) If your code from the previous question works correctly, and is compiled as decode, what will get written to stdout as a result of the following command: echo "I taopcekdbr thhwpism ontedssxt" decode 3 Page 6 of 10

Tear-off Page Listing 1: maxletter.c 1 #include <s t d i o. h> 2 char t o p L e t t e r ( char s t r ) ; 3 int c o u n t L e t t e r s ( char s tr, char l e t ) ; 4 5 int main ( int argc, char argv ) { 6 char maxletter=? ; 7 char maxcount=0; 8 int maxarg=0; 9 int i ; 10 for ( i =1; i<argc ; i ++) { 11 char l e t t e r=t o p L e t t e r ( argv [ i ] ) ; 12 i f ( maxcount < c o u n t L e t t e r s ( argv [ i ], l e t t e r ) ) { 13 maxletter=l e t t e r ; 14 maxcount=c o u n t L e t t e r s ( argv [ i ], l e t t e r ) ; 15 maxarg=i ; 16 17 18 p r i n t f ( Arg : %s has l e t t e r %c used %d times \n, 19 argv [ maxarg ], maxletter, maxcount ) ; 20 return 0 ; 21 22 23 char t o p L e t t e r ( char s t r ) { 24 char l e t ; 25 char maxletter=? ; 26 int maxusage=0; 27 for ( l e t= a ; l e t <= z ; l e t ++) { 28 i f ( c o u n t L e t t e r s ( s t r, l e t ) > maxusage ) { 29 maxletter=l e t ; 30 maxusage=c o u n t L e t t e r s ( str, l e t ) ; 31 32 33 return maxletter ; 34 35 36 int c o u n t L e t t e r s ( char s tr, char l e t ) { 37 int count =0; 38 for ( ; ( s t r )!=0 x00 ; s t r++) i f ( ( s t r)== l e t ) count++; 39 return count ; 40 Page 7 of 10

Tear-off Page Listing 2: maxletter.objdump.txt(main) 00000000000006 b0 <main >: 6b0 : push %rbp 6b1 : mov %rsp,%rbp 6b4 : push %rbx 6b5 : sub $0x28,%rsp 6b9 : mov %edi, 0x24(%rbp) 6bc : mov %rsi, 0x30(%rbp) 6 c0 : movb $0x3f, 0 x11(%rbp) 6 c4 : movb $0x0, 0x12(%rbp) 6 c8 : movl $0x0, 0x18(%rbp) 6 c f : movl $0x1, 0 x1c(%rbp) 6d6 : jmpq 763 <main+0xb3> 6db : mov 0x1c(%rbp),%eax 6de : cltq 6 e0 : lea 0x0(,%rax,8),%rdx 6 e8 : mov 0x30(%rbp),%rax 6 ec : add %rdx,%rax 6 e f : mov (%rax),%rax 6 f 2 : mov %rax,%rdi 6 f 5 : callq 7 ae <topletter > 6 f a : mov %al, 0x1d(%rbp) 6 fd : movsbl 0x12(%rbp),%ebx 7 0 1 : movsbl 0x1d(%rbp),%edx 7 0 5 : mov 0x1c(%rbp),%eax 7 0 8 : cltq 70a : lea 0x0(,%rax,8),% rcx 7 1 2 : mov 0x30(%rbp),%rax 7 1 6 : add %rcx,%rax 7 1 9 : mov (%rax),%rax 71 c : mov %edx,% e s i 71 e : mov %rax,% rdi 7 2 1 : callq 814 <countletters > 7 2 6 : cmp %eax,%ebx 7 2 8 : jge 75 f <main+0xaf> 72 a : movzbl 0x1d(%rbp),%eax 72 e : mov %al, 0x11(%rbp) 7 3 1 : movsbl 0x1d(%rbp),%edx 7 3 5 : mov 0x1c(%rbp),%eax 7 3 8 : cltq 73a : lea 0x0(,%rax,8),% rcx 7 4 2 : mov 0x30(%rbp),%rax 7 4 6 : add %rcx,%rax 7 4 9 : mov (%rax),%rax 74 c : mov %edx,% e s i 74 e : mov %rax,% rdi 7 5 1 : callq 814 <countletters > 7 5 6 : mov %al, 0x12(%rbp) 7 5 9 : mov 0x1c(%rbp),%eax 75 c : mov %eax, 0 x18(%rbp) 75 f : addl $0x1, 0 x1c(%rbp) 7 6 3 : mov 0x1c(%rbp),%eax 7 6 6 : cmp 0x24(%rbp),%eax 7 6 9 : j l 6db <main+0x2b> 76 f : movsbl 0x12(%rbp),% ecx 7 7 3 : movsbl 0x11(%rbp),%edx 7 7 7 : mov 0x18(%rbp),%eax 77a : cltq 77 c : lea 0x0(,%rax,8),% r s i 7 8 4 : mov 0x30(%rbp),%rax 7 8 8 : add %rsi,%rax 78b : mov (%rax),%rax 78 e : mov %rax,% r s i 7 9 1 : lea 0 x140(%rip ),% rdi 7 9 8 : mov $0x0,%eax 79d : callq 560 <p r i n t f @ p l t > 7 a2 : mov $0x0,%eax 7 a7 : add $0x28,%rsp 7ab : pop %rbx 7 ac : pop %rbp 7ad : retq Listing 3: maxletter.objdump.txt(topletter) 00000000000007 ae <topletter >: 7 ae : push %rbp 7 a f : mov %rsp,%rbp 7b2 : sub $0x20,%rsp 7b6 : mov %rdi, 0x18(%rbp) 7ba : movb $0x3f, 0x2(%rbp) 7be : movl $0x0, 0x8(%rbp) 7 c5 : movb $0x61, 0x1(%rbp) 7 c9 : jmp 808 <t o p L e t t e r+0x5a> 7cb : movsbl 0x1(%rbp),%edx 7 c f : mov 0x18(%rbp),%rax 7d3 : mov %edx,% e s i 7d5 : mov %rax,% rdi 7d8 : callq 814 <countletters > 7dd : cmp 0x8(%rbp),%eax 7 e0 : j l e 7 f e <t o p L e t t e r+0x50> 7 e2 : movzbl 0x1(%rbp),%eax 7 e6 : mov %al, 0x2(%rbp) 7 e9 : movsbl 0x1(%rbp),%edx 7ed : mov 0x18(%rbp),%rax 7 f 1 : mov %edx,% e s i 7 f 3 : mov %rax,%rdi 7 f 6 : callq 814 <countletters > 7 fb : mov %eax, 0x8(%rbp) 7 f e : movzbl 0x1(%rbp),%eax 8 0 2 : add $0x1,%eax 8 0 5 : mov %al, 0x1(%rbp) 8 0 8 : cmpb $0x7a, 0x1(%rbp) 80 c : j l e 7cb <t o p L e t t e r+0x1d> 80 e : movzbl 0x2(%rbp),%eax 8 1 2 : leaveq 8 1 3 : retq Page 8 of 10

Tear-off Page Listing 4: maxletter.objdump.txt(countletters) 0000000000000814 <countletters >: 8 1 4 : push %rbp 8 1 5 : mov %rsp,%rbp 8 1 8 : mov %rdi, 0x18(%rbp) 81 c : mov %esi,%eax 81 e : mov %al, 0 x1c(%rbp) 8 2 1 : movl $0x0, 0x4(%rbp) 8 2 8 : jmp 83 f <c o u n t L e t t e r s+0x2b> 82a : mov 0x18(%rbp),%rax 82 e : movzbl (%rax),%eax 8 3 1 : cmp 0x1c(%rbp),% al 8 3 4 : jne 83a <c o u n t L e t t e r s+0x26> 8 3 6 : addl $0x1, 0x4(%rbp) 83a : addq $0x1, 0x18(%rbp) 83 f : mov 0x18(%rbp),%rax 8 4 3 : movzbl (%rax),%eax 8 4 6 : test %al,% al 8 4 8 : jne 82a <c o u n t L e t t e r s+0x16> 84a : mov 0x4(%rbp),%eax 84d : pop %rbp 84 e : retq Page 9 of 10

Tear-off Page Figure 1: Contents of maxletter Stack Memory Address Value Comments 64 bit, big-endian 0x7fffffffe870 0x00000008f7b9b2e8 0x7fffffffe868 0x00007fffffffe938 0x7fffffffe860 0x0000000000400000 0x7fffffffe858 0x00007ffff7a5a2b1 0x7fffffffe850 0x0000555555554850 0x7fffffffe848 0x0000000000000000 0x7fffffffe840 0x00007fffffffe930 0x7fffffffe838 0x6801555500000001 0x7fffffffe830 0x0000000461554850 0x7fffffffe828 0x0000000800000000 0x7fffffffe820 0x00007fffffffe938 0x7fffffffe818 0x00005555555546fa 0x7fffffffe810 0x00007fffffffe850 0x7fffffffe808 0x7474000100000001 0x7fffffffe800 0x00007fffffffe82e 0x7fffffffe7f8 0x00007fffffffec3c 0x7fffffffe7f0 0x00000061000000c2 0x7fffffffe7e8 0x00005555555547fb 0x7fffffffe7e0 0x00007fffffffe810 %rsp,%rbp 0x7fffffffe7d8 0x0000000200000000 0x7fffffffe7d0 0x0000000000000000 0x7fffffffe7c8 0x00007fffffffec3c 0x7fffffffe7c0 0x0000007400000000... t.... 0x7fffffffe7b8 0x0000000000000000 0x7fffffffe7b0 0x0000000000000000 0x7fffffffe7a8 0x0000000000000000 0x7fffffffe7a0 0x0000000000000000 Page 10 of 10