|
2,4 |
||
|
Next Lecture |
initialization
-- loop counters, variable values
termination check -- are we
done yet?
loop
body -- computation
branch -- branch back to the top of the loop
condition evaluation: branch to F if false -- which block of
code is executed?
computations for true case -- execute this block is condition is true
branch to exit block -- now we are done here
F: computations if false -- execute this block is condition is
false
exit: next block of
code -- we are done, move on.
register $t0 = 1 if $t1 < $t2 -
remember we are comparing the contents of the registers!

This is an example of the
structure of a loop where we keep a counter to count the number of times the
loop is executed. Note that we count down from 16 here. We could just as easily
have written the loop
such that we count up. However in this case, to test whether we are completed
we would have to compare the
counter value to the value 16. Comparison to the value 0 is easy in the SPIM
instruction set since register $0
always contains the value 0.
.text
li $t0, 16
loop: # first instruction of the
# loop body where you
# do
some work for
# each
iteration
#
addi $t0, $t0, -1 # decrement
the counter by 1
bne $t0, $0, loop # if we are not done go
back to the beginning
# of the loop
Now suppose in addition to
executing a loop a fixed number of times, we are also scanning memory,
perhaps reading successive elements in an array. We should be able to manage
the pointer to the locations in
memory. In this example the address of an array element is contained in $t2.
Adding 4 to $t2 provides the
address of successive elements which are fetched from memory in successive
iterations.
.data
start: .word 22, 23,2, 4, 45, -6
.text
li $t0, 6
la $t2,
start # this instruction
loads the value of start (an address) into $t0
loop: lw $t1,
0($t2) # access the array element in memory
#
# loop
body where you
# do
some work for
# each
iteration
#
addi $t2, $t2, 4
# point to the next location on memory
addi $t0, $t0, -1 # decrement
the counter by 1
bne $t0, $0, loop # if we are not done go
back to the beginning
# of the loop
Now let us grow the
preceding example into something more concrete: adding the elements of the
array. Let us
further assume that we do not know the number of elements of the array but we
do know the last element is zero.
Note that unlike the preceding examples, we continue to execute the loop until
we find a value of 0
rather than for a fixed number of iterations. We also find the need to use the
"j" instruction.
.data
start: .word 22, 23,2, 4, 45,0
.text
move $t0,
$0 # let is use this register to compute the sum
la $t2,
start #
let use this register as a pointer into the array
loop: lw $t5,
0($t2) # access the array
element in memory
beq $t5, $0, exit # if
the fetched value is zero exit the loop
addi $t2, $t2, 4
# point to the next location on memory
addi $t0, $t0, $t5 # update the sum
of the elements
j
loop
# now we are ready to start the next iteration
exit:
# the label "exit" is applied to the first
# instruction after the loop
The following code adds two
numbers if $t0 is equal to 0, otherwise the difference of the two numbers is
computed.
This represents the basic if-then-else programming construct.
.text
bne $t0, $0, else # check if the contents
of $t0 = 0, Branch if not equal
add $t1,
$t2, $t3 # this is the then part of the
code
j
exit
# now we need to jump over the else branch of the code
else: sub $t1, $t2, $t3 # this is the else part
of the code
exit: move $t4, $t1 # the
program continues using the value in $t1
----
----
The following sequence of
instructions implements the branch-on-less-than-or-equal-to instruction.
Consider the
implementation of the following instruction: ble
$t2, $t3, L1
.text
slt $t1, $t3, $t2 # this instruction is
recording if $t3 < $t2
beq $t1, $0, L1 # if the
preceding test fails (i.e., $t1 =0) then we have $t2 = < $t3
---
---
---
L1: ---
---
This example provides a code
sequence that implements branch-on-greater-than:
bgt $t2, $t3, L1
.text
slt $t1, $t3, $t2 # this instruction is
recording if $t3 < $t2
bne $t1, $0, L1 # if the
preceding test succeeds (i.e., $t1 = 1 signifying $t2 > $t3) then the branch
is taken
---
---
---
L1: ---
---