Princeton University COS 217: Introduction to Programming Systems Spring 2007 Final Exam Answers

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

1 if (a == b) 2 { 3 /* true stuff here */ 4 } 5 else 6 { 7 /* false stuff here */ 8 } 10 /* done either way (i.e., whether true.

CPS104 Recitation: Assembly Programming

Final Exam. Fall Semester 2015 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Goals of this Lecture

Assembly Language Programming - III

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

CPSC W Term 2 Problem Set #3 - Solution

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

Introduction Selected details Live demos. HrwCC. A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner

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

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

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

Sungkyunkwan University

Process Layout and Function Calls

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

Assembly Language: Function Calls

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

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

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

Assembly Language: Function Calls" Goals of this Lecture"

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

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

some things that you know in C assignment simple arithme7c opera7ons func7ons condi'onals loops

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

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

Processes (Intro) Yannis Smaragdakis, U. Athens

System Programming and Computer Architecture (Fall 2009)

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

CS , Fall 2004 Exam 1

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

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

Process Layout, Function Calls, and the Heap

CIT Week13 Lecture

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

An Elegant Weapon for a More Civilized Age

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. A Taste of C. Agenda.

CS 201 Winter 2014 (Karavanic) Final Exam

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

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

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

Datorarkitektur, 2009 Tentamen

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

Machine-level Programming (3)

Lab 10: Introduction to x86 Assembly

CPEG421/621 Tutorial

CS241 Computer Organization Spring Loops & Arrays

Credits and Disclaimers

Roadmap: Security in the software lifecycle. Memory corruption vulnerabilities

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

Secure Programming Lecture 3: Memory Corruption I (Stack Overflows)

Princeton University Computer Science 217: Introduction to Programming Systems. A Taste of C

Instruction Set Architecture

Machine-Level Programming III: Procedures

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

Assignment 11: functions, calling conventions, and the stack

Intel assembly language using gcc

EECS 213 Fall 2007 Midterm Exam

Instructor: Alvin R. Lebeck

ASSEMBLY III: PROCEDURES. Jo, Heeseung

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

Assembly III: Procedures. Jo, Heeseung

CS241 Computer Organization Spring 2015 IA

x86 assembly CS449 Fall 2017

CS , Fall 2001 Exam 1

Second Part of the Course

Assembly Language: IA-32 Instructions

Machine Representa/on of Programs: Control Flow cont d. Previous lecture. Do- While loop. While- Do loop CS Instructors: Sanjeev Se(a

Machine-Level Programming II: Control and Arithmetic

Computer Systems CEN591(502) Fall 2011

CS 3843 Final Exam Fall 2012

Machine Level Programming II: Arithmetic &Control

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

THEORY OF COMPILATION

CS213. Machine-Level Programming III: Procedures

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

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

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

Instruction Set Architecture

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Instruction Set Architecture

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

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

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

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

Instruction Set Architecture

CISC 360 Instruction Set Architecture

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

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

Credits and Disclaimers

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

X86 Assembly -Procedure II:1

CSE 351 Spring 2017 Final Exam (7 June 2017)

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

Stack Debugging. Young W. Lim Sat. Young W. Lim Stack Debugging Sat 1 / 40

Transcription:

Princeton University COS 217: Introduction to Programming Systems Spring 2007 Final Exam Answers The exam was a three-hour, open-book, open-notes exam. Question 1a File client.c: #include <stdio.h> #include <assert.h> #include <ctype.h> #include "buffer.h" #define CONTROL(x) ((x) & 31) void capitalize_forward(buffer_t buf) int size; int pos; char c; size = Buffer_size(buf); if (size == 0) return; pos = Buffer_pos(buf); if (pos == size) return; c = Buffer_getchar(buf, pos); if (islower(c)) Buffer_delete(buf); c = toupper(c); Buffer_insert(buf, c); else Buffer_forward(buf); void display(buffer_t buf) int min = Buffer_pos(buf) < 20? 0 : Buffer_pos(buf)-20; int max = Buffer_size(buf)-Buffer_pos(buf) < 20? Buffer_size(buf) : Buffer_pos(buf)+20; int i; for (i=min; i<max; i++) if (i==buffer_pos(buf)) putchar(' '); putchar(buffer_getchar(buf,i)); if (i==buffer_pos(buf)) putchar(' '); putchar('\n'); void run(void) Buffer_T buf = Buffer_new(); for(;;) char c = getchar(); switch(c) case CONTROL('f'): Buffer_forward(buf); Page 1 of 8

case CONTROL('b'): Buffer_back(buf); case CONTROL('d'): Buffer_delete(buf); case CONTROL('h'): Buffer_back(buf); Buffer_delete(buf); case CONTROL('c'): return; case CONTROL('k'): /* new case here */ capitalize_forward(buf); default: if (isprint(c)) Buffer_insert(buf,c); display(buf); #include <termios.h> #include <unistd.h> int main(int argc, char **argv) /* Set the input to no-echo, character-at-time ("cbreak") * mode, and remember the old mode in t0 */ struct termios t0, t1; tcgetattr(0,&t0); t1 = t0; t1.c_lflag &=!(ECHO ICANON); tcsetattr(0,0,&t1); run(); /* Set the terminal back to its original mode */ tcsetattr(0,0,&t0); return 0; Question 1b When the front array is empty and the back array is nonempty, the correct behavior is to (1) capitalize the character at the cursor if and only if it is an uppercase letter, and (2) move the cursor forward. main() calls run(). run() calls capitalize_forward(). capitalize_forward() calls Buffer_getchar(). Buffer_getchar() returns the first character in the back array. If the character is an uppercase letter, then capitalize_forward() then calls Buffer_delete() to remove that character from the back array, capitalizes the character, and calls Buffer_insert() to insert the character. Doing so moves the cursor past the inserted character. If the character is not an uppercase letter, then capitalize_forward() simply calls Buffer_forward() to move the cursor forward. When the front array is nonempty and the back array is empty, the correct behavior is to do nothing. main() calls run(). run() calls capitalize_forward(). capitalize_forward() calls Buffer_size() and Buffer_pos(), discovers that the values returned by those function calls are equal, and returns. When the front and back arrays both are empty, the correct behavior is to do nothing. main() calls run(). run() calls capitalize_forward(). capitalize_forward() calls Buffer_size(), discovers that the value returned is 0, and returns. Page 2 of 8

Question 2a File intmap.h #ifndef INTMAP #define INTMAP typedef struct IntMap *IntMap_T; IntMap_T IntMap_new(void); void IntMap_free(IntMap_T m); int IntMap_lookup(IntMap_T m, int i); void IntMap_update(IntMap_T m, int i, int x); #endif File intmap.c #include "intmap.h" #include <stdlib.h> #include <assert.h> enum INITIAL_SIZE = 100; struct IntMap int *array; int size; ; IntMap_T IntMap_new(void) int i; IntMap_T m; m = (IntMap_T)malloc(sizeof(struct IntMap)); assert(m!= NULL); m->array = (int*)malloc(initial_size * sizeof(int)); assert(m->array!= NULL); for (i = 0; i < INITIAL_SIZE; i++) m->array[i] = 0; m->size = INITIAL_SIZE; return m; void IntMap_free(IntMap_T m) if (m == NULL) return; free(m->array); free(m); static void IntMap_expand(IntMap_T m, int newsize) int i; int *newarray; assert(m!= NULL); assert(i >= 0); assert(m->size < newsize); newarray = (int*)malloc(newsize * sizeof(int)); for (i = 0; i < m->size; i++) newarray[i] = m->array[i]; for ( ; i < newsize; i++) newarray[i] = 0; free(m->array); m->array = newarray; Page 3 of 8

int IntMap_lookup(IntMap_T m, int i) assert(m!= NULL); assert(i >= 0); if (! (i < m->size)) IntMap_expand(m, i+1); return m->array[i]; void IntMap_update(IntMap_T m, int i, int x) assert(m!= NULL); assert(i >= 0); if (! (i < m->size)) IntMap_expand(m, i+1); m->array[i] = x; File main.c #include <stdio.h> #include <stdlib.h> #include "intmap.h" int main (int argc, char ** argv) IntMap_T m = IntMap_new(); int i; IntMap_update(m,4,5); IntMap_update(m,2,1); IntMap_update(m,9,3); for (i = 0; i < 12; i++) printf("%d ",IntMap_lookup(m,i)); printf("\n"); IntMap_free(m); return 0; Question 2b CFLAGS = -Wall -ansi -pedantic # CFLAGS = -Wall -ansi -pedantic O3 main: main.o intmap.o gcc $(CFLAGS) main.o intmap.o -o main main.o: main.c intmap.h gcc $(CFLAGS) -c main.c intmap.o: intmap.c intmap.h gcc $(CFLAGS) -c intmap.c Page 4 of 8

Question 3 There was a small correction to the question. The statement "int i;" should appear at the beginning of the definition of f()..section ".text".equ N 8.equ I -4.equ A -44.globl f.type f, @function f: pushl %ebp movl %esp, %ebp # int i; subl $4, %esp # int a[10]; subl $40, %esp # i = 0; movl $0, I(%ebp) loop: # if (i > n) goto loopexit; movl N(%ebp), %eax cmpl %eax, I(%ebp) jg loopexit # a[i] = n-i; movl N(%ebp), %eax movl I(%ebp), %ecx subl %ecx, %eax leal A(%ebp), %edx movl %eax, (%edx, %ecx, 4) # i++ incl I(%ebp) # goto loop jmp loop loopexit: # return g(a) + 3; leal A(%ebp), %eax pushl %eax call g addl $4, %esp addl $3, %eax movl %ebp, %esp popl %ebp ret Page 5 of 8

Question 4a stackheight g Old f Old f Old main Old 0 Question 4b.section ".text".globl stackheight.type stackheight, @function stackheight: pushl %ebp movl %esp, %ebp # Set the count to 0. movl $0, %eax # Fetch the first "old ". movl (%ebp), %ecx loop: # If the "old " is 0, then jump to loopend. cmpl $0, %ecx je loopend # Increment the count. incl %eax # Fetch the next "old ". movl (%ecx), %ecx jmp loop loopend: # Return the count (which happens to be in EAX already). movl %ebp, %esp popl %ebp ret Page 6 of 8

Question 5 We are given: P(f n+1 ham) = P(f n+1 spam) We must prove: (1) P(spam f 1,...~f n,f n+1 ) = P(spam f 1,...~f n ) (2) P(spam f 1,...~f n,~f n+1 ) = P(spam f 1,...~f n ) Part (1) P(spam f 1,...~f n,f n+1 ) Use the naïve Bayesian learning algorithm: P(spam)P(f spam)...p(~f spam)p(f 1 n n+1 spam) P(ham)P(f 1 ham)...p(~f n ham)p(f n+1 ham) + P(spam)P(f 1 spam)...p(~f n spam)p(f n+1 spam) Replace P(f n+1 ham) with P(f n+1 spam): P(spam)P(f spam)...p(~f spam)p(f 1 n n+1 spam) = P(ham)P(f 1 ham)...p(~f n ham)p(f n+1 spam) + P(spam)P(f 1 spam)...p(~f n spam)p(f n+1 spam) Divide both numerator and denominator by P(f n+1 spam): P(spam)P(f 1 spam)...p(~f n spam) = P(ham)P(f 1 ham)...p(~f n ham) + P(spam)P(f 1 spam)...p(~f n spam) Use the naïve Bayesian learning algorithm: P(spam f 1,...~f n ) Part (2) P(spam f 1,...~f n,~f n+1 ) Use the naïve Bayesian learning algorithm: P(spam)P(f 1 spam)...p(~f n spam)p(~f n+1 spam) P(ham)P(f 1 ham)...p(~f n ham)p(~f n+1 ham) + P(spam)P(f 1 spam)...p(~f n spam)p(~f n+1 spam) P(~f n+1 spam) = 1 - P(f n+1 spam) = 1 - P(f n+1 ham) = P(~f n+1 ham) So replace P(~f n+1 ham) with P(~f n+1 spam): P(spam)P(f spam)...p(~f spam)p(~f 1 n n+1 spam) = P(ham)P(f 1 ham)...p(~f n ham)p(~f n+1 spam) + P(spam)P(f 1 spam)...p(~f n spam)p(~f n+1 spam) Divide both numerator and denominator by P(~f n+1 spam): P(spam)P(f spam)...p(~f spam) 1 n = P(ham)P(f 1 ham)...p(~f n ham) + P(spam)P(f 1 spam)...p(~f n spam) Use the naïve Bayesian learning algorithm: P(spam f 1,...~f n ) Page 7 of 8

Question 6 The spammer could create a script that: (1) Accesses the free e-mail account web site, capturing the CAPTCHA. (2) Places the CAPTCHA on a web site that offers free pornography, with the caveat that the user must "type the letters you see in this image." (3) Forwards the letters typed by the user to the e-mail account web site. (4) Repeats those steps. Thus the script allows the spammer to sign up for many free e-mail accounts automatically. Copyright 2007 by Robert M. Dondero, Jr. Page 8 of 8