Cmpe 110

HomeWork 4  Floating Point and MIPS TAL

Due: Monday, Oct. 27, 2003

This assignment will test your ability in Floating point conversion and MIPS ISA. 

Floating point conversion

1.) Convert A = 1,985.2 to fixed-point binary. Use as many bits as needed for the integer portion, and use 30 bits for the significand.  Do-not round.(1 point)

A = 011111000001.001100110011001100110011001100

2.) Express number A from ex 1) in IEEE-754 single-precision floating-point format, using the appropriate rounding method. Show all work. (2 points)

A= 1.111100000100110011001100110011 x 210

ASPFloating Point = 0 10001001 11110000010011001100110   or A = 0 10001001 11110000010011001100111

Extra bits after end of 23-bit signifcand 01100110011

Round bit = 0, Sticky bit = OR(1100110011) = 1 , meaning the true number is closer to the , meaning don't add a one to the right end of the significand

SP floating point = 0 10001001 11110000010011001100110

MIPS Coding and Decoding

Consider the C code below.

Assume the address for A[0] is stored $s0, address for B[0] is stored in $s1, and address for flag is stored in $s2;

Also assume these arrays were declared earlier as integer arrays (with a size of 100).  Assume integers to be 4-bytes.

int counter = 100;

int x;

for ( x = 0; x < 100; x++)

    B[x] =  1 - x;

if (flag ==1){

  while(counter != 0){

     counter--;

     A[counter] = B[counter] + counter;

   }

}

else {

while(counter != 0){

     counter--;

     A[counter] = B[counter] - 2 * counter;

   }

}

}

3.) Convert this snippet of C code into MIPS TAL (meaning no pseudoinstructions). (3 points)

\***********This is one of many solutions*****/

            addi $t0, $zero 100             %realistically, 100 would be stored in memory, and we would load it, any way you do it is acceptable

            add $t1, $zero, $zero         %x=0

BeginFor: slti   $t2, $t1, 100            %x < 100

            beq $t2, $zero EndofFor     % if slti = 0 means x >= 100, which means done with for loop 

            sub  $t3, $zero, $t1            % 0-x

            addi  $t3, $t3, 1                 %1-x

            sll     $t4, $t1, 2                  %$t1 = x ;  $t4 = x * 2 * 2

           add $t4, $t4, $s1                 %&B[0] + 4 *x

           sw $t3, 0($t4)                     %B[x] = 1-x

            addi  $s1, $s1, 1              %x++

           J BeginFor                           %For Loop

EndofFor:  addi $t9, $zero, 1

           bne  $s1, $t9 else              %if flag != 1, goto else

while1: beq $zero, $t0 endwhile1    %while counter != 0

          addi $t0, $t0, -1                    %counter--

          sll $t5, $t0, 2                         %counter * 4 for B[counter]

          add $t6, $t5, $s1                    %&B[counter]

          lw $t6, 0($t6)                         %$B[counter]

          add $t6, $t6, $t0                     %$B[counter] + counter

          add $t7, $s0, $t5                    %$t7 = &A[counter]

           sw $t6, 0($t7)                         %A[counter] = B[counter] + counter

           j while1                                    %end while

endwhile1: J Endifelse                           % go to end of if/else block

else:

while2: beq $zero, $t0 endwhile2       %counter ==0, end while loop

          addi $t0, $t0, -1                    %counter--

          sll $t5, $t0, 2                         %counter * 4 for B[counter]

          add $t6, $t5, $s1                    %&B[counter]

          lw $t6, 0($t6)                         %$t6 = B[counter]

          sll $t8, $0, 1                            %counter*2

          sub $t6, $t6, $t8                     %$B[counter] - 2*counter

          add $t7, $s0, $t5                    %$t7 = &A[counter]

           sw $t6, 0($t7)                         %A[counter] = B[counter] + counter

           j while2                                      %while(counter != 0)

endwhile2:               

Endifelse 

 

********Note this is just one of many possibilities.  Good comments will make it easy for the grader to grade you, and an easy 3 points.

*********This solution was modified on 10/31/2003.  There were a few mistakes in the original solution.  I have corrected them here and on your hws.  Sorry for the inconvenience

4.) Consider the MIPS instruction code below.  Assume $s0 is the base address for 3 needed integer variables.  See problem 5, for how these variables look in memory.

            lw   $t0, 0($s0);

            lw   $t1, 4($s0);

            lw   $t3, 8($s0);

loop:    add $t1, $t1, $t0;

            addi $t1, $t1, 1 ;

            sll    $t2, $t0, 1;

            sub  $t0, $t2, $t1;

            slt   $t4, $t0, $t3;

            bne $zero, $t4, outofloop;

            j      loop;

outofloop:

Convert this MIPS TAL to C code.  Comment each line with instructions used to create the C statement on that line. (3 points)

Code should look similar to this.

int a;      //        lw   $t0, 0($s0);       

int b;        //     lw   $t1, 4($s0);

int c;       //       lw   $t3, 8($s0);

int temp;   //$t2 = 2*a

cin << a <<b << c;    // Grader this is not necessary for the grade, because of lack of clarity with the variable declarations

while (1){    // loop:

    b= b+a + 1;      //add $t1, $t1, $t0; addi $t1, $t1, 1

    temp = 2*a;   //  sll    $t2, $t0, 1;

    a =  temp - b;   //sub  $t0, $t2, $t1;

    if (a < c)        \\ slt   $t4, $t0, $t3;bne $zero, $t4, outofloop;

       break;         \\ jump out of loop

}   //end while(1)

 

 

5.)  Consider the MIPS code above.  Now consider the 2 memory blocks below.  Higher addresses go up, each block is 4 bytes or 1 word.

Use the numbers in the memory blocks, in the program above.  And find the answers to the question below.

address            Block 1                        Block2

$s0 + 12

     ????????

 ?????

$s0 + 8

-120

-500

$s0 + 4

3

2

$s0 --->

5

9

How many times will the program loop for each given block of memory? (1/2 point each)

Block 1 will loop 9 times or go through 10 iterations.  Block 2 will loop 11 times or go through 12 iterations.