# ex1 solns

Document Sample

```					Computer Systems and Architecture Exercise Sheet 1: Instruction Sets
1. Some simple arithmetic Show how the following code fragments could be written in MIPS assembly language, assuming that all variables are stored in main memory. You should only need to use the following instructions (see the attached list for full details): lw, dw, add, sub, addi, subi, mult a) b) c) d) e) a a a a a = = = = = (b + c) - (d + e); b * ( c + d); b*c + 25; (b + 10)*c – (d – 5); (b + 5)*c + (b – 3);

Solutions: Possible implementations of these code fragments in MIPS assembly language are: a) lw \$r1 , &b lw \$r2 , &c add \$r3 , \$r1 , \$r2 lw \$r1 , &d lw \$r2 , &e add \$r4 , \$r1 , \$r2 sub \$r1 , \$r3 , \$r4 sw \$r1 , &a b) lw \$r1 , &b lw \$r2 , &c lw \$r3 , &d add \$r4 , \$r2 , \$r3 mult \$r2 , \$r1 , \$r4 sw \$r2 , &a c) lw \$r1 , lw \$r2 , mult \$r3 addi \$r1 sw \$r1 , &b &c , \$r1 , r2 , \$r3 , 25 &a // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // Load b into r1 Load c into r2 Add b and c, result Load d into r1 Load e into r2 Add d and e, result Do the subtraction, Store the result in

in r3

in r4 result in r1 a

d) lw \$r1 , &b addi \$r2 , \$r1 , 10 lw \$r1 , &c mult \$r3 , \$r2 , \$r1 lw \$r1 , &d subi \$r2 , \$r1 , 5 sub \$r1 , \$r3 , \$r2 sw \$r1 , &a e) lw \$r1 , &b addi \$r2 , \$r1 , 5 lw \$r3 , &c mult \$r4 , \$r2 , \$r3 subi \$r2 , \$r1 , 3 add \$r1 , \$r2 , \$r4 sw \$r1 , &a

[Note: the main challenge in exercise (e) is to make sure that you only have to load b once]

2. Doing things the other way around Given the following fragments of MIPS assembly code, write the equivalent high-level construct a) lw \$r1 , &b lw \$r2 , &c sub \$r3 , \$r1 , \$r2 lw \$r1 , &d lw \$r2 , &e sub \$r4 , \$r1 , \$r2 add \$r1 , \$r3 , \$r4 sw \$r1 , &a b) lw \$r1 , lw \$r2 , lw \$r3 , addi \$r3 mult \$r1 mult \$r1 sw \$r1 , c) &b &c &d , \$r3 , 6 , \$r1 , \$r2 , \$r1 , \$r3 &a

lw \$r1 , &b addi \$r2 , \$r0 , 10 mult \$r1 , \$r1 , \$r2 sw \$r1 , &a   d)  lw \$r1 , &a addi \$r1 , \$r1 , 1 sw \$r1 , &a   e) lw \$r1 , &b mult \$r1 , \$r1 , \$r1 lw \$r2 , &c subi \$r2 , \$r2 , 3 add \$r1 , \$r1 , \$r2 sw \$r1 , &a Solutions: a) b) c) d) e) a a a a a = = = = = (b – c) + (d – e); b*c*(d+6); b*10; a + 1; b^2 + c – 3;

// Hint: r0 is always zero.

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 20 posted: 8/25/2009 language: English pages: 2