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

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

Process Layout and Function Calls

CS , Fall 2002 Exam 1

CS , Fall 2001 Exam 1

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

EECS 213 Fall 2007 Midterm Exam

Assembly I: Basic Operations. Jo, Heeseung

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

CS 201 Winter 2014 (Karavanic) Final Exam

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Machine Programming 1: Introduction

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 (3)

CS241 Computer Organization Spring Addresses & Pointers

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

Process Layout, Function Calls, and the Heap

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

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

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

CPSC W Term 2 Problem Set #3 - Solution

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

Machine-Level Programming II: Control and Arithmetic

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

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

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

Sungkyunkwan University

The Hardware/Software Interface CSE351 Spring 2013

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

Machine-Level Programming III: Procedures

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

CS241 Computer Organization Spring Loops & Arrays

Credits to Randy Bryant & Dave O Hallaron

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

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

CPEG421/621 Tutorial

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

SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C

Machine Level Programming II: Arithmetic &Control

CS213. Machine-Level Programming III: Procedures

Instruction Set Architecture

Arrays. Young W. Lim Mon. Young W. Lim Arrays Mon 1 / 17

Instruction Set Architecture

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

Machine-Level Programming II: Control Flow

CS241 Computer Organization Spring 2015 IA

Assembly III: Procedures. Jo, Heeseung

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Computer Systems CEN591(502) Fall 2011

Machine Programming 3: Procedures

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

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

Arrays. Young W. Lim Wed. Young W. Lim Arrays Wed 1 / 19

Data Representa/ons: IA32 + x86-64

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

Credits and Disclaimers

Handout #2: Machine-Level Programs

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

Second Part of the Course

Chapter 3 Machine-Level Programming II Control Flow

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

Instruction Set Architecture

CISC 360 Instruction Set Architecture

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

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

Machine-Level Programming Introduction

Machine-Level Programming Introduction

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

CS61 Section Solutions 3

System Programming and Computer Architecture (Fall 2009)

Sungkyunkwan University

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

Instruction Set Architecture

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

CS/ECE 354 Practice Midterm Exam Solutions Spring 2016

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

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

CIT Week13 Lecture

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

Assembly Language: Function Calls" Goals of this Lecture"

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

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

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

Machine Programming 2: Control flow

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

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

UW CSE 351, Winter 2013 Midterm Exam

Instructor: Alvin R. Lebeck

Computer Systems Organization V Fall 2009

Credits and Disclaimers

Datorarkitektur, 2009 Tentamen

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

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

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

X86 Assembly -Procedure II:1

Machine-Level Prog. IV - Structured Data

h"p://news.illinois.edu/news/12/0927transient_electronics_johnrogers.html

Machine Programming 4: Structured Data

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

Transcription:

CAS CS 210 - Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm This problem set is to be completed individually. Explain how you got to your answers by providing clear justification, which includes commenting on what each assembly instruction does and showing the contents of the stack. 1. Consider a C function with the following prototype: int foo(int x) urn ; The function urns an arithmetic-logic expression in x. No other variables appear in this expression. The function is compiled into IA32 assembly code. (a) Write C code for foo that will have an effect equivalent to the following assembly code: foo: movl 8(%ebp),%eax sall $4,%eax subl 8(%ebp),%eax Answer: (10 points) foo1 corresponds to : urn 15*x; You can test your solution by compiling your C code with the -m32 and -S flags. Your compiler may not generate identical assembly code, but it should be functionally equivalent. 1

(b) Write C code for foo that will have an effect equivalent to the following assembly code: foo:.l4: movl 8(%ebp),%eax testl %eax,%eax jge.l4 addl $15,%eax sarl $4,%eax Answer: (10 points) foo2 corresponds to : urn x / 16; 2

2. Consider the following C function with prototype: int fun(int *ap, int *bp) int a; ; ; urn ; Write C code for fun that will have an effect equivalent to the following IA32 assembly code: movl 8(%ebp),%edx movl 12(%ebp),%eax movl (%edx),%edx addl %edx,(%eax) movl %edx,%eax movl %ebp, %esp Your C code can only use one local integer variable a, in addition to the two arguments do not use register names. You can test your solution by compiling your C code with the -m32 and -S flags. Your compiler may not generate identical assembly code, but it should be functionally equivalent. Answer: (20 points) int fun(int *ap, int *bp) int a = *ap; *bp += *ap; urn a; 3

3. Consider the following IA2 assembly code for a procedure foo(): foo: movl 8(%ebp),%ecx movl 16(%ebp),%edx movl 12(%ebp),%eax decl %eax js.l3.l7: cmpl %edx,(%ecx,%eax,4) jne.l3 decl %eax jns.l7.l3: Based on the assembly code above, fill in the blanks below in its corresponding C source code. (Note: you may only use symbolic variables a, n, val, and i from the source code in your expressions below do not use register names.) int foo(int *a, int n, int val) int i; for (i = ; ; i = ) ; urn i; Answer: (20 points) for (i = n-1; (i >=0) && (a[i] == val); i = i-1) 4

4. Consider the source C code below, where M and N are constants declared with #define. int mat1[m][n]; int mat2[n][m]; void copy_element(int i, int j) mat1[i][j] = mat2[j][i]; This generates the following IA32 assembly code: copy_element: pushl %ebx movl 8(%ebp),%ecx movl 12(%ebp),%ebx movl %ecx,%edx leal (%ebx,%ebx,8),%eax sall $4,%edx sall $2,%eax subl %ecx,%edx movl mat2(%eax,%ecx,4),%eax sall $2,%edx movl %eax,mat1(%edx,%ebx,4) movl -4(%ebp),%ebx (a) What is the value of M? Answer: (10 points) M = 9. (b) What is the value of N? Answer: (10 points) N = 15. 5

5. Consider the following recursive C function: int silly(int n, int *p) int val, val2; if (n > 0) val2 = silly(n << 1, &val); else val = val2 = 0; *p = val + val2 + n; urn val + val2; This yields the following IA32 assembly code: silly:.l3:.l4: subl $20,%esp pushl %ebx movl 8(%ebp),%ebx testl %ebx,%ebx jle.l3 addl $-8,%esp leal -4(%ebp),%eax pushl %eax leal (%ebx,%ebx),%eax pushl %eax call silly jmp.l4.p2align 4,,7 xorl %eax,%eax movl %eax,-4(%ebp) movl -4(%ebp),%edx addl %eax,%edx movl 12(%ebp),%eax addl %edx,%ebx movl %ebx,(%eax) movl -24(%ebp),%ebx movl %edx,%eax 6

(a) Is the variable val stored on the stack? If so, at what byte offset (relative to %ebp) is it stored, and why is it necessary to store it on the stack? Answer: (5 points) Yes, -4, Need to pass pointer to it to recursive call to compute urn value val+val2. (b) Is the variable val2 stored on the stack? If so, at what byte offset (relative to %ebp) is it stored, and why is it necessary to store it on the stack? Answer: (5 points) No, val2 is kept in %eax as urn value of silly. (c) What (if anything) is stored at -24(%ebp)? If something is stored there, why is it necessary to store it? Answer: (5 points) The value of %ebx is saved here, because %ebx is a callee-save register. It represents n. (d) What (if anything) is stored at -8(%ebp)? If something is stored there, why is it necessary to store it? Answer: (5 points) Nothing is stored here. 7