SOLUTION

First Name: _________

Last Name: _________

Student ID: _________

This examination has 11 pages.

Read each question carefully before answering it.

Good luck!

Part 1

Each question is 2 marks – There are no part marks given!

Answer the following multiple choice questions on the bubble sheet at the back of this examination paper.

  1. Consider the following syntactically correct C code fragment:
    float aFloat = 3.1415;
    int sum = (int) aFloat + 0xFFFFFFFE;
    

    Which value does the variable sum contain when the above C code fragment has executed on our target machine?

    • a. 1.1415
    • b. -1
    • c. 0x00000001 (CORRECT)
    • aFloat = 3.1415
    • 0xFFFFFFFE = -2
    • (int) aFloat = 3
    • sum = 3 + (-2) = 1 or 0x00000001 (int in hex) * d. 5 * e. None of the above
  2. Which step in the compilation process transforms our C code into assembly instructions?
    • a. The step called the preprocessor
    • b. The step called the compiler – See Lecture 8 Slide 7 (CORRECT)
    • c. The step called the assembler
    • d. The step called the linker
    • e. None of the above
  3. Consider the following syntactically correct C function:
    char mystery( char someParam ) {
      char result = 0;
      if ( someParam > 0 ) result = someParam;
      else result = -someParam;
      return result;
    }
    

    What will it return once it has executed on our target machine with the parameter someParam set to the value -128?

    • a. 127
    • b. 128
    • c. -127
    • d. -128 (CORRECT) Explaination
    • e. None of the above
  4. Consider the following syntactically correct C code fragment:
    short count = 0xACE;
    printf( "count = %hhi\n", (char) count );
    

    What is printed on the computer monitor screen when the above C code fragment has executed on our target machine?

    • a. count = -50 (CORRECT) Explaination
    • b. count = 0xCE
    • c. count = 206
    • d. count = 0xACE
    • e. None of the above
  5. Consider the following syntactically correct C code fragment:
    short aShort = -2;
    char aChar = aShort;
    short sumS = 0xABBB + (short) aChar + 1;
    

    Which statement below is true about the above C code fragment once it has executed on our target machine, but has not yet exited the scope of the variables aShort, aChar and sumS?

    • a. sumS contains the hex value 0xABBA
    • b. aChar == aShort
    • c. Statements a. and b. are true. (CORRECT) Explaination
    • d. Only the statement b. is true.
    • e. None of the above
  6. Consider the following C code fragment:
    char char1 = 101;
    char char2 = _______ ;
    char sumOfChar = char1 + char2;
    

    Which value must be assigned to char2 in order for the sum of char1 and char2 to create a positive overflow?

    • a. No numbers would create a positive overflow when added to 101.
    • b. 42 (CORRECT) Explaination
    • c. 26
    • d. -230
    • e. None of the above
  7. Consider the following syntactically correct C code fragment:
    unsigned int x = 0xDECAF000;
    unsigned short y = 0xCAFE;
    if ( x > y ) printf("Caf ");
    if ( x < (signed short) y ) printf("Decaf ");
    if ( (unsigned char) x > y ) printf("Latte ");
    

    What is printed on the computer monitor screen when the above C code fragment has executed on our target machine?

    • a. Caf Decaf Latte
    • b. Caf Latte
    • c. Caf
    • d. Decaf
    • e. None of the above (CORRECT) Explaination
  8. Which range of values can be stored in the variable y declared in the C fragment code of Question 7 above?
    • a. [0 .. 2162^{16}]
    • b. [-128 .. 127]
    • c. [0 .. 21612^{16}-1]
    • d. [0 .. 21512^{15} – 1]
    • e. None of the above (CORRECT) Explaination

Part 2

The weight of each question is indicated in [ ] – Write your answer below each question unless instructed otherwise.

1

[Total marks: 15] Consider the following function mystery written in x86-64 assembly code, where the parameter x is in the register %edi and the parameter y is in the register %esi:

.globl power # mystery is crossed out and replaced with power; line 1
power: # x -> %edi, y -> %esi; mystery replaced with power; line 2
  xorl %eax,%eax # line 3
  movl $1,%r8d # line 4
loop: # loop replaces .L3 # line 5
  addl $1,%eax # line 6
  imull %edi,%r8d # line 7
  cmpl %eax,%esi # line 8
  jne loop # loop replaces .L3; line 9
  movl %r8d,%eax # line 10
  ret # line 11

2

[Total marks: 14] Consider a floating point number encoding scheme based on the IEEE floating point format and defined as follows:

From the above, we gather:

Table Powers of 2:

Power of 2x2^x Value
-10 1/1024 = 0.0009765625
-9 1/512 = 0.001953125
-8 1/256 = 0.00390625
-7 1/128 = 0.0078125
-6 1/64 = 0.015625
-5 1/32 = 0.03125
-4 1/16 = 0.0625
-3 1/8 = 0.125
-2 1/4 = 0.25
-1 1/2 = 0.5
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
11 2048
12 4096
13 8192
14 16384
15 32768
16 65536

Table of x86-64 Jumps:

Instruction Condition Description
jmp always Unconditional jump
je/jz ZF Jump if equal / zero
jne/jnz ~ZF Jump if not equal / not zero
js SF Jump if negative
jns ~SF Jump if nonnegative
jo OF Jump if overflow
jno ~OF Jump if not overflow
jq/jnle ~(SF ^ OF) & ~ZF Jump if greater (signed >)
jge/jnl ~(SF ^ OF) Jump if greater or equal (signed >=)
jl/jnge SF ^ OF Jump if less (signed <)
jle/jng (SF ^ OF) | ZF Jump if less or equal (signed <=)
ja/jnbe ~CF & ~ZF JumP if greater (unsigned >)
jae/jnb ~CF Jump if greater or equal (unsigned >=)
jb/jnae/jc CF Jump if less (unsigned <)
jbe/jna/jnc CF | ZF Jump if less or equal (unsigned <=)

Table of x86-64 Registers

64-bit (quad) 32-bit (double) 16-bit (word) 8-bit (byte) 8-bit (byte)  
63..0 31..0 15..0 15..8 7..0  
rax eax ax ah al Return value
rbx ebx bx bh bl Callee served
rcx ecx cx ch cl 4th arg
rdx edx dx dh dl 3rd arg
rsi esi si   sil 2nd arg
rdi edi di   dil 1st arg
rbp ebp bp   bpl Callee served
rsp esp sp   spl Stack position
r8 r8d r8w   r8b 5th arg
r9 r9d r9w   r9b 6th arg
r10 r10d r10w   r10b Callee served
r11 r11d r11w   r11b Callee served
r12 r12d r12w   r12b Callee served
r13 r13d r13w   r13b Callee served
r14 r14d r14w   r14b Callee served
r15 r15d r15w   r15b Callee served

Explainations:

3

Back to answer

char mystery( -128 ) {
  char result = 0;
  if ( -128 > 0 ) result = someParam;
  else result = -(-128);

So, it seems that result = 128 What is the bit pattern of 128? Interpreting 128 as an unsigned char we get: B2U(10000000) -> 272^7 -> 128 but we cannot interpret 128 as a signed char because 128 is outside the range of signed char -> [-128 .. 127], so the bit pattern 10000000 interpreted as a signed char is -128 Therefore even though it seems that result = 128 (10000000) It is actually the case that result = -128 return result i.e., -128

EXTRA

What is -128 as a bit pattern?

-128 -> T2B(X) -> (~(U2B(|X|)))+1 and X = -128

4

Back to answer

5

Back to answer

6

Back to answer

7

Back to answer

unsigned int x = 0xDECAF000;
unsigned short y = 0xCAFE;
if ( x > y ) printf("Caf ");

Promoting y to 32 bits as an unsigned i.e. padding with 0’s: 0xDECAF000 > 0x0000CAFE without a calculator, we can see that these 32 bits 0xDECAF000, interpreted as an unsigned value, will be > than 0x0000CAFE, also interpreted as an unsigned value. So, Caf is printed on the computer monitor screen of target machine.

if ( x < (signed short) y ) printf("Decaf ");

Casting y to 16 bits as a signed i.e. interpreting 0xCAFE as a signed value and promoting it to 32 bits still as a signed i.e. padding with 1’s: 0xFFFFCAFE 0xDECAF000 < 0xFFFFCAFE without a calculator, we can see that these 32 bits 0xDECAF000, interpreted as a signed value, will represent a larger negative value than 0xFFFFCAFE, also interpreted as a signed value Remember from previous questions that 0xFE = 0xFFFE = 0xFFFFFFFE = -2 So, Decaf is printed on the computer monitor screen of target machine.

if ( (unsigned char) x > y ) printf("Latte ");

Casting x (0xDECAF000) to a char -> 8 bits, we get 0x00 = 0 (unsigned value) promoting it to 16 bits still gives us 0 0x0000 > 0xCAFE without a calculator, we can see that this is not the case. So, Latte is NOT printed on the computer monitor screen of target machine.

The answer (Caf Decaf) is not one of the options.

8

Return to answer

unsigned short y = 0xCAFE;

y is declared as an unsigned short -> 16 bits so range of unsigned short is [0 .. 216 -1] which is not one of the options sbove.