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) a = (b + c) - (d + e);
b) a = b * ( c + d);
c) a = b*c + 25;
d) a = (b + 10)*c – (d – 5);
e) a = (b + 5)*c + (b – 3);
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 , &b
lw $r2 , &c
lw $r3 , &d
addi $r3 , $r3 , 6
mult $r1 , $r1 , $r2
mult $r1 , $r1 , $r3
sw $r1 , &a
c) lw $r1 , &b
addi $r2 , $r0 , 10
// Hint: r0 is always zero.
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