Do not start the test until instructed to do so!

Similar documents
Do not start the test until instructed to do so!

Do not start the test until instructed to do so!

Do not start the test until instructed to do so!

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

Data Representation and Binary Arithmetic. Lecture 2

Chapter 2 Number System

Chapter 2 Bits, Data Types, and Operations

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

Chapter 2 Bits, Data Types, and Operations

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

Number Representations

Chapter 2 Bits, Data Types, and Operations

Chapter 8. Characters and Strings

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

ASSIGNMENT 5 TIPS AND TRICKS

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

Fundamentals of Programming (C)

Chapter 2 Bits, Data Types, and Operations

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

RS-232 Control of the Advantage EQ281/8, EQ282/8 and Advantage SMS200

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

Fundamental Data Types

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

Under the Hood: Data Representation. Computer Science 104 Lecture 2

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Fundamentals of Programming

Midterm CSE 131 Winter 2012

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Chapter 2 Bits, Data Types, and Operations

Chapter 3. Information Representation

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

CSE 30 Winter 2014 Final Exam

CSE 30 Fall 2013 Final Exam

Positional Number System

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Number System (Different Ways To Say How Many) Fall 2016

2a. Codes and number systems (continued) How to get the binary representation of an integer: special case of application of the inverse Horner scheme

Midterm CSE 131 Winter 2013

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

CSE 30 Spring 2007 Final Exam

Computer Organization & Systems Exam I Example Questions

CSE 30 Fall 2007 Final Exam

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

Oberon Data Types. Matteo Corti. December 5, 2001

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

Unit 3. Analog vs. Digital. Analog vs. Digital ANALOG VS. DIGITAL. Binary Representation

Lecture (09) x86 programming 8

CSE 30 Fall 2012 Final Exam

4/14/2015. Architecture of the World Wide Web. During this session we will discuss: Structure of the World Wide Web

EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

3.1. Unit 3. Binary Representation

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

RS-232 Control of the Advantage DRI

EXPERIMENT 7: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

CSE 30 Winter 2009 Final Exam

Number Systems II MA1S1. Tristan McLoughlin. November 30, 2013

EE 109 Unit 2. Binary Representation Systems

The Binary Number System

Simple Data Types in C. Alan L. Cox

CSE 30 Fall 2006 Final Exam

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

CSE 30 Spring 2006 Final Exam

Final CSE 131 Winter 2010

Connecting UniOP to Datalogic Barcode Readers

Variables and data types

Midterm CSE 131 Winter 2014

PureScan - ML1. Configuration Guide. Wireless Linear Imager Wireless Laser scanner - 1 -

Number Systems Base r

CSC 8400: Computer Systems. Represen3ng and Manipula3ng Informa3on. Background: Number Systems

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

Midterm CSE 131 Fall 2014

Computer Control of the Advantage DRC 4+4

D730. Remote Display Installation Manual

Midterm Exam, Fall 2015 Date: October 29th, 2015

Universal Asynchronous Receiver Transmitter Communication

Midterm CSE 131 Winter 2015

Final CSE 131 Fall 2014

Do not start the test until instructed to do so!

CS 2506 Computer Organization II Test 2

CS 159 Credit Exam. What advice do you have for students who have previously programmed in another language like JAVA or C++?

CS 2505 Computer Organization I

Characters Lesson Outline

Final CSE 131 Fall 2015

The following are the data types used in the C programming language:

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, SPRING 2013

FA269 - DIGITAL MEDIA AND CULTURE

Transcription:

Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other electronic devices may be used. The use of any such device will be interpreted as an indication that you are finished with the test and your test form will be collected immediately. Answer each question in the space provided. If you need to continue an answer onto the back of a page, clearly indicate that and label the continuation with the question number. If you want partial credit, justify your answers, even when justification is not explicitly required. There are 5 questions, some with multiple parts, priced as marked. The maximum score is 100. When you have completed the test, sign the pledge at the bottom of this page and turn in the test. If you brought a fact sheet to the test, write your name on it and turn it in with the test. Note that either failing to return this test, or discussing its content with a student who has not taken it is a violation of the Honor Code. Do not start the test until instructed to do so! Name printed Pledge: On my honor, I have neither given nor received unauthorized aid on this examination. signed B 1

xkcd.com B 2

1. [20 points] Implement the C function described below. Be sure to note the restrictions if any, violating the restrictions will result in a very low score. Reverses the ordering of the bits, in every byte, in a bytes_t type. Pre: bytes - Points to a valid bytes_t variable and is not NULL. Post: Every bit in each byte in bytes_data(bytes) has been reversed. Examples: If bytes_data(bytes) contains 0xde the result would be 0x7b. 0xde = 1101 1110, reversed would be 0111 1011, or 0x7b. If bytes_data(bytes) contains 0xde, 0xad the result would be 0x7b, 0xb5. Building on the last example, 0xad = 1010 1101, reversed would 1011 0101, or 0xb5. You may use any function from your bytes_t type (or none). void bytes_reverse_bits(bytes_t * const bytes) uint8_t reverse[] = 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe, 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf; uint8_t * data = bytes_data(bytes); for (size_t b = 0; b < bytes->usage; b++) uint8_t lo = data[b] & 0xf; uint8_t hi = (data[b] >> 4); data[b] = (reverse[lo] << 4) reverse[hi]; void bytes_reverse_bits(bytes_t * const bytes) uint8_t * data = bytes_data(bytes); for (size_t b = 0; b < bytes->usage; b++) uint8_t new = 0; uint8_t old = data[b]; for (uint8_t bit = 0; bit < 8; bit++) new = (new << 1) + (old % 2); old /= 2; data[b] = new; B 3

2. [20 points] Implement the C function described below. Be sure to note the restrictions and pre and post conditions, failure to meet these conditions will result in a low score. /* Humans normally view IP addresses in what's called dot-decimal notation (e.g. * 192.168.1.1) but the address is often stored as 32 bit integer. Each number * in dot-decimal format represents an octet (or byte) in the 32 bit integer. * This function converts from dot-decimal format (string) to binary (uint32_t). * You may assume each number in the IP address >= 0. * * Pre: ip_addr - Points to a properly formatted C string, containing a valid * IP address. * result Points to a potentially uninitialized uint32_t. * * Post: The octets in ip_addr have been converted from their string * representation to integers and stored in the uint32_t, *result. * * Examples: * result contains 0xC0A80101, 0xC0 is 192, 0xA8 is 168, 0x01 is 1. * ip_to_int("192.168.1.1", &result); * result contains 0x7F000001, 0x7F is 127, 0x00 is 0, and 0x01 is 1. * ip_to_int("127.0.0.1", &result); * * Restrictions: * - You may NOT use other I/O, e.g. any printf() or scanf() variants. * - You may NOT make any use of character/string variables or arrays * besides the parameter (ip_addr). * - You may NOT use math.h or string.h, nor any function declared within. * - You may NOT use array brackets, all data access must use pointer syntax. * - You may NOT use any additional libraries. * - You may NOT use recursion. */ void ip_to_int(const char * ip_addr, uint32_t * const result) uint8_t octet = 0; uint8_t tens = 1; uint8_t shift = 0; const char *tmp = ip_addr; while (*tmp) tmp++; *result = 0; while (--tmp >= ip_addr) while ((*tmp!= '.') && (tmp >= ip_addr)) octet += (*tmp - 48) * tens; tens *= 10; tmp--; *result = (((uint32_t) octet) << shift); octet = 0; shift += 8; tens = 1; B 4

3. [12 points] Write a solution for the function below and explain the logic that makes it work. The explanation will be worth more than the solution. You may not use any loops (while, for, do-while), selections (if, switch), or casts, and you may not write any constants that are more than one byte wide. Don t worry about the number of operations. replacebyte(x, n, c) - Replace byte n in x with c Bytes numbered from 0 (LSB) to 3 (MSB) Example: replacebyte(0x12345678,1,0xab) = 0x1234ab78 You may assume 0 <= n <= 3 and 0 <= c <= 255 Legal ops:! ~ & ^ + - << >> int replacebyte(int x, int n, int c) int mask = ~(0xff << (n << 3)); int shift = c << (n << 3); return (x & mask) shift; B 5

4. [18 points] Write a solution for the function below and explain the logic that makes it work. The explanation will be worth more than the solution. You may not use any loops (while, for, do-while), selections (if, switch), or casts, and you may not write any constants that are more than one byte wide. Don t worry about the number of operations. is_between(x, lo, hi) - returns a non-zero value (true) if lo <= x <= hi, and 0 (false) otherwise Precondition: lo <= hi Examples: is_between(5, 5, 10) = true is_between(10, 5, 10) = true is_between(0, 5, 10) = false Legal ops:! ~ & ^ + - << >> int is_between(int x, int lo, int hi) Not worrying about overflow: lo = lo + ~0; return ((lo - x) >> 31) & ~((hi - x) >> 31); B 6

5. Consider the following C function: void mystery(char * input) char *leader, *trailer; leader = trailer = input; while (*trailer) uint64_t tmp = 0x0; uint64_t mask = 0x0; uint8_t * tmp_ptr = (uint8_t *) &tmp; uint8_t * mask_ptr = (uint8_t *) &mask; while((*leader) && (leader - trailer < sizeof(uint64_t))) You may assume char is signed type. char byte = *tmp_ptr++ = *leader++; a if(((0x40 - byte) >> 7) & ~((0x5A - byte) >> 7)) *mask_ptr++ = 0x20; c else *mask_ptr++ = 0x00; tmp = mask ^ tmp; memcpy(trailer, &tmp, leader - trailer); d b trailer = leader; For the questions below, assume the function has been called with input "TestF16!", i.e. mystery("testf16!"). a. [6 points] This function modifies tmp using tmp_ptr. Given the input above, what will tmp contain immediately after the inner while loop finishes (right before d)? Write the answer in big endian order, byte-by-byte and left to right. Be specific, vague answers will receive no credit. 'T' 'e' 's' 't' 'F' '1' '6' '!' 0x54 0x65 0x73 0x74 0x46 0x31 0x36 0x21 B 7

b. [6 points] In plain English, explain what the bitwise operations used in the if statement condition accomplish. This should not be a reiteration of the bitwise operations, and should be specific, e.g. these operations determine the remainder when performing an integer division. Tests if the character is an uppercase letter. If the character is an uppercase letter, 0x20 is added to the mask, otherwise 0x00. c. [6 points] This function also modifies mask using mask_ptr. Given the input above, what will mask contain immediately after the inner while loop finishes (right before d)? Write the answer in big endian order, byte-by-byte and left to right. Be specific, vague answers will receive no credit. 'T' 'e' 's' 't' 'F' '1' '6' '!' 0x20 0x00 0x00 0x00 0x20 0x00 0x00 0x00 d. [6 points] Given the values of tmp and mask from your answers in parts a and c, what is the result of mask ^ tmp? Write the answer in big endian order, byte-by-byte and left to right. Be specific, vague answers will receive no credit. ^ 0x54 0x65 0x73 0x74 0x46 0x31 0x36 0x21 0x20 0x00 0x00 0x00 0x20 0x00 0x00 0x00 0x74 0x65 0x53 0x54 0x66 0x31 0x36 0x21 't' 'e' 's' 't' 'f' '1' '6' '!' e. [6 points] In plain English, explain what the function mystery() accomplishes. This should not be a reiteration of each line or the bitwise operations, and should be specific, e.g. this function sums the numbers from 1-100. Converts the uppercase letters to lowercase letters, leaving the other characters alone. B 8

The ASCII Table 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI 1 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US 2 SP! " # $ % & ' ( ) * +, -. / 3 0 1 2 3 4 5 6 7 8 9 : ; < = >? 4 @ A B C D E F G H I J K L M N O 5 P Q R S T U V W X Y Z [ \ ] ^ _ 6 ` a b c d e f g h i j k l m n o 7 p q r s t u v w x y z ~ DEL Scratch Space Can be used as extra space or scratch space for any question. B 9