Exam 2 Study Guide

Picture ID is REQUIRED for all exams

Exam 2 covers the assembly language portion of the course which is presented in chapter 3 of the text (excluding sections 3.10 - 3.12).

Students are permitted to bring one 8 1/2 x 11 piece of paper to the exam which may contain any information the student chooses. In addition, this instruction set "cheat sheet" will be provided with your exam.

This guide presents topics and suggests questions which are likely to be found on the exam. This guide should not be considered comprehensive, but just a guide.

Students are encouraged to complete the practice problems provided in the text (for which solutions are also provided) as well as the homework problems from the text listed on the class schedule page. Practice problem numbers and page numbers refer to the 2nd edition of the text.

One important skill that runs through all exam questions is beingable to read and interpret a sequence of assembly language instructions as you've done with the bomb project. This skill will be required in many forms which are discussed in more detail below.

    Bits, Bytes and Integers

  1. Convert the following decimal values to hexadecimal
    1. 123456
    2. 313
    3. 42
    4. 109384
  2. Convert the following hex values to decimal and binary
    1. 0x313
    2. 0x42
    3. 0x1234
  3. Convert the following binary values to hex
    1. 010011101101
    2. 01101110010000
    3. 1010110110
  4. What does the C operator sizeof( ) do, and why is important for writing portable C code?
  5. What is the highest memory address available on a machine with a 32-bit word size>
  6. What is meant by the terms big Endian and Litte Endian?
  7. What is the largest positive number that can be stored using 2's complement (TMax)
    in an integer of word size w.
  8. What is the smallest negative number that can be stored using 2's complement (TMin)
    in an integer of word size w.
  9. Let A = 0110000110, and B = 0011011000, both in binary. What is the value of each of the following C expressions?
    1. A | B
    2. A & B
    3. ~A
    4. ~B
    5. !B
    6. A ^ B
    7. A & 0xFF
    8. B | 0xab
    9. A && B
    10. A || B
    11. A << 3
    12. B >> 4
  10. List all "integral" data types in C
  11. Define the terms arithmetic right shift, logical right shift, sign extension, overflow

  12. What possible problems may result from converting an unsigned int to a signed int and vice-versa.
  13. What possible problems may result from converting an int to a short?
  14. Define overflow, negative overflow, positive overflow as they relate to arithmetic operations
  15. Describe a simple technique for performing 2's complement negation.
  16. Explain how bit shifting can be used to perform integer division and multiplication.
  17. Find the decimal values of the following 8-bit binary numbers.
    1. 1000 0001
    2. 0111 1011
    3. 1111 0001
    4. 0010 1010
  18. Perform 2's comlement addition on each pair of 8-bit binary numbers.
    In each case, indicate whether an overflow has occured, assuming an 8-bit word size.
    1. 1110 1001 + 0110 1001
    2. 1110 1011 + 1111 1111
    3. 1000 1100 + 1100 0001
    4. 0111 1001 + 0000 1001
  19. Complete the following table, assuming a 4-bit word size
    Hex ValueBit RepresentationUnsigned ValueSigned Value
    5   
    8   
    A   
    C   
    F   
  20. Assume your program is running on a 6-bit machine using 2's complement arithmetic for signed integers.
    A short integer is encoded using 3 bits.
    Fill in the empty boxes in the table below.
    The following declarations are used in the table
      short sy = -3;
      int y = sy;
      int x = -17;
      unsigned ux = x;
      
    ExpressionDecimal ValueBinary
    Zero0 
    n/a-6 
    n/a 01 0010
    ux  
    y  
    x >> 1  
    TMax  
    -TMin  
    TMin + TMAX  

  21. Assume your program is running on a 32-bit machine with 32-bit integers represented in 2's complement
    and that integers are right shifted arithmetically. Value of type unsigned int are also 32 bits.
    Consider the following declarations
      int x = random( );  /* some random integer */
      int y = random( );
      int z = random( );
      unsigned ux = (unsigned) x;
      unsigned uy = (unsigned) y;
      
    For each of the C expressions in the table below, indicate whether or not the expression always evaluates to true. If not, explain why not
    ExpressionAlways True?
    (x < y ) == (-x > - y) 
    ((x = y) << 4) + y - x == 17 * y + 15 * x  
    ~x + ~y + 1 == )~)x + y) 
    ux - uy == -(y - x)  
    (x >= 0) || (x < ux) 
    ((x >> 1) << 1 <= x 
  22. Registers, Memory and Arithmetic Operations

  23. Practice problem 3.1 (p 170) -- interpreting operands for the movl instruction
  24. Practice problem 3.4 (p 176) -- writing movl instructions and writing the correspondinc C code.
  25. Practice problem 3.6 (p 178) -- practice with leal instructions
  26. Practice problem 3.7 (p 179) -- practice with arithmetic instructions
  27. Practice problem 3.8 (p 180) -- practice with shift instructions
  28. Practice problem 3.9 (p 181) -- interpreting arithmetic instructions
  29. Practice problem 3.27 (p 212) -- practice with reverse engineering assembly code
  30. Discuss the process by which an application consisting of C source file(s) and the C library becomes and executable program.
  31. Given (a) the current contents of some registers, (b) the contents of some addresses in memory, and / or (c) the state of the program stack show the new contents of the registers, memory and stack
    1. after a sequence of one or more arithmetic operations
    2. after a sequence of one or more movl instructions
  32. Compute an effective memory address using any form of simple or indexed address mode.
  33. Given assembly language instructions that represent simple arithemtic operations, be able to reconstuct the equivalent C code and vice-versa.
  34. Control Flow, Loops, Switches

  35. Practice problem 3.13 (p 189) -- practice with cmp instructions
  36. Practice problem 3.14 (p 190) -- practice with test instructions
  37. Practice problem 3.16 (p 195) -- practice with conditional statements
  38. Practice problem 3.17/3.18 (p 196) -- practice with conditional statements
  39. Practice problem 3.20 (p 199) -- practice with do-while loops
  40. Practice problem 3.21 (p 201) -- practice with while loops
  41. Practice problem 3.23 (p 205) -- practice with for loops
  42. Practice problem 3.28 (p 217) -- practice with switch statements
  43. Practice problem 3.29 (p 218) -- practice with switch statements
  44. Give an example of the assembly language syntax for an "indirect" jump.
  45. What C control structure is often implemented with "indirect" jumps?
  46. What are CF, ZF, OF, SF and how are they used to control program flow?
  47. Given assembly language instructions that represent a conditional branch, be able to reconstruct the equivalent C code and vice-versa.
  48. Given C code for a for-loop, while-loop or do-while-loop be able to write the C "go-to" version of the code.
  49. Given the assembly language code for a for-loop, while-loop or do-while-loop, reconstruct the C code for the loop.
  50. Discuss the implementation of a switch statement using a jump table. Why is a jump table appropriate?
  51. Discuss the implementation of a sparse switch statement. Why is a jump table not appropriate?
  52. Discuss the use of the cmpl and setX instructions.
  53. Procedures

  54. Practice problem 3.31 (p 224) -- saving registers in function calls
  55. Practice problem 3.33/3.33 (p 228) -- practice with the stack when making function calls
  56. Practice problem 3.34 (p 231) -- practice with recusive function calls
  57. Define the following terms
    1. stack frame
    2. stack pointer
    3. frame pointer
    4. program counter
  58. Show the effect of the pushl and popl on the stack.
  59. With respect to a function call, what are responsibilities of the caller? Be sure to consider registers, the return address, and the stack.
  60. With respect to a function call, what are responsibilities of the callee? Be sure to consider registers, the return address, and the stack.
  61. The typical "start up" code in a function consists of the following instructions
    
           pushl %ebp;
           movl %esp, %ebp;
    
    
    Describe the purpose of each of these instructions
  62. The typical "finish" code in a function consists of the following instructions
    
           popl %ebp;
           movl %ebp, %esp;
    
    
    Describe the purpose of each of these instructions
  63. Below is a typical implementation of a recursive function to calculate the Nth number in the Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, 21, .....
    
    int fib( int n)
    {
        int val1, val2;
    
        if (n <= 2) return 1;
    
        val1 = fib( n - 1 );
        val2 = fib( n - 2 );
        return val1 + val2;
    }
    
    
    1. Why are the values stored in the variables val1, val2 NOT overwritten each time a recursive call to fib( ) is made?
  64. Arrays

  65. Practice problem 3.35 (p 233) -- practice with arrays
  66. Practice problem 3.36 (p 234) -- practice with arrays
  67. Practice problem 3.37 (p 236) -- practice with 2-d arrays
  68. Answer the questions below regarding the 2-d array short S[5][6]
    1. What is the size of the 2-d array?
    2. If the array is stored in memory starting at location 0x300, what is the memory address of the start of the 2nd row of S?
    3. If the array is stored in memory starting at location 0x300, what is the memory address of S[ 2 ][ 2 ]?
    4. What is the general formula for calculating the memory address of S[ i ][ j ]?
    5. Write the assembly language code to perform the general calculation.
  69. Given an array declaration, 2-d array declaration, or dynamically allocated array and associated C and/or assembly language code, be able to compute the memory address of an array element, size of the array, or the type of the array.
  70. Discuss the problems resulting from C's lack of bounds checking on array indices.
  71. Discuss the differences and similarities between a 2-d array defined as int A[12][12] and as int *A[12] both from the C programmer's view point and from the assembly language viewpoint
  72. Structs and Unions

  73. Practice problem 3.39 (p 243) -- practice with structs
  74. Practice problem 3.40 (p 247) -- practice with unions
  75. Practice problem 3.41 (p 251) -- practice with data alignment
  76. Describe the differences and similarities between structs and unions.
  77. Discuss the concept of alignment and how it affects struct and union definitions.
  78. Discuss the concept of alignment and how it affects an array of structs.
  79. Given the following struct and definition and variable declaraions, answer the questions below.
    
       typedef struct
       {
          int age;
          char name[13];
          short height;     /* in inches */
          float weight;	/* in pounds */
       } PERSON;
    
       PERSON people[12];
       PERSON bob;
    
    
    1. What is the size of PERSON?
    2. Determine the offset of each member of PERSON (in bytes).
    3. True/False -- the size of PESON must be a multiple of 4. If true, why? If false, why not?
    4. True/False -- the variable bob is stored at a memory address which is a multiple of 4.
    5. Write the C code for the function short getHeight( PERSON *p ).
    6. Translate the C code for getHeight( ) into IA32 assembly language.
    7. Write the C code for the function float getWeight( int index ) that returns the weight of people[ index ]
    8. Translate the C code for getWeight( ) into IA32 assembly language.