Due: Thursday, 26 May 2016 by 5pm Submission notes: For full credit, please show your work and denote your answers with a circle or a box. Always write and draw your diagrams neatly! We cannot be expected to GUESS what you meant to write! Please staple your work before submitting and remove any ruffled edges from notebook paper. Points for each problem are as indicated. Some portions of problems are marked as BONUS, which count as extra credit. 1. (20 pts) You are given three 16-bit values shown below. Each of these values can be interpreted as: An unsigned number A sign-magnitude number A two s complement number Provide the decimal (base 10) equivalent of each value for each of these interpretations. Show your work. a. 0x2040 b. 0x4BE5 c. 0xBEEF 2. (10 pts) Binary Coded Decimal (or BCD) is a legacy format that uses 4 bits to represent each digit in a decimal number. a. Express the decimal integer 42517 in BCD. b. If the number N = 0110 0111 0001 0101 0011 1001 is encoded in BCD, what is the decimal value of N? 3. (10 pts) Assume that our class will have 3 exams, 5 labs, and 4 or 5 homework assignments. All exams are at most 100 points; homeworks and labs may have varying point values, but are never more than 150 points. All assignment and exam grades always have integer values (we always round up!). Consider the following code to compute the average of your exam grades: #define NUM_EXAMS (3) char exam_grades[3]; char exam_avg; exam_avg = ((int)(exam_grades[0]+exam_grades[1]+exam_grades[2]))/num_exams; a. Are the variables used appropriately sized? Explain why or why not. b. The last line of the example uses a cast. What is a cast in C programming? Is it necessary in this code? Explain. ECE2049-E16 (Instructor: N. DeMarinis) Page 1 of 5
4. (20 pts) In a table like the one below, show how the following variables would be stored in memory by: A Little-endian CPU A Big-endian CPU int ii = -4; // Two s complement 16-bit integer long long unsigned int ser_num = 0x12345A6B7C8D9EFF; // An unsigned 64-bit integer char *class = ECE2049 ; // Character array containing ASCII text int arr[3] = 1, -32768, -1; // 3-element array of int long unsigned int count = 72342; a. First, determine the representation of each variable listed above in hex. b. Next, arrange the variables as they would be stored in memory starting at address 0x4300. To do this, make a table similar to the one below, labeling each address and denoting which byte would be stored in that address in each case. Remember that each address in memory holds 1 byte. Address Little Endian Big Endian 0x4302 0x4301 0x4300 Note: This table is usually drawn with the lowest memory address at the bottom of the table, as shown here: c. Is the MSP430 architecture we use in class big or little endian? d. (BONUS: 5 pts) What if we added one additional variable in the example above as shown below. Based on only the code below, what is the value of new_var? How is it arranged in memory? long unsigned int count = 72342; unsigned int new_var; ECE2049-E16 (Instructor: N. DeMarinis) Page 2 of 5
5. (10 pts) In C, there are often multiple ways to write code to solve a given problem. Rewrite the following code segment to use a for loop instead of a while loop and to use a switch statement instead of if/else statements. Your code should be typed. #define MAX_KEY_PRESS (20) unsigned char curr_key, press = 0; unsigned char key_hist[max_key_press]; while(press < MAX_KEY_PRESS) curr_key = getkey(); key_hist[press] = curr_key; if(curr_key == * ) leds_off(); buzzer_on(); else if(curr_key == # ) leds_off(); buzzer_off(); else leds_set_value(curr_key); press++; ECE2049-E16 (Instructor: N. DeMarinis) Page 3 of 5
6. (10 pts) Answer the following questions regarding execution of loops. Assume that the code below is executed on an MSP430 (ie, an int has a size of two bytes). a. In the following code segment, how many times does the loop iterate? What are the values of kk, mm, and inval (in decimal) at the beginning and end of the first loop, and the beginning and end of the final loop? unsigned int inval, out, kk = 0x0001, mm = 0x1000; char count = 1; while(kk!= 0) inval = (mm + kk) / kk; out = somefunction(inval); mm /= 2; kk = kk << 1; b. In the code segment below, what is the value of count in the last three loops? char count = 1; while(count > 0) // Do some things... count++; ECE2049-E16 (Instructor: N. DeMarinis) Page 4 of 5
7. (20 pts) This problem involves converting numbers in IEEE754 floating-point format. Express the following number as a 32-bit floating point value. Show your work, and express your final answer in hex: a. -32.875 The following 32-bit hexadecimal values encode a 32-bit floating point number. Find the decimal value it represents. Show your work. b. 0x422A8000 c. 0x3C800000 d. BONUS (5 pts): Express the number 19.3303 as a 32-bit floating point value. How accurate is the floating point representation? ECE2049-E16 (Instructor: N. DeMarinis) Page 5 of 5