Transcript Slide 1

MIPS assembly
Review

Lat lecture, we learnt





addi,
and, andi, or, ori, xor, xori, nor,
beq, j, bne
An array is stored sequentially in the memory
The instructions are also stored sequentially in the
memory. Executing the code is to load then execute the
instructions one by one, unless we encounter a branch
condition.
Review


Label: A label is associated with exactly one instruction.
We can understand it as the address of the instruction in
the memory. Go to a label means that fetch that
instruction from the memory and execute it.
A label in MIPS looks like
L1: add $t0, $t1, $t2
Review


We talked about the if-else in MIPS.
When we want to do
if ($t0 == $t1)
$t0 = $t1 + $t2;
else
$t0 = $t1 - $t2;

One of the correct ways is
beq $t0, $t1, L1
sub $t0, $t1, $t2
j exit
L1: add $t0, $t1, $t2
exit:
Review

Suppose $t0, $t1, $t2 are storing 1,2,3,
respectively. What will the values in $t0 be after we
come to these instructions?
beq $t0, $t1, L1
sub $t0, $t1, $t2
L1:
add $t0, $t1, $t2
exit:
Exercise 2

How to implement this with only the instructions we
learnt?
if ($t1 > $t2)
$t0 = $t1;
else
$t0 = $t2;
Exercise 2
# if ($t1 > $t2) $t0 = $t1;
# else $t0 = $t2;
sub $t3, $t1, $t2
srl $t3, $t3, 31
bne $t3, $zero, L1
ori $t0, $t1, 0
j L2
L1:
ori $t0, $t2, 0
L2:
slt, slti

slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 <
$t2; else clear $t3 to be 0.



“Set Less Than.”
slti $t3, $t1, 100 – set $t3 to be 1 if $t1 <
100; else clear $t3 to be 0.
Using slt, the code is simpler.
Using slt
slt $t3,
bne $t3,
ori $t0,
j L22
L21:
ori $t0,
L22:
$t1, $t2
$zero, L21
$t1, 0
$t2, 0
Some Comments


Being able to write if-else, we can have all other fancy
things like for loop, while loop….
That is why we do not have an instruction for the for
loop or while loop, but we build it from the if-else.
Compiling a while loop in C

How to translate the following to MIPS assembly?

11
We first translate into a C program using if and goto
week04-3.ppt
7/21/2015
Compiling a while loop in C

Assume that i and k correspond to registers $s3 and
$s5 and starting address of array save is in $s6
12
week04-3.ppt
7/21/2015