Archive | October 2013

Computer Architecture Homework 2

Ed Smart

CSC 320

Homework 2

2.1 Assume the variables f, g, h, and i are given and could be considered 32-bit integers as declared in a C program.

a. f = g – h;

b. f = g + (h – 5);

2.1.1 For the C statements above, what is the corresponding MIPS assembly code? Use a minimal number of MIPS assembly instructions.

a. sub f, g, h                   #h is subtracted from g and the difference is stored                                        #in f

b. addi f, h, -5                  #Since MIPS can only perform 1 operation at a                                             #time and the value of (h – 5) must be computed                                            #and then added to g, the (h – 5) operation is                                                 #performed first and the result is stored in f                                                  #The addi operator can be used with a negative                                             #constant to give the same result as                                                             #”subtract immediate”    

add f, f, g                    #g is then added to f, which currently contains the                                        #difference of (h – 5), and the new result of g + (h – 5)                                   #is then stored in f

Part b can also be written using a temporary register as such:

addi $t0, h, -5                #5 is subtracted from h and the difference is stored                                        #in a temporary register $t0

add f, g, $t0                 #g is added to the value of (h – 5) stored in $t0 and                                        #the result of g + (h – 5) is stored in f

2.1.2 For the C statements above, how many MIPS assembly instructions are needed to perform the C statement?

For the C statements above, the total minimum number of MIPS assembly instructions needed is 3. MIPS can only perform one operation at a time and part b requires 2 operations (subtraction and addition), meaning two instructions are needed. Part a, however, only requires 1 simple sub instruction, making the total number of required instructions 3.

2.1.3 If the variables f, g, h, and i have values 1, 2, 3, and 4 respectively, what is the end value of f?

a. f = g – h;

1 = 2 – 3;

-1 = 2-3;

The end value of f here is -1. f’s initial value of 1 is replaced when the operation between g and h is performed, as the difference between g and h is stored in f.

I decided to throw this problem into MIPS for a better explanation. Register $t0 represents f, $t1 is g, and $t2 is h.

2.1.3a

The output:

I ran the program step by step so that you can see the process instead of just the end result. As you can see here, the program has just finished loading the values of f, g, and h into registers $t0, $t1, and $t2 respectively (note the values on the right, where $t2 is highlighted green), but has not yet performed the operation.

2.1.3a3

And this is after the operation has been performed. Notice the difference of $t1 and $t2 is now stored in $t0, replacing f’s original value of 1 with -1.

2.1.3a2

b. f = g + (h – 5);

1 = 2 + (3 – 5);

1 = 2 + (-2);

0 = 2 – 2;

The end value of f here is 0. The initial value of f is again replaced when the addition and subtraction operations are completed and the new value is placed in f.

Here’s the MIPS code of the problem for further clarity.

2.1.3b

And the outputs:

First, registers $t0, $t1, and $t2 are again given the values of f, g, and h respectively.

2.1.3b2

Then, as MIPS can only perform one instruction at a time, the problem must be broken into 2 steps. Here, 5 is subtracted from $t2’s value of 3 and the difference of -2 is stored in $t0.

2.1.3b3

Finally, $t1 is added to $t0 (which now contains the difference of $t2 and 5) and the result is stored in $t0, replacing its previous value of -2 with the final result, 0.

2.1.3b4

a. addi f, f, 4

b. add f, g, h

     add f, i, f

2.1.4 For the MIPS assembly instructions above, what is the corresponding C statement?

a. f = f + 4;          //4 is added f and the value of f is replaced with the new                                //sum

b. f = g + h;         //g and h are added and the resulting sum is stored in f

f = i + f;          //i is then added to f, which currently holds the value of (g + h),                      //and the new sum is then stored in f, meaning f = g + h + i

2.1.5 If the variables f, g, h, and i have values 1, 2, 3, and 4 respectively, what is the end value of f?

a. f = f + 4;

1 = 1 + 4;             //initial value of f is added to 4

5 = 1 + 4;            //sum of f and 4 is stored in f, replacing its initial value

The end value of f here is 5. f’s initial value of 1 is added to 4 and the resulting sum of 5 is then stored in f.

Here’s a brief MIPS code to better illustrate the process using register $t0 to represent f:

2.1.5a

The output:

First f’s assigned value of 1 is stored in register $t0.

2.1.5a2

The constant 4 is then added to $t0, which contains f’s initial value of 1. The sum is then stored in $t0, updating its value to 5.

2.1.5a3

b. f = g + h;

f = i + f;

1 = 2 + 3;         //initial values of f, g, and h respectively

5 = 2 + 3;         //initial value of f is replaced by the sum of g and h

5 = 4 + 5;         //i is added to the new value of f, which is the sum of g + h

9 = 4 + 5;         //f is updated with the sum of i + f, which is equivalent to                                //(g + h + i)

The end value of f here is 9. The sum of g and h (2 + 3) is 5, which is stored in f. The value of i is then added to f and stored as f’s new value, so f = g + h + i = 2 + 3 + 4 = 9.

Here, I’ve again illustrated the process with some MIPS code:

2.1.5b

The output:

First, the values of f, g, h, and i are stored in registers $t0, $t1, $t2, and $t3 respectively.

2.1.5b2

Then, the values of $t1 and $t2 are added and the sum is stored in $t0, replacing its initial value of 1 with 5.

2.1.5b3

Finally, the value stored in $t3 is added to the new value stored in $t0, and $t0 is then updated with the final sum of 9.

2.1.5b4

2.2 Assume the variables f, g, h, and i are given and could be considered 32-bit integers as declared in a C program.

a. f = g – f;

b. f = i + (h – 2);

2.2.1 For the C statements above, what is the corresponding MIPS assembly code? Use a minimal number of MIPS assembly instructions.

a. sub f, g, f                          #subtract f from g, store difference in f

b. addi f, h, -2                        #subtract 2 from h, store difference in f

add f, i, f                              #add i to f (which contains h – 2), replace f’s                                                   #value with new sum

2.2.2 For the C statements above, how many MIPS instructions are needed to perform the C statement?

In total, 3 MIPS instructions are needed to perform the C statements above. Part a requires 1 instruction to subtract f from g and store the difference in f. Part b requires 2 instructions. The first instruction subtracts 2 from h using addi and -2 and stores the difference in f. The second instruction adds i to the value of f (which contains h – 2) and then stores the sum in f, meaning f = i + (h – 2).

2.2.3 If the variables f, g, h, and i have values 1, 2, 3, and 4 respectively, what is the end value of f?

a. f = g – f;

1 = 2 – 1;                //initial values, f’s final value remains 1

In this case, the end value of f is the same as its initial value of 1. g = 2 and f = 1, 1 is subtracted from 2 and stored in f.

As shown in MIPS here, where $t0 represents f and $t1 represents g:

2.2.3a

First, the values 1 and 2 are stored in registers $t0 and $t1 respectively.

2.2.3a2

$t0’s value is then subtracted from $t1’s and the difference is stored in $t0, in this case, resulting in $t0’s value remaining the same, as it is overwritten with the difference of 1.

b. f = i + (h – 2);

1 = 4 + (3 – 2);

5 = 4 + 1;

The end value of f here is 5. First, 2 is subtracted from h’s value of 3, resulting in the difference of 1 being stored in f. Then, i’s value of 4 is added to the 1 stored in f and f is updated with the new value of 5.

As shown here, where f, h, and i are stored in registers $t0, $t2, and $t3 respectively:

2.2.3b

The output:

First, the values are stored in the registers:

2.2.3b2

Then, $t0 is updated with the difference of $t2 and 2, which is the same as its original value.

2.2.3b3

Finally, $t0 is added to $t3 and the sum is stored back into $t0.

2.2.3b4

a. addi f, f, 4

b. add f, g, h

    sub f, i, f

2.2.4 For the MIPS assembly instructions above, what is a corresponding C statement?

a. f = f + 4;                //4 is added to f and the sum is stored as f’s new value

b. f = g + h;              //g and h are added and the sum is stored in f

f = i – f;                     //f is then subtracted from i and the difference is                                           //stored in f

2.2.5 If the variables f, g, h, and i have values 1, 2, 3, and 4 respectively, what is the end value of f?

a. addi f, f, 4

addi 1, 1, 4                 #initial values, 1 + 4 is added and stored in f

The end value of f in this case is 5. f’s initial value is 1, which is added to the constant 4, resulting in a sum of 5 being stored in f as its new value.

Which can be seen here:

2.2.5a

After loading f’s initial value of 1 into register $t0, 4 is then added to that value and $t0’s final value is the sum, which is 5.

2.2.5a2

b. add f, g, h

sub f, i, f

add 1, 2, 3               #initial values

sub 5, 4, 5              #new value of f is subtracted from i

The end value of f here is -1. First, g and h are added (2 + 3) and the sum of 5 is stored in f. Then, f’s value of 5 is subtracted from i’s value of 4 and the difference of -1 is stored in f.

I’ve shown the process in MIPS here, where f, g, h, and i are represented by registers $t0, $t1, $t2, and $t3 respectively:

2.2.5b

Output:

First, the initial values are loaded into their corresponding registers.

2.2.5b2

$t0’s value is then replaced with the sum of $t1 and $t2, which is 5.

2.2.5b3

Then, $t0’s new value of 5 is subtracted from $t3’s value of 4 and the resulting -1 is stored back into $t0.

2.2.5b4

2.3 Assume the variables f and g are given and can be considered 32-bit integers as declared in a C program.

a. f = -g – f;

b. f = g + (-f – 5);

2.3.1 For the C statements above, what is the corresponding MIPS assembly code? Use a minimal number of MIPS assembly instructions.

a. sub f, -g, f             #f is subtracted from -g and the difference is stored in f

b. addi f, -f, -5         #addi is used to add -5 to -f, and the sum is stored in f

add f, g, f                 #g is then added to f and the new sum is stored in f

2.3.2 For the C statements above, how many MIPS assembly instructions are needed to perform the C statement?

For the C statements above, 3 MIPS assembly instructions are needed for part a. Part a requires 1 statement to make g negative by multiplying it by -1. The second statement is used to move -g from the Lo register, where the quantity is stored, to where g is stored. The third statement subtracts f from -g and stored the differences of -3 in f.

Part b requires 4 MIPS assembly instructions. The first one multiplies f by -1 in order to get -f, which is stored in the Lo register. The second one moves -f from Lo back to where f is stored, replacing f’s initial value with -f. The third instruction subtracts 5 from f’s new value and stores the difference in f. The last instruction adds f to g and stores the sum in f.

2.3.3 If the variables f, g, h, i, and j have variables 1, 2, 3, 4, and 5 respectively, what is the end value of f?

a. f = -g – f;

1 = -2 – 1;            //initial values

-3 = -2 – 1;          //f’s value is replaced with the result of the operation

The end value of f here is -3. 1 is subtracted from -2 and the result is stored in f.

This is shown more clearly here:

2.3.3a

First the values are stored in their corresponding registers.

2.3.3a2

g’s value of 2 is then multiplied by -1 in order to get -g, and the result is stored in the Lo register.

2.3.3a3

-g is then moved from the Lo register into $t1, replacing g with -g.

2.3.3a4

f is then subtracted from -g and the result is stored in $t0, replacing f’s initial value of 1 with -3.

2.3.3a5

b. f = g + (-f – 5);

1 = 2 + (-1 – 5);      //initial values

-4 = 2 + (-6);          //final value is stored in f after the operation is finished

The end value of f in this case is -4.  First, 5 is subtracted from -f’s initial value of -1. The resulting -6 is then added to 2, giving a sum of -4, which is then stored in f.

And here is the problem written in MIPS:

2.3.3b

After the initial values are loaded into the registers, f’s initial value of 1 is multiplied by -1 in order to get -f, which is stored in the Lo register.

2.3.3b2

The -f value contained in Lo is then moved into $t0, replacing f’s initial value with -f.

2.3.3b3

5 is then subtracted from f and the difference is stored in f.

2.3.3b4

Finally, f is added to g and the sum is stored in f.

2.3.3b5

a. addi f, f, -4

b. add i, g, h

      add f, i, f

2.3.4 For the MIPS statements above, what is a corresponding C statement?

a. f = f – 4;          //The addi operator used with a negative constant is the                                //same as subtracting a positive constant

b. i = g + h;        //h is added to g and the sum is stored in i

f = i + f;              //f is then added to i and f is updated with the sum

2.3.5 If the variables f, g, h, and i have values 1, 2, 3, and 4 respectively, what is the end value of f?

a. f = f – 4;

1 = 1 – 4;           //initial values

-3 = 1 – 4;         //difference is stored in f

The end value of f is -3. 4 is subtracted from f’s initial value of 1 and the difference is then stored in f.

Here is the MIPS code for the problem:

2.3.5a

Output:

First, f’s value of 1 is stored in register $t0.

2.3.5a2

Then, 4 is subtracted from the value of 1 stored in $t0 and $t0 is then updated with the difference, -3.

2.3.5a3

b. i = g + h;

f = i + f;

4 = 2 + 3;         //initial values

5 = 2 + 3;        //updated value of i

1 = 5 + 1;        //initial value of f is added to updated value of i

6 = 5 + 1;        //final result is stored in f

The end value of f is 6. First, g and h are added and the sum is stored in i. Then, f is added to the updated value of i and the result is stored in f.

The problem coded in MIPS:

2.3.5b

Output:

First, the values of f, g, h, and i are stored in registers $t0, $t1, $t2, and $t3 respectively.

2.3.5b2

Then, the value of i stored in $t3 is replaced with the sum of g and h.

2.3.5b3

Finally, the new value of i is added to f and the sum is stored in $t0.

2.3.5b4