CPSC 213, Winter 2009, Term 2 Midterm Exam Date: March 12, 2010; Instructor: Mike Feeley

Similar documents
CPSC 213, Winter 2013, Term 2 Final Exam Date: April 14, 2014; Instructor: Mike Feeley

CPSC 213, Winter 2016, Term 2 Final Exam Date: April 19, 2017; Instructor: Mike Feeley and Alan Wagner

CPSC 213, Summer 2017, Term 2 Midterm Exam Date: July 27, 2017; Instructor: Anthony Estey

CPSC 213. Introduction to Computer Systems. Static Scalars and Arrays. Unit 1b

CPSC 213. Introduction to Computer Systems. Scalars and Arrays. Unit 1b

CPSC 213, Winter 2016, Term 2 Final Exam Solution Date: April 19, 2017; Instructor: Mike Feeley and Alan Wagner

CPSC213/2014W1 Midterm EXTRA Practice

CPSC 213, Summer 2017, Term 2 Midterm Exam Solution Date: July 27, 2017; Instructor: Anthony Estey

CPSC 213. Introduction to Computer Systems. Readings for Next 2 Lectures. Loops (S5-loop) Control Flow. Static Control Flow. Unit 1d.

Reading. Companion. Text. - yup, 3.10 again - mainly "Function pointers" box. 1ed: 3.6.6, in Java. static methods are class methods

CPSC 213. Introduction to Computer Systems. Static Control Flow. Unit 1d

CPSC 213. Introduction to Computer Systems. Static Control Flow. Unit 1d

CPSC 213. Introduction to Computer Systems. Dynamic Control Flow Polymorphism and Switch Statements. Unit 1f

CPSC 213. Introduction to Computer Systems. Dynamic Control Flow. Unit 1f Feb 28, Mar 2, 5, 7 and 9. Winter Session 2017, Term 2

CPSC 213. Introduction to Computer Systems. Reading. Control Flow. Loops (S5-loop) Static Control Flow. Unit 1d. Companion.

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

UW CSE 351, Winter 2013 Midterm Exam

CPSC 313, Summer 2013 Final Exam Date: June 24, 2013; Instructor: Mike Feeley

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e Feb 11, 13, 15, and 25. Winter Session 2018, Term 2

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2

Part 1 (70% of grade for homework 2): MIPS Programming: Simulating a simple computer

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

CPSC 213. Introduction to Computer Systems. Instance Variables and Dynamic Allocation. Unit 1c

CSCE 5610: Computer Architecture

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CPSC 313, Winter 2016 Term 1 Sample Date: October 2016; Instructor: Mike Feeley

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Lecture Notes Companion CPSC 213

Computer Architecture /

Homework I - Solution

CODE GENERATION Monday, May 31, 2010

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

Lecture 5: Instruction Set Architectures II. Take QUIZ 2 before 11:59pm today over Chapter 1 Quiz 1: 100% - 29; 80% - 25; 60% - 17; 40% - 3

SOEN228, Winter Revision 1.2 Date: October 25,

Practical Malware Analysis

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

CSE351 Winter 2016, Final Examination March 16, 2016

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code

The Assembly Language of the Boz 5

University of California, San Diego CSE 30 Computer Organization and Systems Programming Winter 2014 Midterm Dr. Diba Mirza

Instruction Set Architectures Part I: From C to MIPS. Readings:

Compilers and Interpreters

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Lectures 3-4: MIPS instructions

Midterm CSE 131 Winter 2012

ENCM 369 Winter 2017 Lab 3 for the Week of January 30

Microcontroller Intel [Instruction Set]

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Two 11 March Your Name (please print) total

System Software Assignment 1 Runtime Support for Procedures

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

Course Administration

COMP 524 Spring 2018 Midterm Thursday, March 1

Lecture 2: The Instruction Set Architecture

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

CS143 Final Fall 2009

Instructions: Language of the Computer

CSCI565 Compiler Design

Lecture 4: Instruction Set Architecture

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

Run-time Environments

Software Protection: How to Crack Programs, and Defend Against Cracking Lecture 3: Program Analysis Moscow State University, Spring 2014

Code Generation (#')! *+%,-,.)" !"#$%! &$' (#')! 20"3)% +"#3"0- /)$)"0%#"

Run-time Environments

MIPS Functions and Instruction Formats

Procedure Call. Procedure Call CS 217. Involves following actions

COS 126 Midterm 1 Written Exam Spring 2015

Computer Architecture

Cache Organizations for Multi-cores

Computer Systems Lecture 9

Final CSE 131B Spring 2004

COS 126 Written Exam 2 (Spring 2015)

ECE 2035 Programming HW/SW Systems Fall problems, 5 pages Exam Three 28 November 2012

Lecture 5: Procedure Calls

Inheritance, Polymorphism and the Object Memory Model

are Softw Instruction Set Architecture Microarchitecture are rdw

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

Question Points Score Total: 100

CS 270 Fall 2013 Mock Final

UW CSE 351, Winter 2013 Final Exam

MIPS (SPIM) Assembler Syntax

CS 24: INTRODUCTION TO. Spring 2018 Lecture 3 COMPUTING SYSTEMS

COS 126 General Computer Science Fall Midterm 1

Operations, Operands, and Instructions

SAM Architecture Reference (Revised)

Midterm CSE 131 Winter 2014

3. Instruction Set Architecture The MIPS architecture

Computer Organization MIPS ISA

Final CSE 131B Winter 2003

Computer System Architecture Midterm Examination Spring 2002

RTN (Register Transfer Notation)

CSE351 Spring 2018, Midterm Exam April 27, 2018

CS 3330 Exam 3 Fall 2017 Computing ID:

CS3330 Exam 3 Spring 2015

Summer 2003 Lecture 14 07/02/03

Principles of Compiler Design

Compilers. Cool Semantics II. Alex Aiken

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

Transcription:

CPSC 213, Winter 2009, Term 2 Midterm Exam Date: March 12, 2010; Instructor: Mike Feeley This is a closed book exam. No notes. Electronic calculators are permitted. Answer in the space provided. Show your work; use the backs of pages if needed. There are 8 questions on 4 pages, totaling 50 marks. You have 50 minutes to complete the exam. STUDENT NUMBER: NAME: SIGNATURE: Q1 / 2 Q2 / 4 Q3 / 4 Q4 / 11 Q5 / 6 Q6 / 8 Q7 / 7 Q8 / 8 Total / 50 1 (2 marks) Memory Addresses. Give an example of a memory address that is aligned to an four-byte boundary, but not to an eight-byte boundary. 2 (4 marks) Pointer Arithmetic. Consider the following three lines of C code. For the assignments to i and j, say (a) whether the code generates a runtime error and why or (b) what value the variables have after the code executes. If the first generates an error and the second does not, give the value of the second ignoring the first. Show your work. 2a i: int a[10] = { 0,1,2,3,4,5,6,7,8,9 }; int i = *(a + ((&a[7])-a) + *(a+2)); int j = *(a + *((&a[7])-a) + *(a+2)); 2b j: 3 (4 marks) Dynamic Allocation. A dangling pointer exits when a program retains a reference to dynamically allocated memory after it has been freed/reclaimed. A memory leak exists when a program fails to free/reclaim dynamically allocated memory that it is no longer needed by the program. 3a Are dangling pointers possible in C, Java, both or either? Why or why not? 3b Are memory leaks possible in C, Java, both or neither? Why or why not?

4 (11 marks) Global Arrays. In C, global arrays can be declared in two ways, exemplified by a and b below. int i; int* a; int b[10]; 4a Indicate which of the following the compiler knows statically and which is determined dynamically. the address of a? the address of b? the address of a[0]? the address of b[0]? the number of bytes between b[0] and b[i]? 4b Give C code that assigns a value to a so that a[0]==b[4], a[1]==b[5] and so on (don t copy the entire array, just assign a value to a). 4c Give SM213 assembly code that reads the value of a[i] into register r0; assume the value of i is in r1. 4d Give SM213 assembly code that reads the value of b[i] into register r0; assume the value of i is in r1. 4e Give SM213 assembly code that stores the value in r0 in a[i]; assume the value of i is in r1. Comment your code. 2

5 (6 marks) Instance Variables. A C struct is a bit like a Java object, but without methods. Variables stored in an object or struct are called instance variables. Now consider this C declaration of two global variables, a and b. typedef struct { int i, j, k; } A; A a; A* b; 5a Indicate which of the following the compiler knows statically and which is determined dynamically. the address of a? the address of b? the address of a.k? the address of b->k? the number of bytes between b->i and b->k? the difference between the value of b and the address of b->k? 5b Give SM213 assembly code that reads the value of b->k into r0. 6 (8 marks) Procedures. Consider the local variables and arguments declared in the following C code. void foo (int a, int b, int c) { int i, j, k; } 6a Indicate which of the following the compiler knows statically and which is determined dynamically. the address of c? the address of k? the offset to k from the value of the stack pointer? the number of bytes between i and k? 6b Give machine/assembly code that implements the procedure call foo (1, 2, 3). Be sure to include every part of the procedure call statement (but just this statement). Pass the arguments on the stack. 3

7 (7 marks) Dynamic Procedure Calls. Procedure calls in C are normally static. Dynamic calls, like Java s polymorphic dispatch, however, have many benefits and can be implemented in C with jump tables (i.e., arrays of function pointers). Consider the following C code that declares (a) an array of pointers to four procedures and (b) a procedure that uses this array to invoke the ith one of them (starting with 0). void proca () { printf ("A"); } void procb () { printf ("B"); } void procc () { printf ("C"); } void procd () { printf ("D"); } void (*proc[4])() = { proca, procb, procc, procd }; int i; void foo () { proc[i] (); } 7a Indicate which of these the compiler knows statically and which is determined dynamically. the address of proc[2]? the value of proc[2]? the address of the procedure that foo calls? 7b Give SM213 assembly code that implements the procedure call proc[i] using as few instructions as possible. 8 (8 marks) Reading Assembly Code. Consider the following snippet of SM213 assembly code. ld $0, r0 # ld 0(r5), r1 # ld 4(r5), r2 # L0: bgt r2, L1 # br L9 # L1: ld (r1), r3 # shl $31, r3 # beq r3, L2 # inc r0 # L2: inca r1 # dec r2 # br L0 # L9: j *(r6) # 8a Carefully comment every line of code above. 8b The code implements a simple function. What is it? Give the simplest, plain English description you can. 4

OpCode Format Semantics Eg Machine Eg Assembly load immediate 0d-- r[d] v 0100 ld $0x1000,r1 vvvvvvvv 00000100 load base 1osd r[d] m[o 4 + r[s]] 1123 ld 4(r2),r3 load indexed 2sid r[d] m[r[i] 4 + r[s]] 2123 ld (r1,r2,4),r3 store base+dis 3sod m[o 4 + r[d]] r[s] 3123 st r1,8(r3) store indexed 4sdi m[r[i] 4 + r[d]] r[s] 4123 st r1,(r2,r3,4) halt f000 f000 halt nop ff00 ff00 do nothing (nop) rr move 60sd r[d] r[s] 6012 mov r1, r2 add 61sd r[d] r[d] + r[s] 6112 add r1, r2 and 62sd r[d] r[d] & r[s] 6212 and r1, r2 inc 63-d r[d] r[d] + 1 6301 inc r1 inc addr 64-d r[d] r[d] + 4 6401 inca r1 dec 65-d r[d] r[d] 1 6501 dec r1 dec addr 66-d r[d] r[d] 4 6601 deca r1 not 67-d r[d]!r[d] 6701 not r1 shift 7dss r[d] r[d] << s 7102 shl $2, r1 71fe shr $2, r1 branch 8-oo pc pc + 2 o 1000: 8004 br 0x1008 branch if equal 9roo if r[r] == 0, pc pc + 2 o 1000: 9104 beq r1, 0x1008 branch if greater aroo if r[r] > 0, pc pc + 2 o 1000: a104 bgt r1, 0x1008 jump b--- pc a b000 jmp 0x1000 aaaaaaaa 00001000 get program counter 6f-d r[d] pc 6f01 gpc r1 jump indirect croo pc r[r] + 2 o c102 jmp 8(r1) jump double ind, b+disp droo pc m[4 o + r[r]] d102 jmp *8(r1) jump double ind, index eri- pc m[4 r[i] + r[r]] e120 jmp *(r1,r2,4) 5