sections in this module | City
College of San Francisco - CS270 Computer Architecture Module: MIPS-II |
module list |
Translating
switch statements
can be translated as a set of if statements
No big surprises here. We know how to translate if statements and this would just be messy.
Things get a bit more interesting in the case of a large switch that is dense. By dense we mean the cases are packed tightly together. This kind of a switch statement is more interesting when we consider how to translate it efficiently.
Suppose we have a really stupid switch statement that simply does this:
and let's suppose each value of i from 1 to 20 is included in the switch statement. We will ignore the silly code attached to each case, and just imagine that the code for each case was arbitrarily complex. Translating this switch statement as a series of if-then-else-if's is time-consuming. If, for example, the value of i is 20, we have to execute code to compare i to each of 1...19 before we find the right match! Wouldn't it be nice if a solution was available that was independent of the value of i?
There is! It is called a jump table. Here is how it works. First, we create the sequence of entries for each case like this (we will assume that the address of a is in $t9 and i is in $t1):
Now we have a sequence of labels L1 ... L20 and Ld for each case. We now construct our jump table in .data
Jumptbl: .data .word Ld,L1,L2,L3,L4,L5.........
where Ld takes the
0th entry (since it would be the default case as well) We now use
the following preamble code to branch to the default case if i
does not have a value that falls in our jump table:
Now we load the correct entry from our jump table and jump to it!
Question: what is the maximum number of instructions that it takes to get to the correct entry in the switch statement?
Answer: 11. If you answered 8 you forgot to expand the three pseudoinstructions!
Prev | This page was made entirely
with free software on linux: the Mozilla Project and Openoffice.org |
Next |