Friday, October 4, 2013

Assign 2 - Exercise 2.1 Writeup

I used this as reference for some of the code, in addition to some examples  in our book.


I am assuming f,g,h,i are offsets 0, 4, 8, 12 respectively


a) f = g - h;
b) f = g + (h - 5);


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


a)
lw $r2, 4($s1) #g is 4 away from $s1
lw $r3, 8($s1) #h is 8 away from $s1
sub $r1, $r2, $r3 #f = g - h
sw $r1, 0($s1) #store f into $s1 offset 0


b)
lw $r3, 8($s1) #h is 8 away from $s1
addi $r3, $r3, -5 #add -5 to h and store result in h, $r3
lw $r2, 4($s1) #g is 4 away from $s1
add $r1, $r3, $r2 #add g and h, store in f, $r1
sw $r1, 0($s1) #store f into $s1 offset 0


Simple exercise meant to introduce MIPS instructions at very basic levels, add and sub.
2.1.2
For the C statements above, how many MIPS assembly instructions are needed to perform the C statement?


I am honestly not sure why this is a question.  Possibly to show that 1 operation in c corresponds to 1 operation in MIPS asm.  Also, these numbers may be incorrect if we are not counting the load and stores.
a) 4
b) 5


2.1.3


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


f=1
g=2
h=3
i=4


a)
In C
f = 2-3;
f = -1;
In MIPS
lw $r2, 4($s1) #load value of g into $r2
lw $r3, 8($s1) #load value of h into $r3
sub $r1, $r2, $r3 #$r1 = 3-2  = -1
sw $r1, 0($s1) #store $r1 into $s1 offset 0


b)
In C
f = 2+(3-5);
f = 2+ -2;
f = 0
In MIPS
lw $r3, 8($s1) #load value of h into $r3
addi $r3, $r3, -5 #add -5 to h and store result in h, $r3
#$r3 = 3-5 = -2
lw $r2, 4($s1) #load value of g into $r2
add $r1, $r3, $r2 #add g and h, store in f, $r1
#$r1 = 2 + -2  = 0
sw $r1, 0($s1) #store f into $s1 offset 0


Now, we are stepping through the code we made with actual data, both to verify our results, and verify that we understand what each instruction actually does.




a) addi f, f, 4
b) add  f, g, h
   add  f, i, f


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


a) f = f + 4;
b) f = g+h;
   f = i + f;
therefore
f = i + (g+h);


Now we do the opposite.  We use the MIPS that was given to us, and bring it back into C. Once again, to get us familiar with MIPS and its association with C.


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


f = 1;
g = 2;
h = 3;
i = 4;


a) f = f + 4
f = 1 + 4 = 5


b) f = g+ h
f = 2 + 3 = 5
f = i + f
f = 4 + 5 = 9


We end once again with data verification of our data.

To double verify the results of my data and get more practice, I put all my data into MARS (http://courses.missouristate.edu/KenVollmar/MARS/) set up as a consecutive running program outputting the results of f in each exercise and case.  I went a little overboard in the program adding in a stack for nested procedures and system calls for printing.  There were a few changes in the asm code, such as using pre-defined data, and a few more loads and stores, but the instructions performing the calculations didn’t change.

No comments:

Post a Comment