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

Similar documents
CS , Fall 2001 Exam 1

EECS 213 Fall 2007 Midterm Exam

CS , Fall 2002 Exam 1

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

CS 201 Winter 2014 (Karavanic) Final Exam

Process Layout and Function Calls

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

CS , Fall 2004 Exam 1

Assembly I: Basic Operations. Jo, Heeseung

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

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

UW CSE 351, Winter 2013 Midterm Exam

Process Layout, Function Calls, and the Heap

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

Machine-level Programming (3)

Machine Programming 1: Introduction

CS , Spring 2004 Exam 1

Assembly III: Procedures. Jo, Heeseung

ASSEMBLY III: PROCEDURES. Jo, Heeseung

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

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

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

Machine-Level Programming II: Control and Arithmetic

CS241 Computer Organization Spring Addresses & Pointers

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

Machine-Level Programming III: Procedures

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

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

Chapter 3 Machine-Level Programming II Control Flow

Sungkyunkwan University

CS213. Machine-Level Programming III: Procedures

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

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

Machine-Level Programming II: Arithmetic & Control. Complete Memory Addressing Modes

Machine Programming 3: Procedures

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

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

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

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

Sungkyunkwan University

Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012

Computer Systems Organization V Fall 2009

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

Credits to Randy Bryant & Dave O Hallaron

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

Instruction Set Architecture

CISC 360 Instruction Set Architecture

Datorarkitektur, 2009 Tentamen

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

Machine-Level Programming Introduction

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

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

Instruction Set Architecture

Instruction Set Architecture

CPSC W Term 2 Problem Set #3 - Solution

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

Second Part of the Course

CPEG421/621 Tutorial

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

SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C

Computer Systems CEN591(502) Fall 2011

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

CS241 Computer Organization Spring 2015 IA

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

The Hardware/Software Interface CSE351 Spring 2013

Machine Level Programming II: Arithmetic &Control

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

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

You may work with a partner on this quiz; both of you must submit your answers.

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

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

Handout #2: Machine-Level Programs

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

Control flow. Condition codes Conditional and unconditional jumps Loops Switch statements

Full Name: CISC 360, Fall 2008 Example of Exam

Machine-Level Programming Introduction

X86 Assembly -Procedure II:1

The Hardware/Software Interface CSE351 Spring 2015

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

CS241 Computer Organization Spring Loops & Arrays

Instructor: Alvin R. Lebeck

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

CS/ECE 354 Practice Midterm Exam Solutions Spring 2016

Assembly Language: Function Calls

X86 Assembly -Data II:1

CS 3843 Final Exam Fall 2012

An Experience Like No Other. Stack Discipline Aug. 30, 2006

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

Machine-Level Programming II: Control Flow

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

Assembly Language: Function Calls" Goals of this Lecture"

Ge-ng at things on the chip you can t easily reach from C

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

University of Washington

Machine- Level Programming III: Switch Statements and IA32 Procedures

Machine- Level Programming III: Switch Statements and IA32 Procedures

Transcription:

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009 Name Score out of 70 UMBC Username Notes: a. Please write clearly. Unreadable answers receive no credit. b. For TRUE/FALSE questions, write the word TRUE or the word FALSE. T and F will result in a 2-point deduction. c. There are no intentional syntax errors in any code provided with this exam. If you think you see an error that would affect your answer, please bring it to my attention.

TRUE/FALSE - 2 points each. Write the word TRUE or the word FALSE in each blank 1. All switch statements are implemented with a jump table. 2. None of the IA32 instructions differentiate between sign and unsigned integer types. 3. When a function is executing, its stack frame is always the same size. 4. The last thing a function caller must do before executing its call instruction is to save its registers on the stack. 5. The first thing a function that is called must do is save the caller's frame pointer on the stack. 6. Given the declaration short S[10][6], the memory address of S[r][c] can be calculated as S + 12*(r-1) + 2*(c-1) 7. All members of a union have the same byte offset relative to the start of the union. 8. The size of a function's stack frame can change while the function is executing. 9. The instruction pointer (%eip) contains the address of the next instruction to be executed. 10. The C compiler will check for an invalid array index if the size of the array is known at compiler time.

11. (10 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 %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 What are the values of M and N? M N

12. (6 points) Consider the following code fragment containing the incomplete definition of a data type matrix_entry with 4 fields. struct matrix_entry ; a; b; int c; d; struct matrix_entry matrix[2][5]; int urn_entry_c (int i, int j) urn matrix[i][j].c; Complete the above definition of matrix_entry so that the following assembly code could be generated from it on a Linux/x86 machine: urn_entry_c: movl 8(%ebp),%eax leal (%eax,%eax,4),%eax addl 12(%ebp),%eax sall $4,%eax movl matrix+4(%eax),%eax Note that there are multiple correct answers. Choose your answers from the following types, assuming the following sizes and alignments TYPE SIZE ALIGNMENT char 1 1 short 2 2 int 4 4 double 8 4

13. (10 points) Consider the following assembly representation of a function foo containing a for loop: foo: 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 Fill in the blanks to provide the functionality of the loop: int foo(int a) int i; int result = ; for( ; ; i++ ) ; ; urn result;

14. (6 points) Match each of the assembler routines on the left with the equivalent C function on the right. Write the name of the function in the box in the table below foo1: movl 8(%ebp),%eax sall $4,%eax subl 8(%ebp),%eax foo2: movl 8(%ebp),%eax testl %eax,%eax jge.l4 addl $15,%eax.L4: sarl $4,%eax foo3: movl 8(%ebp),%eax shrl $31,%eax Assembly Code int choice1(int x) urn (x < 0); int choice2(int x) urn (x << 31) & 1; int choice3(int x) urn 15 * x; int choice4(int x) urn (x + 15) /4 int choice5(int x) urn x / 16; int choice6(int x) urn (x >> 31); Function foo1 foo2 foo3

15. (10 points): 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 values of the following characters: Character Hex Value Character Hex Value 'a' 0x61 'f' 0x66 'b' 0x62 'g' 0x67 'c' 0x63 'h' 0x68 'd' 0x64 'i' 0x69 'e' 0x65 '\0' 0x00 /* copy string x to buf */ void foo(char *x) int buf[1]; strcpy((char *)buf, x); void callfoo() foo("abcdefghi"); _ Here is the corresponding machine code on a Linux/x86 machine: 080484f4 <foo>: 080484f4: 55 080484f5: 89 e5 080484f7: 83 ec 18 subl $0x18,%esp 080484fa: 8b 45 08 movl 0x8(%ebp),%eax 080484fd: 83 c4 f8 addl $0xfffffff8,%esp 08048500: 50 pushl %eax 08048501: 8d 45 fc leal 0xfffffffc(%ebp),%eax 08048504: 50 pushl %eax 08048505: e8 ba fe ff ff call 80483c4 <strcpy> 0804850a: 89 ec 0804850c: 5d 0804850d: c3 08048510 <callfoo>: 08048510: 55 08048511: 89 e5 08048513: 83 ec 08 subl $0x8,%esp 08048516: 83 c4 f4 addl $0xfffffff4,%esp 08048519: 68 9c 85 04 08 pushl $0x804859c # push string address 0804851e: e8 d1 ff ff ff call 80484f4 <foo> 08048523: 89 ec 08048525: 5d 08048526: c3 Now consider what happens on a Linux/x86 machine when callfoo calls foo with the input string abcdefghi. a. List the contents of the following memory locations immediately after strcpy urns to foo. Each answer should be an unsigned 4-byte integer expressed as 8 hex digits. buf[0] = 0x buf[1] = 0x buf[2] = 0x

b. Immediately before the instruction at address 0x0804850d executes, what is the value of the frame pointer register %ebp? %ebp = 0x c. Immediately after the instruction at address 0x0804850d executes, what is the value of the program counter register %eip? %eip = 0x

16. (4 points) Consider the following C functions on the left and the assembly code on the right int fun1(int a, int b) if (a < b) urn a; else urn b; int fun2(int a, int b) if (b < a) urn b; else urn a; int fun3(int a, int b) unsigned ua = (unsigned) a; if (ua < b) urn b; else urn ua; movl 8(%ebp),%edx movl 12(%ebp),%eax cmpl %eax,%edx jge.l9 movl %edx,%eax.l9: Which of the functions above on the left generated the assembly language above on the right? Write the name of the function in the box on the right.

17. (4 points) Consider the following C functions and assembly code: int fun4(int *ap, int *bp) int a = *ap; int b = *bp; urn a+b; int fun5(int *ap, int *bp) int b = *bp; *bp += *ap; urn b; int fun6(int *ap, int *bp) int a = *ap; *bp += *ap; movl 8(%ebp),%edx movl 12(%ebp),%eax movl (%edx),%edx addl %edx,(%eax) movl %edx,%eax urn a; Which of the functions above on the left generated the assembly language above on the right? Write the name of the function in the box.