CS , Fall 2001 Exam 1

Similar documents
CS , Fall 2002 Exam 1

Full Name: CISC 360, Fall 2008 Example of Exam

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

EECS 213 Fall 2007 Midterm Exam

CS , Spring 2004 Exam 1

CS , Fall 2004 Exam 1

Datorarkitektur, 2009 Tentamen

CS 201 Winter 2014 (Karavanic) Final Exam

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

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

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Assembly I: Basic Operations. Jo, Heeseung

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

CS 3843 Final Exam Fall 2012

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

15-213, Fall 2007 Midterm Exam

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Introduction to Computer Systems. Exam 1. February 22, This is an open-book exam. Notes are permitted, but not computers.

The course that gives CMU its Zip! Floating Point Arithmetic Feb 17, 2000

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

Introduction to Computer Systems. Exam 1. February 22, Model Solution fp

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

Machine-Level Programming I: Introduction Jan. 30, 2001

CS , Fall 2007 Exam 1

UW CSE 351, Winter 2013 Midterm Exam

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Machine Programming 1: Introduction

Computer Systems Organization V Fall 2009

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

CISC 360. Machine-Level Programming I: Introduction Sept. 18, 2008

CS , Fall 2009 Exam 1

Sungkyunkwan University

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

Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday

Buffer Overflows. Buffer Overflow. Many of the following slides are based on those from

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Chapter 3 Machine-Level Programming II Control Flow

CMSC 313 Lecture 12 [draft] How C functions pass parameters

Machine Programming 4: Structured Data

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

Instruction Set Architecture

Instruction Set Architecture

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

Machine-Level Programming V: Miscellaneous Topics Sept. 24, 2002

Giving credit where credit is due

15-213, Fall 2007 Final Exam

CS241 Computer Organization Spring 2015 IA

Instruction Set Architecture

CISC 360 Instruction Set Architecture

Machine-Level Programming Introduction

Page # CISC 360. Machine-Level Programming I: Introduction Sept. 18, IA32 Processors. X86 Evolution: Programmerʼs View.

Process Layout, Function Calls, and the Heap

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, 2002

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

Final exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Dec 20, Student's name: Student ID:

Machine-Level Programming IV: Structured Data

CS , Fall 2009 Exam 1

CS61, Fall 2012 Midterm Review Section

CS / ECE , Spring 2010 Exam 1

CS241 Computer Organization Spring Addresses & Pointers

! Starting in 1978 with ! Added more features as time goes on. ! Still support old features, although obsolete

Machine Programming 3: Procedures

CS , Spring 2002 Exam 2

Introduction to Computer Systems , fall th Lecture, Sep. 28 th

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

Machine-level Programming (3)

Process Layout and Function Calls

Assembly III: Procedures. Jo, Heeseung

IA32 Processors The course that gives CMU its Zip! Machine-Level Programming I: Introduction Sept. 10, X86 Evolution: Programmer s View

ASSEMBLY III: PROCEDURES. Jo, Heeseung

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Systems I. Machine-Level Programming I: Introduction

Sample Exam I PAC II ANSWERS

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Machine-Level Programming II: Control and Arithmetic

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

Machine-Level Programming III: Procedures

CIS 2107 Computer Systems and Low-Level Programming Spring 2012 Final

CSE 351 Midterm - Winter 2015 Solutions

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

CS213. Machine-Level Programming III: Procedures

The Hardware/Software Interface CSE351 Spring 2015

CSE 351 Midterm - Winter 2017

System Programming and Computer Architecture (Fall 2009)

Handout #2: Machine-Level Programs

CS/ECE 354 Practice Midterm Exam Solutions Spring 2016

Linux Memory Layout The course that gives CMU its Zip! Machine-Level Programming IV: Miscellaneous Topics Sept. 24, Text & Stack Example

CS241 Computer Organization Spring Loops & Arrays

COMP 210 Example Question Exam 2 (Solutions at the bottom)

CPEG421/621 Tutorial

Machine-Level Programming Introduction

Assembly Language Programming - III

THEORY OF COMPILATION

X86 Assembly -Data II:1

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

Transcription:

Andrew login ID: Full Name: CS 15-213, Fall 2001 Exam 1 October 9, 2001 Instructions: Make sure that your exam is not missing any sheets, then write your full name and Andrew login ID on the front. Write your answers in the space provided below the problem. If you make a mess, clearly indicate your final answer. The exam has a maximum score of 64 points. The problems are of varying difficulty. The point value of each problem is indicated. Pile up the easy points quickly and then come back to the harder problems. This exam is OPEN BOOK. You may use any books or notes you like. You may use a calculator, but no laptops or other wireless devices. Good luck! 1 (09): 2 (12): 3 (03): 4 (03): 5 (08): 6 (08): 7 (09): 8 (12): TOTAL (64): Page 1 of 11

Problem 1. (9 points): Assume we are running code on a -bit machine using two s complement arithmetic for signed integers. A short integer is encoded using bits. Fill in the empty boxes in the table below. The following definitions are used in the table: short sy = -3; int y = sy; int x = -17; unsigned ux = x; Note: You need not fill in entries marked with. Expression Decimal Representation Binary Representation Zero 01 0010 TMax TMin TMin TMin Page 2 of 11

Problem 2. (12 points): Consider the following -bit floating point representation based on the IEEE floating point format: There is a sign bit in the most significant bit. The next 3 bits are the exponent. The exponent bias is. The last 4 bits are the fraction. The representation encodes numbers of the form: and is the biased exponent., where is the significand The rules are like those in the IEEE standard(normalized, denormalized, representation of, infinity, and NAN). FILL in the table below. Here are the instructions for each field: Binary: The 8 bit binary representation. M: The value of the significand. This should be a number of the form or, where is an integer, and is an integral power of. Examples include,. E: The integer value of the exponent. Value:The numeric value represented. Note: you need not fill in entries marked with. Description Binary Value Minus zero 0 100 0101 Smallest denormalized (negative) Largest normalized (positive) One 5.5 Positive infinity Page 3 of 11

Problem 3. (3 points): Consider the following C functions and assembly code: int fun7(int a) return a * 30; int fun8(int a) return a * 34; int fun9(int a) return a * 18; Which of the functions compiled into the assembly code shown? pushl %ebp movl %esp,%ebp movl 8(%ebp),%eax sall $4,%eax addl 8(%ebp),%eax addl %eax,%eax movl %ebp,%esp popl %ebp ret Problem 4. (3 points): Consider the following C functions and assembly code: int fun4(int *ap, int *bp) int a = *ap; int b = *bp; return a+b; int fun5(int *ap, int *bp) int b = *bp; *bp += *ap; return b; int fun6(int *ap, int *bp) int a = *ap; *bp += *ap; return a; Which of the functions compiled into the assembly code shown? pushl %ebp movl %esp,%ebp movl 8(%ebp),%edx movl 12(%ebp),%eax movl %ebp,%esp movl (%edx),%edx addl %edx,(%eax) movl %edx,%eax popl %ebp ret Page 4 of 11

Problem 5. (8 points): Consider the source code below, where M and N are constants declared with #define. int array1[m][n]; int array2[n][m]; int copy(int i, int j) array1[i][j] = array2[j][i]; Suppose the above code generates the following assembly code: copy: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp),%ecx movl 12(%ebp),%ebx leal (%ecx,%ecx,8),%edx sall $2,%edx movl %ebx,%eax sall $4,%eax subl %ebx,%eax sall $2,%eax movl array2(%eax,%ecx,4),%eax movl %eax,array1(%edx,%ebx,4) popl %ebx movl %ebp,%esp popl %ebp ret What are the values of M and N? M = N = Page 5 of 11

Problem 6. (8 points): Consider the following assembly representation of a function foo containing a for loop: foo: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp),%ebx leal 2(%ebx),%edx xorl %ecx,%ecx cmpl %ebx,%ecx jge.l4.l6: leal 5(%ecx,%edx),%edx leal 3(%ecx),%eax imull %eax,%edx incl %ecx cmpl %ebx,%ecx jl.l6.l4: movl %edx,%eax popl %ebx movl %ebp,%esp popl %ebp ret Fill in the blanks to provide the functionality of the loop: int foo(int a) int i; int result = ; for( ; ; i++ ) ; ; return result; Page 6 of 11

Problem 7. (9 points): Consider the following C declarations: typedef struct short code; long start; char raw[3]; double data; OldSensorData; typedef struct short code; short start; char raw[5]; short sense; short ext; double data; NewSensorData; A. Using the templates below (allowing a maximum of 24 bytes), indicate the allocation of data for structs of type OldSensorData NewSensorData. Mark off and label the areas for each individual element (arrays may be labeled as a single element). Cross hatch the parts that are allocated, but not used (to satisfy alignment). Assume the Linux alignment rules discussed in class. Clearly indicate the right hand boundary of the data structure with a vertical line. OldSensorData: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ NewSensorData: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ Page 7 of 11

B. Now consider the following C code fragment: void foo(oldsensordata *olddata) NewSensorData *newdata; /* this zeros out all the space allocated for olddata */ bzero((void *)olddata, sizeof(olddata)); olddata->code = 0x104f; olddata->start = 0x80501ab8; olddata->raw[0] = 0xe1; olddata->raw[1] = 0xe2; olddata->raw[2] = 0x8f; olddata->raw[-5] = 0xff; olddata->data = 1.5; newdata = (NewSensorData *) olddata;... Once this code has run, we begin to access the elements of newdata. Below, give the value of each element of newdata that is listed. Assume that this code is run on a Little-Endian machine such as a Linux/x86 machine. You must give your answer in hexadecimal format. Be careful about byte ordering!. (a) newdata->start = 0x (b) newdata->raw[0] = 0x (c) newdata->raw[2] = 0x (d) newdata->raw[4] = 0x (e) newdata->sense = 0x Page 8 of 11

The next problem concerns the following C code. This program reads a string on standard input and prints an integer in hexadecimal format based on the input string it read. #include <stdio.h> /* Read a string from stdin into buf */ int evil_read_string() int buf[2]; scanf("%s",buf); return buf[1]; int main() printf("0x%x\n", evil_read_string()); Here is the corresponding machine code on a Linux/x86 machine: 08048414 <evil_read_string>: 8048414: 55 push %ebp 8048415: 89 e5 mov %esp,%ebp 8048417: 83 ec 14 sub $0x14,%esp 804841a: 53 push %ebx 804841b: 83 c4 f8 add $0xfffffff8,%esp 804841e: 8d 5d f8 lea 0xfffffff8(%ebp),%ebx 8048421: 53 push %ebx address arg for scanf 8048422: 68 b8 84 04 08 push $0x80484b8 format string for scanf 8048427: e8 e0 fe ff ff call 804830c <_init+0x50> call scanf 804842c: 8b 43 04 mov 0x4(%ebx),%eax 804842f: 8b 5d e8 mov 0xffffffe8(%ebp),%ebx 8048432: 89 ec mov %ebp,%esp 8048434: 5d pop %ebp 8048435: c3 ret 08048438 <main>: 8048438: 55 push %ebp 8048439: 89 e5 mov %esp,%ebp 804843b: 83 ec 08 sub $0x8,%esp 804843e: 83 c4 f8 add $0xfffffff8,%esp 8048441: e8 ce ff ff ff call 8048414 <evil_read_string> 8048446: 50 push %eax integer arg for printf 8048447: 68 bb 84 04 08 push $0x80484bb format string for printf 804844c: e8 eb fe ff ff call 804833c <_init+0x80> call printf 8048451: 89 ec mov %ebp,%esp 8048453: 5d pop %ebp 8048454: c3 ret Page 9 of 11

Problem 8. (12 points): This problem tests your understanding of the stack discipline and byte ordering. Here are some notes to help you work the problem: scanf("%s", buf) reads an input string from the standard input stream (stdin) and stores it at address buf (including the terminating \0 character). It does not check the size of the destination buffer. printf("0x%x", i) prints the integer i in hexadecimal format preceded by 0x. Recall that Linux/x86 machines are Little Endian. You will need to know the hex values of the following characters: Character Hex value Character Hex value d 0x64 v 0x76 r 0x72 i 0x69. 0x2e l 0x6c e 0x65 \0 0x00 s 0x73 A. Suppose we run this program on a Linux/x86 machine, and give it the string dr.evil as input on stdin. Here is a template for the stack, showing the locations of buf[0] and buf[1]. Fill in the value of buf[1] (in hexadecimal) and indicate where ebp points just after scanf returns to evil read string. <- buf[0]-> <-buf[1] -> +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ What is the 4-byte integer (in hex) printed by the printf inside main? 0x Page 10 of 11

B. Suppose now we give it the input dr.evil.lives (again on a Linux/x86 machine). (a) List the contents of the following memory locations just after scanf returns to evil read string. Each answer should be an unsigned 4-byte integer expressed as 8 hex digits. buf[0] = 0x buf[3] = 0x (b) Immediately before the ret instruction at address 0x08048435 executes, what is the value of the frame pointer register %ebp? %ebp = 0x You can use the following template of the stack as scratch space. Note: this does not have to be filled out to receive full credit. <- buf[0] -><- buf[1] -> --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-- --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-- Page 11 of 11