CS 5513 Entry Quiz. Systems Programming (CS2213/CS3423))

Similar documents
Dynamic Memory Allocation and Command-line Arguments

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

CS241 Computer Organization Spring Data Alignment

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

Dynamic Memory Allocation

Memory Management. CSC215 Lecture

Dynamic memory allocation

CPEG421/621 Tutorial

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 6

Computer Systems Lecture 9

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

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

CS241 Computer Organization Spring 2015 IA

x86 assembly CS449 Fall 2017

Representation of Information

Understanding Pointers

Process Layout and Function Calls

Lecture 3: Instruction Set Architecture

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

CNIT 127: Exploit Development. Ch 3: Shellcode. Updated

Lecture 8 Dynamic Memory Allocation

Memory Allocation. General Questions

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1


Introduction to C. Systems Programming Concepts

CS 11 C track: lecture 5

Practical Malware Analysis

CS , Fall 2004 Exam 1

CPS104 Recitation: Assembly Programming

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

C introduction: part 1

Anne Bracy CS 3410 Computer Science Cornell University

ESC101N: Fundamentals of Computing End-sem st semester

C Tutorial. Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

PRINCIPLES OF OPERATING SYSTEMS

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

For Teacher's Use Only Q No Total Q No Q No

System Programming and Computer Architecture (Fall 2009)

Computer Architecture and Assembly Language. Practical Session 5

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

Assembler Programming. Lecture 10

Short Notes of CS201

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

Lecture 10: Program Development versus Execution Environment

CS201 - Introduction to Programming Glossary By

ECE 2035 Programming HW/SW Systems Spring problems, 7 pages Exam One Solutions 4 February 2013

Outline. Unresolved references

Assembly basics CS 2XA3. Term I, 2017/18

M2 Instruction Set Architecture

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Portland State University. CS201 Section 5. Midterm Exam. Fall 2018

CS C Primer. Tyler Szepesi. January 16, 2013

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

System calls and assembler

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

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

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

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

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

CSE 333 Midterm Exam Sample Solution 7/28/14

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

CS 61C: Great Ideas in Computer Architecture Introduction to C

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

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

C and Programming Basics

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

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

Intermediate Programming, Spring 2017*

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

CS61, Fall 2012 Midterm Review Section

Buffer overflow is still one of the most common vulnerabilities being discovered and exploited in commodity software.

The Compilation Process

Process Layout, Function Calls, and the Heap

Orange Coast College. Business Division. Computer Science Department CS 116- Computer Architecture. The Instructions

Is stack overflow still a problem?

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CS240: Programming in C

CS201 Some Important Definitions

Come and join us at WebLyceum

Lecture 03 Bits, Bytes and Data Types

Class Information ANNOUCEMENTS

A Short Course for REU Students Summer Instructor: Ben Ransford

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

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

putting m bytes into a buffer of size n, for m>n corrupts the surrounding memory check size of data before/when writing

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

CS 2630 Computer Organization. Meeting 10/11: data structures in MIPS Brandon Myers University of Iowa

CS 314 Principles of Programming Languages. Lecture 11

211: Computer Architecture Summer 2016

CSCI 171 Chapter Outlines

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Towards the Hardware"

Transcription:

Name (please print): CS 5513 Entry Quiz Systems Programming (CS2213/CS3423)) 1. What is a compiler? In addition to the definition, give examples of compilers you have used. A compiler is a program that translate a high-level language like C so that it can run ona particular target. An example would be gcc, which takes in C code, and produces assembly, object files and/or executables (thus it calls a linker). 2. Compare and contrast object file, executable, and source file. Source file: code in high level language such as C, which is read in by a compiler to produce object code. Object code is simply source code that has been compiled so that it is in the hex format specified by the machine s ISA (binary form of assembly). An executable is a series of object files that are linked to make a full program for the target architecture. 3. What command would you give to compile a program on the commandline assuming you save your C routine to the name bob.c, and wish the executable to be called xmyprog and that you have used the standard C function sqrt? gcc -o xmyprog bob.c -lm 1

4. Assume you have allocated a float array during the execution of a procedure, and you need to pass back the allocated pointer as a parameter. Assume the subroutine-local float pointer name is fp, and that the parameter name is Pret. What should the declaration of Pret look like (eg., how many * do you need?), and how would you assign fp (already allocated inside the routine) to this parameter? Declaration: float **Pret assignment: *Pret = fp; 5. What is the difference between calloc and malloc? Calloc zeros the allocated space, and malloc does not. 6. What would the following print statement print out: printf("sizeof(int,float,double) = (%d,%d,%d)\n", sizeof(int), sizeof(float), sizeof(double)); sizeof(int,float,double) = (4,4,8) 2

7. Write a main routine that reads command line arguments. It should accept the flags in any order, and accept an integer, a float, and a string. These values will be indicated by the flags -I, -F, and -S, respectively. Your program should read them in from the command line, and print out the discovered values. So, the invocation:./xmyprog -I 6 -F 3.2 -S "I know systems programming" Should print out: You have entered: Int = 6 float = 3.20 string = I know systems programming done. You should generate the same output if the flag order is switched. You should print a usage message if any other flags are passed. If values are not specified, the integer should default to -1, the float should default to 3.14, and the string should default to default string. 3

#include <stdio.h> #include <stdlib.h> void PrintUsage(int iarg, char *arg) fprintf(stderr, "Unrecognized flag at %d: %s \n", iarg, arg); fprintf(stderr, "USAGE: [-I <int>] [-F <float>] [-S <string>]\n"); exit(iarg?iarg:-1); int main(int nargs, char **args) int i, I=(-1); float F=3.14; char *str = "default string"; for (i=1; i < nargs; i++) if (args[i][0]!= - ) PrintUsage(i, args[i]); switch(args[i][1]) case S : if (++i >= nargs) PrintUsage(i, "out of flags"); str = args[i]; break; case F : if (++i >= nargs) PrintUsage(i, "out of flags"); F = atof(args[i]); break; case I : if (++i >= nargs) PrintUsage(i, "out of flags"); I = atoi(args[i]); break; default: PrintUsage(i, args[i]); printf("you have entered:\n"); printf(" Int = %d\n", I); printf(" float = %.2f\n", F); printf(" string = %s \n", str); printf("done.\n"); return(0); 4

8. Complete the function outlined below, assuming proper C standard header files have already been included. #define INITSIZE 16 double *AppendToArray ( int N, /* # of elements from newd to add to existing array */ double *newd /* array, at least N long, of elts to add to existing array */ ) /* * This routine appends the first N elements of newd to a dynamically * allocated array, which is returned by this function, and persists between * function calls. The new elements are assigned after the last valid entry * in the existing array. If this function is called with N<0, then any * dynamically allocated array is deallocated, and NULL is returned. * For non-negative N, if the array does not exist, it is allocated with size * of MAX(2*N,INITSIZE). If the array exists but is not large enough to hold * the union of the old and new values, then a new array of at least double * the length (and large enough to hold all values) is allocated, the old * and new values are assigned to the new array, and the old array is * deallocated. Values are kept in the order in which they were appended. * * RETURNS: pointer to dynamically allocated array holding the union of any * old values (first entries) with new values (next N entries). */ static int n=0, len=0; static double *d=null; int i; /* * Got array entries to append */ if (N > 0) int newn = N + n; /* * If present array too short, allocate one that is long enough, * copy the old stuff to it, and free old array */ if (newn > len) double *p; if (len == 0) len = (INITSIZE >= N+N)? INITSIZE : N+N; else len = (newn >= len+len)? newn : len+len; p = malloc(len * sizeof(double)); assert(p); for (i=0; i < n; i++) p[i] = d[i]; if (d) free(d); d = p; 5

for (i=n; i < newn; i++) d[i] = newd[i-n]; n = newn; /* * Want to free existing array */ else if (N < 0) free(d); len = n = 0; d = NULL; return(d); #define NELT 20 int main(int nargs, char **args) int i; double d, *dp; for (i=0; i < NELT; i++) d = i; dp = AppendToArray(1, &d); for (i=0; i < NELT; i++) printf("d[%d] = %.2f\n", i, dp[i]); assert(!appendtoarray(-1, NULL)); return(0); 6

Assembly & Pipelining (CS 3843 Computer Organization) 9. Assume that MIPS register r2 holds the address of the start of the 80-element integer array X, and that r3 holds the address of the start of the the 80-element integer array Y. Assuming integers are 32-bits, write a MIPS assembly code fragment to add the corresponding elements of X into Y. Assume one delay slot. li r4, 80*4 /* stx = 80*sizeof(int) */ dadd r2, r4 /* stx = X + 80*sizeof(int) */ LOOP: lw r5, 0(r2) /* r5 = *X */ lw r6, 0(r3) /* r6 = *Y */ dadd r5, r6 /* r6 = *Y + *X */ sw r6, 0(r3) /* *Y = r6 */ daddiu r2, r2, 4 /* X++ */ bne r2, r4, LOOP /* while (stx!= X); */ daddiu r3, r3, 4 /* Y++; in delay slot! */ 10. Add the hex number 0x8f to the binary number 0b11001, and write the answer in both decimal and hex form. 1000 1111 + 1 1001 ========= 1010 1000 = (10) (8) = 0xA8 = 2^3 + 2^5 + 2^7 = 8 + 32 + 128 = 168 7

11. Assume that IA32 register %eax holds the address of the start of the 80-element integer array X, and that %ecx holds the address of the start of the the 80-element integer array Y. Assuming integers are 32-bits, write a x86-32 assembly code fragment to add the corresponding elements of X into Y. add $80*4, %eax /* X += N */ add $80*4, %ecx /* Y += N */ movl $-80*4, %edx /* edx = loop index */ LOOP: mov (%eax, %edx), %ebx /* ebx = *X */ add (%ecx, %edx), %ebx /* ebx = *X + *Y */ mov %ebx, (%ecx, %edx) add $4, %edx jnz LOOP 12. Assume a 6 stage pipeline, with cache access completion in stage 4, hazard detection (and stall) occuring in stage 2, and integer EX occuring in stage 3. How many cycles of delay are there for a load immediately followed by a use (DADD), assuming single issue and forwarding? Draw the pipeline diagram demonstrating the delay you report. S1 S2 S3 S4 S5 S6 S1 S2 S2 S3 S4 S5 S6! only one cycle of delay 8

CS 3733 Operating Systems 13. What is a process, and how is it different than a thread? A process is a thread of execution that has its own private virtual memory address space. A thread is a lightweight process that shares its virtual address space with other threads (making inter-thread communication much easier than inter-process), and only has a private stack space. 14. Draw a picture of a typical layout of virtual memory, including the locations of the stack and heap. stack unused space heap data (global) text (code) 0x0 9