Midterm 1 Name: Tuesday September 25, 2018 UIN: Page 1 of 6 Midterm 1 – CS 261 Please read this page and follow the directions before proceeding with the rest of the exam. 1) Put your name on every page of this exam in the space provided on the top of each page. Be sure to put your name on any scratch paper you submit with the exam. Doing so will get you 2 points. 2) Carefully read the entire exam while doing 1) above 3) Place your answers in the boxes provided to assist in grading 4) GOOD LUCK!!! Problem Page 2 Page 3 Page 4 Page 5 Directions Total Max Points 25 30 21 24 2/0 100 Points Midterm 1 Name: Tuesday September 25, 2018 UIN: Page 2 of 6 1. Write a sequence of C bitwise operations that will create the 3-nibble value RESULT, that is composed of the 0th, 3rd, and 6th nibble from the 32-bit quantity stored in the variable X. For example, if X is equal to 0xABCDEF98 (0x8 is the 0th nibble, E is the 3rd nibble, and B is the 6th nibble) your sequence should return 0x0BE8 in RESULT. You can assume X is declared as an int and RESULT is declared as an int on systems3/systems4 (15 points) 2. Encode the decimal value 2018 as a signed 2's complement binary number using 16 bits. (5 points) 3. Encode the decimal value -2018 as a signed 2's complement binary number using 16 bits. (5 points) Midterm 1 Name: Tuesday September 25, 2018 UIN: Page 3 of 6 4. Encode the binary value 1011110110101011100 as a hexadecimal value (5 points) 5. Give the binary for the following hexadecimal value 0x3C7A1 (5 points) 6. Assume that rbp, rax, and rcx are initialized to the values shown in the table BEFORE each instruction is executed. State the 64-bit value that will be in rcx AFTER each instruction is executed. (5 points each instruction) (20 points total) rbp rax rcx 0x0000000000FE840 0x0000000000000003 0xFFFF0000FFFF0000 Instruction leaq 64-bit result in rcx after the instruction is executed 8(%rbp,%rax,8), %rcx andq 8(%rbp), %rcx movq 0x10(%rbp),%rcx sarq $6, %rcx Address Memory Value 0xFE830 0xDEED0DAD0FED0ADE 0xFE838 0xCAB0DAB0BED0ACE0 0xFE840 0xABABABABABABABAB 0xFE848 0x1C532145541235C1 0xFE850 0x1234567890ABCDEF 0xFE858 0xABCDEF0134561327 0xFE860 0x72AE983C4E2E3CB1 Midterm 1 Name: Tuesday September 25, 2018 UIN: Page 4 of 6 7. Using the values shown for the memory (some of which is the stack), $rsp, $rax and $rbx, state which register changes as a result of each instruction, and what the new value for the register will be after the instruction. Start with the same initial stack and register values for each instruction. (21 points) rsp rax rbx 0x0000000000FE848 0x0000000000000003 0xFFFF0000FFFF0000 Memory / Stack Address Value 0xFE830 0xDEED0DAD0FED0ADE 0xFE838 0xCAB0DAB0BED0ACE0 0xFE840 0xABABABABABABABAB 0xFE848 0x1C532145541235C1 0xFE850 0x1234567890ABCDEF 0xFE858 0xABCDEF0134561327 0xFE860 0x72AE983C4E2E3CB1 Which register changes (2 pts each) a) pushq $rax b) movq 8($rsp), $rbx c) ret New register value is (5 pts each) Midterm 1 Name: Tuesday September 25, 2018 UIN: Page 5 of 6 7. For each assembly code snippet, give the C code that could have produced the assembly when compiled with gcc. For variables, i is stored at -4(%rbp) and j is stored at -8(%rbp). (24 points total, 8 points each snippet) Assembly C code that produces the assembly to the left movl jmp $5, -4(%rbp) .L2 .L3: ** some assembly code ** addl $2, -4(%rbp) .L2: cmpl $14, -4(%rbp) jle .L3 movl movl cmpl jg -4(%rbp), %edx -8(%rbp), %eax %eax, %edx .L2 ** some assembly code ** jmp .L3 .L2: ** some assembly code ** .L3: sarl movl addl movl addl movl -8(%rbp) -8(%rbp), %eax %eax, -4(%rbp) -4(%rbp), %eax %eax, %eax %eax, -8(%rbp) Midterm 1 Name: Tuesday September 25, 2018 UIN: Page 6 of 6