CS 282 Winter 2003

Assignment 1 Solutions

  1. (10 points) Assume that an instruction fetch, single operand load, or storage of a result takes 7 time units to complete. Instruction decode, operation execution, and program counter update each take 1 time unit to complete. How long does it take to execute the following code sequence?

mul   A, B, C

div    D, E, F

add    X, A, B

There are two ways in which you can solve this problem:

Method 1

consider:           mul A, B, C

Instruction fetch

7

PC update

1

Instruction decode

1

operand load for C

7

operand load for B

7

operation execution

1

storage of a result A

7

Total

31 time units

            It is similar in the case of the other two instructions.

            Hence, total time taken is  (31 * 3) = 93 time units.

 

            Method 2

As in method 1, for the first instruction and also second instruction,

time taken = 31 units. But for third instruction,

Instruction fetch

7

PC update

1

Instruction decode

1

operand load for C

0 (Because it was already loaded once before)

operand load for A

7

operation execution

1

storage of a result

7

Total

24 time units

Hence, total time taken  = 31 + 31 + 24 = 86 time units.

  1. (5 points) Explain briefly the process of translation from a high level language to machine language. Where and how do a compiler and an assembler fit into this process?

 

 

 

 

 

 

 


A compiler as we can see above, is a computer program that translates code written in a high level language into an intermediate-level abstract language.

 

An assembler is a computer program that translates an assembly language program into machine language, that can be directly executed by the hardware.

 

  1. (3 points) Convert the following numbers into decimal system:
    1. 1110 11002     =  4 + 8 + 32 + 64 + 128  = 136
    2. ABF16             = (F) * (16^0) + (B) * (16^1) * (A) *(16^2)

= 15 + (11 * 16 ) + ( 10 * 256 ) =  2751

    1. 12678              =  7 + (6 * 8) + (2 * 64) + ( 1 * 512) =  695
  1. (10 points) Convert 104410  into
    1. Octal system and
    2. Hexadecimal system,

using Subtraction of Powers method. Please show the steps involved.

 

Octal system:

Powers of 8 = { 1, 8, 64, 512, 4096,…}

 

1044 <   ( 8 ^ 4 )  à m = 4

The number is of the form, C3C2C1C0.

 

1044   - (2) * (8^3)  = 20        à C3 = 2

20       - (0) * (8^2) = 20         à C2 = 0

20       - (2) * (8^1)  = 4          à C1 = 2

1044   - (4) * (8^0)  = 0          à C0 = 4

 

Therefore, the number is  20248.

 

Hexadecimal system:           

 

Powers of 16 = { 1, 16, 256, 4096,…}

 

1044 <   ( 16 ^ 3 )  à m = 3

The number is of the form, C2C1C0.

 

1044   - (4) * (16^2) = 20       à C2 = 4

20       - (1) * (16^1)  = 4        à C1 = 1

4         - (4) * (16^0)  = 0        à C0 = 4

 

Therefore, the number is  41416.

 

  1. (6 points)  Problem 3.12 in textbook, page 81, chapter 3

 

(a)   Given 64.5, to convert it into binary:

 

64.5  = 64 + 0.5       f6f5f4f3f2f1f0   = 1000000

 

2 * 0.5 = 1 + .0            f -1 = 1

 

Therefore, 64.510 = 1000000.12

 

(b)   Given 0.025. to convert it into binary:

 

0.025   = 0 + 0.025      So,       f0 =  0

 

2 * 0.025 =  0 + 0.05               f -1 = 0

 

2 * 0.05  = 0 + 0.1                   f -2 = 0

 

2 * 0.1    =  0 + 0.2                  f -3 = 0

 

2 * 0.2    =  0 + 0.4                  f -4 = 0              // step (4)

 

2 * 0.4    =  0 + 0.8                  f -5 = 0

 

2 * 0.8    =  1 + 0.6                  f -6 = 1

 

2 * 0.6    =  1 + 0.2                  f -7 = 1

 

This is going to repeat from step (4) above.

                                   

Hence, the answer is 0.0000011

 

 

 

 

 

 

(c)    Given 18.0625, to convert into binary:

 

18.0625 = 18 + 0.0625          f4f3f2f1f0 = 10010

 

2 * 0.0625 = 0 + 0.125            f -1 = 0

2 * 0.125 = 0 + 0.25                f -2 = 0

2 * 0.25 = 0 + 0.5                    f -3 = 0

2 * 0.5 = 1 + 0.0                      f -4 = 1

 

Therefore, 18.062510 = 10010.00012

 

  1. (1 point) Convert 8A716 into octal system, using the ‘Powers of Two’ special conversion technique (That is, notice that both the bases 16 and 8 are powers of two and use the technique we discussed in class.)

 

16    = 2 ^ 4

Therefore, a = 4.

 

            First, lets convert into binary:

            Convert each symbol of 8A716 into the equivalent binary string of 4 digits

(since, a = 4)

 

1000    1010    0111

 

Concatenate the result and drop leading zeros.

 

100010100111

 

Now, lets convert it from binary to octal:

For octal system, 8 = 2 ^ 3 à a = 3

 

Group the digits together in groups of three.

 

100      010      100      111

 

No need to add any leading zeros, because we already have 4 complete groups of 3 bits.

 

Substitute for each group, an equvalent symbol in octal system.

 

So, the answer is  42478.