Exam 1

Prof. Tak Auyeung

Instructions: You may bring any material that is handwritten or printed prior to the examination to help you. You can also bring a calculator if you think it may help you. However, you can only use the calculator for numerical computations only.

You, as an individual, are expected to do your own work. This means you cannot seek, receive or otherwise acquire any assistance except clarifications from the professor during an examination. Any communication involving the contents of the subject matter or the examination is considered cheating. Do not initiate or accept such communication, or the result of your examination is automatically voided.

Each correct answer is worth one point, each wrong answer is worth zero point, and each unanswered question is also worth zero point. This means you should guess and leave no question unanswered.

Make sure you write down you name on the upper right corner first, otherwise I cannot give points to anonymous students!

  1. Which of the following instructions potentially changes the value of a general purpose register? Assume labels are defined so that all lines assemble.
    1. movl $0xff, (%eax)
    2. movl $0xff, eax
    3. movl $0xff, 6324523
    4. movb $0xff, (,%eax)
    5. None of the other choices modify a general purpose register
  2. Observe the following code:
    movb $0x80,%al
    subb $____,%al
    

    Select a value for ____ such at the carry (C) flag is set after the subb instruction executes.

    1. 0x00
    2. 0x10
    3. 0x7f
    4. 0x80
    5. 0xff
  3. The following are byte values at location somevar:
    0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08
    
    Assume that %eax has the address of somevar. What is the content of somevar after the following code executes?
    movl (%eax),%ebx
    addl $1,%eax
    movl %ebx,(%eax)
    
    1. 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08
    2. 0x01 0x01 0x03 0x04 0x05 0x06 0x07 0x08
    3. 0x01 0x01 0x02 0x03 0x04 0x05 0x06 0x07
    4. 0x01 0x01 0x02 0x03 0x04 0x06 0x07 0x08
    5. 0x01 0x02 0x03 0x04 0x01 0x02 0x03 0x04
  4. Which of the following code compares two 64-bit number, and branches to label1 if and only if val1 is greater than val2? Assume val1 and val2 are signed, and they are properly allocated space. Select an answer that works in general.
    1. movl val1,%eax
      cmpl val2,%eax
      jg   label1
      
    2. movl val1+4,%eax
      cmpl val2+4,%eax
      jg   label1
      jl   label2
      movl val1,%eax
      cmpl val2,%eax
      jg   label1
      label2:
      

    3. movl val1,%eax
      cmpl val2,%eax
      jg   label1
      movl val1+4,%eax
      cmpl val2+4,%eax
      jg   label1
      
    4. movl val1,%eax
      cmpl val2+4,%eax
      jg   label1
      movl val1+4,%eax
      cmpl val2,%eax
      jg   label1
      
    5. movl val1,%eax
      cmpl val2+4,%eax
      jg   label1
      movl val1+4,%eax
      cmpl val2,%eax
      jg   label1
      
  5. Assume that var1 and var1_end are defined as follows:
    .data
    var1: .org +20
      var1_end = .
    
    The direction .org +20 means increase the memory location counter by 20 (bytes). var1_end is effectively defined to be the address of the byte immediately following the 20 bytes allocated by the first line.

    What does the following code do to the allocated memory locations?

        movl $var1,%eax
    L1:
        cmpl $var1_end,%eax
        jb   L2
        movb $0,(%eax)
        addl $1,%eax
        jmp  L1
    L2:
    
    1. nothing
    2. the first 19 bytes are initialized to 0
    3. all 20 allocated bytes are initialized to 0
    4. this is an infinite loop, %eax always alternates between 0 and 1
    5. all 20 allocated bytes are initialized to 0, but one additional byte is also written to 0.
  6. Assume the bytes starting at location var1 are as follows:
    0x01 0x02 0x03 0x04 0x00 0x05 0x06 0x07 0x08 0x09'
    
    What is the value of %ecx when the following code completes?
        movl $10,%ecx
        movl $var1,%eax
    L1:
        cmpb $0,(%eax)
        jz   L2
        cmpl $0,%ecx
        jz   L2
        addl $1,%eax
        subl $1,%ecx
        jmp  L1
    L2:
    
    1. 10
    2. 7
    3. 6
    4. 4
    5. 0
  7. Read the assembly code in the previous question. Select a matching pseudocode represenation. Recall that $*\mathtt{eax}$ means the location pointed to by eax. $&\mathrm{var1}$ means the address of var1.

    1. \begin{algorithmic}
\STATE $\mathtt{eax} \leftarrow \&\mathrm{var1}$
\STATE $\...
... \STATE $\mathtt{ecx} \leftarrow \mathtt{ecx} - 1$
\ENDWHILE
\end{algorithmic}

    2. \begin{algorithmic}
\STATE $\mathtt{eax} \leftarrow \&\mathrm{var1}$
\STATE $\...
... \STATE $\mathtt{ecx} \leftarrow \mathtt{ecx} - 1$
\ENDWHILE
\end{algorithmic}

    3. \begin{algorithmic}
\STATE $\mathtt{eax} \leftarrow \&\mathrm{var1}$
\STATE $\...
... \STATE $\mathtt{ecx} \leftarrow \mathtt{ecx} - 1$
\ENDWHILE
\end{algorithmic}

    4. \begin{algorithmic}
\STATE $\mathtt{eax} \leftarrow \&\mathrm{var1}$
\STATE $\...
... \STATE $\mathtt{ecx} \leftarrow \mathtt{ecx} - 1$
\ENDWHILE
\end{algorithmic}

    5. \begin{algorithmic}
\STATE $\mathtt{eax} \leftarrow \&\mathrm{var1}$
\STATE $\...
... \STATE $\mathtt{ecx} \leftarrow \mathtt{ecx} - 1$
\ENDWHILE
\end{algorithmic}
  8. What is the value of register eax after the following code completes?
    movl $1, %eax
    addl %eax, %eax
    addl %eax, %eax
    

    1. 1
    2. 2
    3. 3
    4. 4
    5. 8
  9. How do we interpret the following assembly instructions?
    cmpl %eax, %ebx
    jna label1
    
    1. jump to label1 iff register eax is not greater than ebx, signed interpretation
    2. jump to label1 iff register ebx is not greater than eax, unsigned interpretation
    3. jump to label1 iff register ebx is not greater than eax, signed interpretation
    4. jump to label1 iff register eax is not greater than ebx, unsigned interpretation
    5. jump to label1 iff register eax is greater than ebx, unsigned interpretation
  10. Which flag(s) (Carry, Sign, Overflow and Zero) is/are set after the following code?
    movl $-1, %eax
    movl $-1,%ebx
    addl %eax,%ebx
    
    1. C, S
    2. C, S, O
    3. S, O
    4. C, O
    5. S


Copyright © 2004-10-06 by Tak Auyeung