Computer Organization & Systems Exam I Example Questions

Similar documents
CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Beginning C Programming for Engineers

THE FUNDAMENTAL DATA TYPES

Bits, Bytes and Integers

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Do not start the test until instructed to do so!

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

CSCI 2212: Intermediate Programming / C Chapter 15

Computer System and programming in C

Jin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Arithmetic and Bitwise Operations on Binary Data

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

A Fast Review of C Essentials Part I

Midterm Exam Review Slides

Fall 2018 Discussion 2: September 3, 2018

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

CMSC 313 Fall2009 Midterm Exam 1 Section 01 October 12, 2009

Hardware: Logical View

SIGNED AND UNSIGNED SYSTEMS

CS 241 Data Organization Binary

Computer Systems Programming. Practice Midterm. Name:

Chapter 3 Basic Data Types. Lecture 3 1

Number Systems, Scalar Types, and Input and Output

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Work relative to other classes

N.B. These pastpapers may rely on the knowledge gained from the previous chapters.

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

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

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Memory, Data, & Addressing II CSE 351 Spring

Fundamentals of Programming

Do not start the test until instructed to do so!

C-string format with scanf/printf

Programming. Elementary Concepts

C-string format with scanf/printf

Lecture 5-6: Bits, Bytes, and Integers

Computer Systems C S Cynthia Lee

Survey. Motivation 29.5 / 40 class is required

CS:APP Web Aside DATA:TNEG: Bit-Level Representation of Two s Complement Negation

Bits, Bytes, and Integers Part 2

Integer Representation Floating point Representation Other data types

: Principles of Imperative Computation Victor Adamchik. Practice Exam - I

CS 33. Data Representation, Part 1. CS33 Intro to Computer Systems VII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Programming Project 2

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Number Representation

16.216: ECE Application Programming Spring 2015 Exam 3 Solution

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Programming refresher and intro to C programming

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

ECET 264 C Programming Language with Applications

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

A flow chart is a graphical or symbolic representation of a process.

printf( Please enter another number: ); scanf( %d, &num2);

Fundamental of Programming (C)

Engineering Computing I

Integer Representation

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

COMP2121: Microprocessors and Interfacing. Number Systems

EL2310 Scientific Programming

ECET 264 C Programming Language with Applications

Arithmetic and Bitwise Operations on Binary Data

The C Programming Language Guide for the Robot Course work Module

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

COMP Primitive and Class Types. Yi Hong May 14, 2015

Integers II. CSE 351 Autumn Instructor: Justin Hsia

Expression and Operator

Bits, Bytes, and Integers

Integers II. CSE 351 Autumn Instructor: Justin Hsia

BITS, BYTES, AND INTEGERS

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Chapter 7. Basic Types

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

BLM2031 Structured Programming. Zeyneb KURT

Arithmetic Operators. Portability: Printing Numbers

CS 107 Lecture 2: Bits and Bytes (continued)

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

Bits, Bytes and Integers Part 1

Integers. Today. Next time. ! Numeric Encodings! Programming Implications! Basic operations. ! Floats

C Syntax Out: 15 September, 1995

Lexical Considerations

a) Write the signed (two s complement) binary number in decimal: b) Write the unsigned binary number in hexadecimal:

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Representation of Information

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Appendix. Bit Operators

More on C programming

INTRODUCTION 1 AND REVIEW

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Transcription:

Computer Organization & Systems Exam I Example Questions 1. Pointer Question. Write a function char *circle(char *str) that receives a character pointer (which points to an array that is in standard C string format, i.e., an array of char whose last entry is the character \0 ) and replaces all instances of a lowercase letter in the string with the corresponding uppercase letter. circle must not change the original string, rather it must return a pointer to a char array with the characters replaced. You may use only bitwise operations (, &, ^) to convert characters. You may not use arithmetic and you may not use a case or a nested if statement that checks for every lowercase letter. You may use if statements in other ways, for example to check whether a character is uppercase. The ASCII chart is attached at the end of this exam. You may declare two character pointers in your program, but my not use any other character variables, character pointer variables, or character or string constants. For example, you may not use the character constant A anywhere in your program, nor may you declare variables of type char. You may not use any string library functions in your solution. You may use loops (for and while both allowed). You may not use array syntax at all. Use C-style hexadecimal notation for all masks (i.e., 0xFF00) SOLUTION: char *circle(char* s){ int i = 0; char *temp, *temp2; // determine the size of s and create a new string temp = s; while (*temp++!= '\0') i++; temp = malloc(i * sizeof(char)); temp2 = temp;

while (*s!= '\0') { printf("s is: %c\n", *s); *temp = *s; if (*s > 96 && *s <= 111) { /* one way: *temp &= 0x0F; *temp = 0x40; */ // faster way: *temp &= 0x4F; if (*s > 111 && *s < 123) { /* one way: *temp &= 0x0F; *temp = 0x50; */ // faster way: *temp &= 0x5F; s++; temp++; return temp2;

2. (20 minutes) Answer the following questions about information representation. a. Do the following steps on each of the numbers given below. For all results assume that the binary numbers are in 2 s complement form. The number of bits available for the binary number is given beside the number. Show all your work at the bottom of this page. 1. Convert the following hexadecimal numbers to binary and then to decimal. Show the results. 2. Next do one ASL operations on the binary representation of each number showing the results in binary. Convert the shifted binary numbers to Hexadecimal and to decimal. Show the binary results and the hexadecimal and decimal results in the table. 3. Next do a ASR operation on the binary representation of the result from step 2. Then convert the shifted binary numbers to Hexadecimal and to decimal. Show the binary results and the hexadecimal results. 5D in 7 bits 3E in 7 bits binary: 101 1101 binary: 011 1110 decimal: 35 decimal: +62 ASL operation binary: 011 1010 binary: 111 1100 hexadecimal: 0x3A hexadecimal: 0x7C decimal: +58 decimal: -4 ASR operation binary: 001 1101 binary: 111 1110 hexadecimal: 0x1D hexadecimal: 0x7E decimal: +29 decimal: -2

b. Perform additions on the following two sets of numbers, assuming seven-bit 2 s complement binary representation. Show the effect on the carry and overflow bits. 001 0111 101 1101 + 011 1111 + 101 0111 C: 0 V: 1 101 0110 011 0100 C: 1 V: 1 3. (30 minutes) (20 points) For each of the following propositions of C code, indicate if they are true or false. If false, give a counterexample. We assume that the variables are declared as follows int x,y; unsigned ux; and initialized to some unknown value. You should formulate your counterexamples in terms of the word size w. I have given the first answer as an example. You may assume right shift is arithmetical. Problem Answer Reason if false x < 0 è ((x*2) < 0) False TMin ux >= 0 True x & 7 == 7 è (x<<30) < 0 True ux > -1 False Both treated as unsigned. ux=1 x > y è -x < -y False 1, TMin x * x >= 0 False TMin x > 0 && y > 0 è x + y > 0 False TMax, TMax (overflow) x >= 0 è -x <= 0 True All pos num can be represented as neg num x <= 0 è -x >= 0 False TMin

4. Provide answers to the following pointer & dynamic memory questions a. Consider the declaration below. Change this declaration into a pointer declaration and give the code to dynamically create the 2D array. Both dimensions must be declared dynamically. Do not use a loop. int ARR1[ROWS][COLS]. int *ARR1 = (int **) malloc (sizeof(int) * ROWS * COLS) 5. Solve the below problems following these instructions. For more examples, see the lecture slides. Each "Expr" is an expression using ONLY the following: 1. Integer constants 0 through 255 (0xFF), inclusive. You are not allowed to use big constants such as 0xffffffff. 2. Function arguments and local variables (no global variables). 3. Unary integer operations! ~ 4. Binary integer operations & ^ + << >> Some of the problems restrict the set of allowed operators even further. Each "Expr" may consist of multiple operators. You are not restricted to one operator per line. You are expressly forbidden to: 1. Use any control constructs such as if, do, while, for, switch, etc. 2. Define or use any macros. 3. Define any additional functions in this file. 4. Call any functions. 5. Use any other operations, such as &&,, -, or?: 6. Use any form of casting. 7. Use any data type other than int. This implies that you cannot use arrays, structs, or unions. /* evenbits - return word with all even-numbered bits set to 1 * Legal ops:! ~ & ^ + << >> * Max ops: 8 * Rating: 1 */ int evenbits(void) {

int byte = 0x55; int word = byte byte<<8; return word word<<16;